-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathserver.js
51 lines (40 loc) · 1.53 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* CONFIG */
var SSLPORT = 443; //Default 443
var HTTPPORT = 80; //Default 80 (Only used to redirect to SSL port)
var privateKeyPath = "./cert/key.pem"; //Default "./cert/key.pem"
var certificatePath = "./cert/cert.pem"; //Default "./cert/cert.pem"
/* END CONFIG */
var fs = require('fs');
var express = require('express');
var https = require('https');
var app = express();
app.use(express.static(__dirname + '/webcontent'));
var privateKey = fs.readFileSync( privateKeyPath );
var certificate = fs.readFileSync( certificatePath );
var server = https.createServer({
key: privateKey,
cert: certificate
}, app).listen(SSLPORT);
var io = require('socket.io').listen(server, { log: false });
// Redirect from http to https
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + ":"+ SSLPORT + "" + req.url });
res.end();
}).listen(HTTPPORT);
console.log("Webserver & Socketserver running on port: "+SSLPORT+ " and "+ HTTPPORT);
//Handel connections
io.sockets.on('connection', function (socket) {
console.log("New user connected:", socket.id);
io.emit('clients', io.engine.clientsCount);
socket.on('disconnect', function () {
console.log("User disconnected:", socket.id);
socket.broadcast.emit('clients', io.engine.clientsCount);
});
socket.on('d', function (data) {
data["sid"] = socket.id;
//console.log(data["a"]);
socket.broadcast.emit('d', data); //Send to all but the sender
//io.emit("d", data); //Send to all clients (4 debugging)
});
});