-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
52 lines (41 loc) · 1.27 KB
/
serve.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
var WebSocketServer = require('websocket').server;
var http = require('http');
const webSocketsServerPort = 8080
function log(message) {
console.log(`${new Date()} ${message}`)
}
var server = http.createServer(function(request, response) {
/* empty */
})
server.listen(webSocketsServerPort, function() {
log(`Server is listening on port ${webSocketsServerPort}`)
})
wsServer = new WebSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket
// request is just an enhanced HTTP request. For more info
// http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
})
var clients = []
wsServer.on('request', function(request) {
log(`Connection from origin ${request.origin}.`)
var connection = request.accept(null, request.origin)
clients.push(connection)
connection.on('message', function(message) {
if (message.type === 'binary') {
for (var i = 0, iMax = clients.length; i < iMax; i++) {
if (clients[i] !== connection) {
clients[i].send(message.binaryData)
}
}
}
})
connection.on('close', function(connection) {
log('Client disconnected')
let index = clients.indexOf(connection)
if (index < 0) {
return
}
clients = clients.filter((x) => { return x !== connection })
})
})