-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathsocketServer.js
More file actions
41 lines (35 loc) · 1.14 KB
/
socketServer.js
File metadata and controls
41 lines (35 loc) · 1.14 KB
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
var socketServer = require('ws').Server;
var wss = new socketServer({
port: 8080
});
// var app = require('express')();
// var server = require('http').Server(app);
// var wss = new socketServer({server: server, port: 8080});
wss.on('connection', function (client) {
client.on('message', function (_message) {
var _messageObj = JSON.parse(_message);
//status = 1 表示正常聊天
_messageObj.status = 1;
this.message = _messageObj;
//把客户端的消息广播给所有在线的用户
wss.broadcast(_messageObj);
});
// 退出聊天
client.on('close', function() {
try{
this.message = this.message || {};
// status = 0 表示退出聊天
this.message.status = 0;
//把客户端的消息广播给所有在线的用户
wss.broadcast(this.message);
}catch(e){
console.log('刷新页面了');
}
});
});
//定义广播方法
wss.broadcast = function broadcast(_messageObj) {
wss.clients.forEach(function(client) {
client.send(JSON.stringify(_messageObj))
});
};