-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.js
More file actions
49 lines (35 loc) · 960 Bytes
/
hello.js
File metadata and controls
49 lines (35 loc) · 960 Bytes
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
var net = require('net')
var p = require('./oprate_file').p
var express= require('express')
var clientList = []
net.createServer().on('connection', function(client){
client.name = client.remoteAddress + ':' + client.remotePort
client.write('Hi!\n' + client.name + '!\n');
clientList.push(client)
client.on('data', function(data){
broadcast(data, client)
})
client.on('end',function () {
clientList.splice(clientList.indexOf(client), 1)
})
client.on('err', function(e){
p(e)
})
function broadcast (message, client) {
var cleanup = []
for (var i =0 ; i<clientList.length; i++) {
if(client != clientList[i]){
if(clientList[i].writable){
clientList[i].write(client.name + " says " + message)
}else{
cleanup.push(clientList[i])
clientList[i].destory()
}
}
};
for (i = 0; i<cleanup.length ; i++) {
clientList.splice(clientList.indexOf(cleanup[i]), 1)
};
}
}).listen(8888)
p('hello world')