-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (66 loc) · 2.17 KB
/
server.js
File metadata and controls
80 lines (66 loc) · 2.17 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var WebSocketServer = require("websocket").server;
var http = require("http");
process.title = "node-chat";
var HOST = "127.0.0.1";
var PORT = 5000;
var ORIGIN = "http://test.local";
var server = http.createServer(function(request, response){}).listen(PORT, HOST);
wsServer = new WebSocketServer({
httpServer: server
});
var hist = [];
var clients = [];
wsServer.on("request", function(request){
// reject from other hosts
if (doReject(request)) {
console.log((new Date()) + " " + request.origin + " was rejected.");
return;
}
// accept connection
var connection = request.accept(null, request.origin);
console.log((new Date()) + " " + request.origin + " was connected.");
var index = clients.push(connection) - 1;
connection.on("message", function(message){
// skip if non-utf message
if (message.type !== "utf8") {
return;
}
console.log((new Date()) + " Receive new message");
var msgData;
try {
msgData = JSON.parse(message.utf8Data);
} catch (e) {
console.log((new Date()) + " Error:");
console.log(e);
return;
}
/**
* messageData = {
* type:"auth|spick",
* data:""
* }
*/
if (msgData.type === 'auth') {
console.log((new Date()) + " Auth as '" + msgData.data + "'");
clients[index].userName = msgData.data;
} else
if (msgData.type === 'spick') {
console.log((new Date()) + " '" + clients[index].userName + "' say: " + msgData.data);
wsServer.broadcast(JSON.stringify({
user: clients[index].userName,
data: msgData.data
}));
}
});
connection.on("close", function(connection){
console.log((new Date()) + " User '" + clients[index].userName + "' disconnected.");
clients.splice(index, 1);
});
});
process.addListener('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
var doReject = function(request) {
// return request.origin !== ORIGIN;
return false;
};