forked from bensinober/node_rfidgeek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsserver.js
64 lines (55 loc) · 2.13 KB
/
wsserver.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
// WEBSOCKET SERVER
var WebSocketServer = require('websocket').server;
var http = require('http');
var connections = [];
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function(request) {
console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
// accept connection - you should check 'request.origin' to make sure that
// client is connecting from your website
// (http://en.wikipedia.org/wiki/Same_origin_policy)
var connection = request.accept(null, request.origin);
connections.push(connection);
console.log(connection.remoteAddress + " connected - Protocol Version " + connection.webSocketVersion);
connection.on('message', function(message) {
if (message.type === 'utf8') { // accept only text
console.log((new Date()) + ' message: ' + message.utf8Data);
// rebroadcast command to all clients
connections.forEach(function(destination) {
destination.sendUTF(message.utf8Data);
});
}
});
// Handle closed connections
connection.on('close', function() {
console.log(connection.remoteAddress + " disconnected");
var index = connections.indexOf(connection);
if (index !== -1) {
// remove the connection from the pool
connections.splice(index, 1);
}
});
});
module.exports = wsServer;