-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathone.js
55 lines (52 loc) · 1.73 KB
/
one.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
54
55
const ws = require("nodejs-websocket");
const Time = require("./utils/formatTimes");
// 大群聊天
var user = []
var server = ws.createServer(function (conn) {
let name = conn.headers['name-userinfo'];
let openId = conn.headers['openid-userinfo'];
let avatarUrl = conn.headers['avatarurl-userinfo'];
user.push({ name, openId, avatarUrl });
// 加入房间
broadcast({
name: name,
openid: openId,
avatarurl: avatarUrl
}, { type: 'add', content: '加入房间' });
conn.on("text", function (data) {
let dataParse = JSON.parse(data)
if (dataParse.type == 'heart') { conn.sendText(JSON.stringify({ ...dataParse, user })) }
else {
broadcast({
name: conn.headers['name-userinfo'],
openid: conn.headers['openid-userinfo'],
avatarurl: conn.headers['avatarurl-userinfo']
}, dataParse)
}
})
conn.on("error", function (err) {
user.map((value, key) => {
if (conn.headers['openid-userinfo'] == value.openId) { user.splice(key, 1) }
});
console.log(err)
})
conn.on("close", function (code, reason) {
user.map((value, key) => {
if (conn.headers['openid-userinfo'] == value.openId) { user.splice(key, 1) }
});
broadcast({
name: name,
openid: openId,
avatarurl: avatarUrl
}, { type: 'add', content: '离开群聊' });
})
}).listen(8080, function () {
console.log('the ws port(8080) running');
});
function broadcast(user, msg) {
let data = { ...user, ...msg }
console.log(data)
server.connections.forEach(function (conn) {
conn.sendText(JSON.stringify(data))
})
}