-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (40 loc) · 1 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
/**
* Created by Administrator on 2017/4/26.
*/
var webSocketServer=require("ws").Server;
var wss=new webSocketServer({port:4567});
var clientlength=1;
function fasong(state,obj) {
wss.clients.forEach(function (client) {
if (state==1){
client.send(obj.name+"说了:"+obj.neirong);
}
if(state==0){
client.send(obj.name+"退出了");
}
})
}
wss.on("connection",function(ws){
var user;
console.info("连接成功");
ws.send("当前连接人数"+clientlength+"人");
clientlength+=1;
ws.on("message",function (req,flags) {
var obj=eval("("+req+")");
console.info(obj.neirong);
console.info(obj.name);
if (obj.name!=""){
user=obj;
fasong(1,obj)
}
});
ws.on("close",function (close) {
clientlength-=1;
try{
console.info(user.name);
fasong(0,user)
}catch(e){
console.info("页面刷新了")
}
})
});