-
Notifications
You must be signed in to change notification settings - Fork 9
/
wsserver.js
72 lines (67 loc) · 1.66 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
65
66
67
68
69
70
71
72
/**
* wsserver -- websockets
* http://einaros.github.io/ws/
*/
var wss = require("nodejs-websocket");
var endsWith = function(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
};
var server = wss.createServer(function (connection) {
connection.nickname = null;
//This seems to ring
connection.on("text", function (str) {
console.log("TEXT "+str);
//block nickname only -- empty message
if (!endsWith(str, ':')) {
if (connection.nickname === null) {
connection.nickname = str;
console.log("WSSFOO");
broadcast(str);
} else {
broadcast("["+connection.nickname+"] "+str);
}
}
});
function broadcast(str) {
console.log("BROADCAST "+str);
server.connections.forEach(function (connection) {
connection.sendText(str);
});
}
connection.on("close", function () {
broadcast(connection.nickname+" left");
});
connection.on("error", function(err) {
console.log("WSS error "+err);
});
});
server.listen(4444);
/**
wss = new WSServer({port:4444});
console.log("WebSocketServer "+wss);
var clients = [];
wss.on('connection', function(socket) {
console.log("WSS "+clients);
if (clients.indexOf(socket) < 0) {
clients.push(socket);
}
console.log("WSS foo");
var length=0;
socket.on('message', function (msg) {
length = clients.length;
//console.log('Recieved: ', msg, '\n',
//'From IP: ', socket.upgradeReq.connection.remoteAddress);
//socket.send(msg);
console.log("WSS msg "+length+" "+msg)
var broadcast = function(msg) {
for(var i=0;i<length;i++) {
console.log(clients[i]);
clients[i].send(msg);
}
}
if (msg != "") {
broadcast(msg);
}
});
});
*/