This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
74 lines (62 loc) · 2.29 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Starts up a WebSocket and a HTTP Server
*
* The express HTTP server will expose the HTML and Javascript
* from the ./public directory
*
* The WebSocket server acts as a signaling server for the WebRTC communication,
* and will relay messages from the sender to the other clients.
*/
const express = require('express');
const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
const PORT = process.env.PORT || 8000;
const app = express();
const serverOptions = {
key: fs.readFileSync('./ssl/key.pem'),
cert: fs.readFileSync('./ssl/cert.pem'),
passphrase: '123456789',
};
// use express static to deliver resources HTML, CSS, JS, etc)
// from the public folder
app.use(express.static('public'));
app.use(function httpsRedirectMiddleware(req, res, next) {
if (req.headers['x-forwarded-proto'] === 'http') {
res.redirect('https://' + req.get('Host') + req.url);
} else {
next();
}
});
const httpsServer = https.createServer(serverOptions, app).listen(PORT);
const url = 'https://localhost:' + PORT;
console.log('HTTPS server is up and running on ' + url);
// Start up the WebSocket server
const wssServer = new WebSocket.Server({ server: httpsServer });
console.log('WebSocket Secure server is up and running.');
wssServer.on('connection', function onWsConnection(client) {
console.log(
'A new WebSocket client was connected. Clients connected:',
this.clients.size
);
// Listen to messages from the connected client
client.on('message', function onIncomingWsMessage(message) {
console.log('Relaying message', message);
wssServer.broadcast(message, client);
});
});
// broadcasting the message to all WebSocket clients.
wssServer.broadcast = function onWsBroadcast(data, exclude) {
const numberOfClients = this.clients ? this.clients.size : 0;
// If there's only 1 or 0 clients connected there's no need to broadcast
if (numberOfClients < 2) return;
console.log(
'Broadcasting message to ' + numberOfClients + ' WebSocket clients.'
);
this.clients.forEach(function broadCastToClient(client) {
// Avoid sending back the message to the sender
if (client === exclude) return;
if (client.readyState === client.OPEN) client.send(data);
else console.error('Error: the client state is ' + client.readyState);
});
};