-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (41 loc) · 1.11 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
46
47
48
49
50
51
52
53
var express= require('express');
var SocketServer= require('ws');
var PORT= process.env.PORT||8000
var server= express().use(express.static('www')
).listen(PORT,function(){console.log('listening on '+PORT)} );
var ws= new SocketServer.Server({server});
ws.on('connection',function(w){
console.log('connection...')
online++;
var sysData={
'pic':'',
'content':'',
'infor':''
};
w.on('open',function(){
console.log('open...');
});
w.on('message',function(data){
// console.log('message...'+data);
// var msg= online+'*'+data
ws.clients.forEach(function(client){
// client.send( msg )
client.send(data)
})
});
w.on('close',function(){
console.log('close...')
online--;
clearInterval(intervalSend);
});
try{
sysData.content='welcome join the chat !'
sysData.infor='online: '+online ;
sysData.content='';
w.send( JSON.stringify(sysData) ) ; //send only supose STRING type
var intervalSend= setInterval(function(){ sysData.infor='online:'+online; w.send(JSON.stringify(sysData)) },5000)
}catch(e){
console.log( 'interval send error,need clear it.')
}
});
var online=0;