-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·45 lines (35 loc) · 1.03 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
var net = require("net")
var util = require('util');
var telnetServer = net.createServer()
var ActiveUserList = []
var DisconnectedUserList = []
telnetServer.on('connection',function(client){
client.name = client.remoteAddress+":"+client.remotePort;
ActiveUserList.push(client)
broadcast(" has joined the conversation\r\n",client)
client.on('data',function(data){
broadcast(data,client);
})
client.on('end', function(data){
broadcast("has left the conversation",client);
})
})
function broadcast(message,client){
var i;
for(var i=0;i<ActiveUserList.length;i++){
if (client !== ActiveUserList[i]){
console.log(util.inspect(ActiveUserList[i], false, null));
if (ActiveUserList[i].writable === true){
ActiveUserList[i].write(client.name+":"+message);
}
else{
DisconnectedUserList.push(ActiveUserList[i]);
ActiveUserList[i].destroy();
}
}
}
for (var j=0;j<DisconnectedUserList.length;j++){
ActiveUserList.splice(ActiveUserList.indexOf(DisconnectedUserList[j]), 1);
}
}
telnetServer.listen(9000);