forked from leesipeng/chat-wall-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.js
More file actions
145 lines (131 loc) · 5.62 KB
/
Copy pathwebsocket.js
File metadata and controls
145 lines (131 loc) · 5.62 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const WebSocket = require('ws');
const xssFilters = require('xss-filters');
const Mock = require('mockjs')
function websocket(server) {
const wss = new WebSocket.Server({ server });
let timer = null;
if(timer) clearInterval(timer);
/*
* 监听socket连接
*/
wss.on('connection', function connection(socket, req) {
//监听用户发布聊天内容
console.log("有新客户端连接!");
// 构造客户端对象
var newclient = {
socket: socket,
nickName: false
};
/**
* 小程序首次连接 url携带appInfo 标识当前用户
* 截取客户端发送的url 获取用户信息 解析获取的appInfo对象
* 如果url不包含参数 则是pc端
*/
let index = req.url.indexOf('=');
if (index != -1) {
let encodeStr = req.url.substr(index + 1)
let appInfoStr = decodeURIComponent(decodeURIComponent(encodeStr))
let appInfo = (appInfoStr && JSON.parse(appInfoStr))
newclient.nickName = appInfo.nickName
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
console.log('第一次登录')
// 当前用户登录 广播一条消息
client.send(JSON.stringify({
type: 'LOGIN',
userId: appInfo.userId,//这条消息的拥有者是my
nickName: appInfo.nickName,
avatarUrl: appInfo.avatarUrl,
timestamp: Date.now(),
timeStr: getMyTime(Date.now()),
content: appInfo.nickName + ' 加入群聊~',
}));
}
});
// 监听客户端关闭事件 广播消息 --只监听小程序连接
socket.on('close', function close() {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
type: 'LOGOUT',
content: newclient.nickName + ' 默默的就离开了~',
timeStr: getMyTime(Date.now()),
nickName: newclient.nickName,
timestamp: Date.now(),
}));
}
console.log(newclient.nickName + "离开聊天。");
});
});
} else {
newclient.nickName = '大屏幕';
}
// 监听消息事件
socket.on('message', function incoming(data) {
console.log('dataStr')
console.log(data)
var data = data && JSON.parse(data)
wss.clients.forEach(function each(client) {
if (client !== socket && client.readyState === WebSocket.OPEN) {
// 文本消息
if (data.type === 'text') {
let sendObj = {
userId: data.userId,//这条消息的拥有者是my
nickName: data.nickName,
avatarUrl: data.avatarUrl,
timestamp: Date.now(),
timeStr: getMyTime(Date.now()),
content: data.content,
type: data.type
};
client.send(JSON.stringify(sendObj));
} else if (data.type === 'image') {
let sendObj = {
userId: data.userId,//这条消息的拥有者是my
nickName: data.nickName,
avatarUrl: data.avatarUrl,
timestamp: Date.now(),
timeStr: getMyTime(Date.now()),
content: data.content,
largePic: data.largePic,
type: data.type
};
client.send(JSON.stringify(sendObj));
}
}
});
});
let testContent = [
Mock.Random.csentence(1, 30),// 中文1-30
Mock.Random.paragraph(1, 7), // 英文句子
Mock.Random.cparagraph(1, 5), // 中文文本
Mock.Random.sentence(1, 10), // 英文单词
Mock.Random.float(),
]
/**
* 测试用 定时广播消息
*/
timer = setInterval(() => {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
userId: Mock.Random.natural(111, 999),//这条消息的拥有者是my
nickName: testContent[Mock.Random.natural(0, testContent.length)],
avatarUrl: Mock.Random.image(),
timestamp: Date.now(),
timeStr: getMyTime(Date.now()),
content: testContent[Mock.Random.natural(0, testContent.length)],
type: 'text'
}));
}
});
}, 5000)
});
// 时间转换函数
function getMyTime(timestamp) {
let date = new Date(timestamp);
let hours = date.getHours(), minutes = date.getMinutes();
return `${hours >= 10 ? hours : '0' + hours}:${minutes > 10 ? minutes : '0' + minutes}`
};
}
module.exports = websocket;