-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (46 loc) · 1.72 KB
/
server.js
File metadata and controls
52 lines (46 loc) · 1.72 KB
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
52
const WebSocketServer = require('ws').Server,
express = require('express'),
https = require('https'),
app = express(),
fs = require('fs');
const pkey = fs.readFileSync('./ssl/key.pem'),
pcert = fs.readFileSync('./ssl/cert.pem'),
options = {key: pkey, cert: pcert, passphrase: '123456789'};
var wss = null, sslSrv = null;
// use express static to deliver resources HTML, CSS, JS, etc)
// from the public folder
app.use(express.static('public'));
app.use(function(req, res, next) {
if(req.headers['x-forwarded-proto']==='http') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});
// start server (listen on port 443 - SSL)
sslSrv = https.createServer(options, app).listen(443);
console.log("The HTTPS server is up and running");
// create the WebSocket server
wss = new WebSocketServer({server: sslSrv});
console.log("WebSocket Secure server is up and running.");
/** successful connection */
wss.on('connection', function (client) {
console.log("A new WebSocket client was connected.");
/** incomming message */
client.on('message', function (message) {
/** broadcast message to all clients */
wss.broadcast(message, client);
});
});
// broadcasting the message to all WebSocket clients.
wss.broadcast = function (data, exclude) {
var i = 0, n = this.clients ? this.clients.length : 0, client = null;
if (n < 1) return;
console.log("Broadcasting message to all " + n + " WebSocket clients.");
for (; i < n; i++) {
client = this.clients[i];
// don't send the message to the sender...
if (client === exclude) continue;
if (client.readyState === client.OPEN) client.send(data);
else console.error('Error: the client state is ' + client.readyState);
}
};