-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
341 lines (320 loc) · 9.09 KB
/
Copy pathutils.js
File metadata and controls
341 lines (320 loc) · 9.09 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
const {
saveToRedis,
removeFromRedis,
setAsSet,
delFromSet
} = require("../libs/common");
// 建立了新的socket连接后,处理用户连接
function handleConnect(socket, users, params) {
if (params.uid > 0 && params.type.length > 0) {
for (let i = 0; i < users.length; i++) {
const user = users[i];
// 单点登录,登录以前先进行删除的操作
if (user.uid === params.uid) {
// 更换 topic
if (user.topic !== params.topic) {
if (user.topic) {
socket.leave(user.topic, () => {
});
}
if (params.topic) {
socket.join(params.topic, () => {// 把该token加入对应的 '房间'
// console.log(socket.rooms);
});
}
user.topic = params.topic
}
if (params.type === 'customer' || params.type === 'rider' || user.socketIds[0] === socket.id) {
if (params.type === 'merchant') {
var clientIo = merchantIo;
} else {
var clientIo = params.type === 'rider' ? riderIo : customerIo;
}
clientIo.sockets[user.socketIds[0]].leave(user.topic);
users.splice(i, 1);
}
}
if (user.token === params.fcm_token) {
if (user.uid !== params.uid) {
user.uid = params.uid;
}
if (user.socketIds.indexOf(socket.id) === -1) {
user.socketIds.push(socket.id);
}
return;
}
}
let data = {
uid: params.uid,
token: params.fcm_token,
socketIds: [socket.id],
client: params.client,
type: params.type, // 保存fcm token到数据库时使用拼接key
topic: params.topic
}
users.push(data);
if (params.type === 'rider') {
setAsSet('online', params.uid);
}
// 第一次登录
if (params.topic) {
socket.join(params.topic, () => {// 把该token加入对应的 '房间'
// console.log(socket.rooms);
})
}
// 商家端
// 骑手端
// 商家应用端的 房间 -> app pos pad 以便平台可以单独对每个端做一个推送消息
addToClientRoom(socket, params);
removeFromRedis(data);
// debug 查看当前链接用户
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
}
}
function addToClientRoom(socket, params) {
let room_name = '';
let rooms = [];
if (params.type === 'customer' || params.type === 'rider') {
// 1. openfood_customer
// 2. openfood_rider
room_name = 'openfood_' + params.type; // 用户端/骑手端所有的token 对应房间名 -> 用于平台推送
} else if (params.type === 'merchant') {
if (params.client) {
// 1. openfood_merchant_app
// 2. openfood_merchant_pos
// 3. openfood_merchant_pad
room_name = 'openfood_merchant_' + params.client
if (params.client === 'pos') {
// NOTE: 需要给pos单独推送的话,需要单独的一个房间 如:pos_1_base_notification
let topic_arr = params.topic.split('_')
topic_arr[0] = params.client
let client_room = topic_arr.join('_')
rooms.push(client_room)
socket.join(client_room, () => {
// console.log(socket.rooms);
});
}
}
}
if (room_name !== '') {
rooms.push(room_name)
socket.join(room_name, () => {
// console.log(socket.rooms);
})
}
id_topic[socket.id] = {
rooms
}
}
/**
* 处理用户断开连接
*
* @param {*} users
* @param {*} users
*/
function handleDisConnect(socket, users, id_topic, params) {
var topic = '';
for (let i = 0; i < users.length; i++) {
const user = users[i];
let sidIdx = user.socketIds.indexOf(socket.id);
if (sidIdx > -1) {
topic = user.topic;
// 如果参数type 的类型是 disconnect,说明是异常断开,把改fcm_token保存至redis,以备发送fcm token激活该设备的连接
if (params.action === 'disconnect') {
// user 就是需要找的用户
saveToRedis(user);
}
if (user.type === 'rider') {
delFromSet('online', user.uid);
}
user.socketIds.splice(sidIdx, 1);
if (user.socketIds.length === 0) {
users.splice(i, 1);
break;
}
}
}
// 从对应的房间删除
if (typeof id_topic[socket.id] !== 'undefined') {
let rooms = id_topic[socket.id].rooms
rooms.forEach(room => {
socket.leave(room, () => { })
});
}
if (topic) {
socket.leave(topic, () => { });
}
// debug 查看当前链接用户
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
return;
}
function forDebugInfo() {
for_show.customer = [];
for_show.rider = [];
for_show.merchant = [];
for_show.h5 = [];
for_show.platform = [];
// 处理 customer
customer.forEach((item) => {
let key = item.type + '_' + item.uid + '_' + item.client + ' ===> ' + item.socketIds[0];
for_show.customer.push(key);
});
rider.forEach((item) => {
let key = item.type + '_' + item.uid + '_' + item.client + ' ===> ' + item.socketIds[0];
for_show.rider.push(key);
});
merchant.forEach((item) => {
let key = item.type + '_' + item.uid + '_' + item.client + ' ===> ' + item.socketIds[0];
if (item.topic) {
key = key + ' ======> topic : ' + item.topic;
}
if (item.client === 'pos' && typeof printer_sid[item.socketIds[0]] !== 'undefined') {
key = key + ' ======> deviceid : ' + printer_sid[item.socketIds[0]];
}
for_show.merchant.push(key);
});
h5.forEach((item) => {
let key = 'person connect: ' + item.client + '_' + item.uid + ' ===> ' + item.socketIds[0];
for_show.h5.push(key);
});
for (let i in h5_topic) {
let key = 'table topic: ' + i + ' : ' + h5_topic[i].join(' <==> ')
for_show.h5.push(key);
}
platform.forEach((item) => {
let key = 'platform_' + item.uid + ' ===> ' + item.socketIds.join(' <======> ');
for_show.platform.push(key);
});
show_id.forEach((id) => {
showIo.to(id).send(JSON.stringify(for_show));
});
// 处理 rider
// 处理 merchant
}
// 处理 h5 的链接
function handleH5Connect(socket, users, params, type) {
if (params.uid.length > 0 && params.type.length > 0) {
if (type === 'person') {
for (let i = 0; i < users.length; i++) {
const user = users[i];
if (user.uid === params.uid) {
user.socketIds = [];
user.socketIds.push(socket.id);
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
return;
}
}
}
// 如果是堂吃的直接加入 桌台的topic, 清台的时候需要清空topic
if (type === 'table') {
var topic = 'h5_' + params.uid + '_base_notification';
socket.join(topic, () => {// 把该token加入对应的 '房间'
// console.log(socket.rooms);
});
if (!h5_topic[topic]) {
h5_topic[topic] = [];
}
if (h5_topic.indexOf(socket.id) === -1) {
h5_topic[topic].push(socket.id);
}
}
if (type === 'person') {
let data = {
uid: params.uid,
socketIds: [socket.id],
client: params.client
}
users.push(data);
}
// debug 查看当前链接用户
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
}
}
// 处理h5断开链接
function handleH5DisConnect(socket, users, h5_topic) {
for (let i = 0; i < users.length; i++) {
const user = users[i];
let sidIdx = user.socketIds.indexOf(socket.id);
if (sidIdx > -1) {
user.socketIds.splice(sidIdx, 1);
if (user.socketIds.length === 0) {
users.splice(i, 1);
break;
}
}
}
for (let i in h5_topic) {
let index = h5_topic[i].indexOf(socket.id);
if (index !== -1) {
h5_topic[i].splice(index, 1);
socket.leave(i);
}
if (h5_topic[i].length === 0) {
delete h5_topic[i];
}
}
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
}
// 处理 platform 的连接
function handlePlatformConnect(users, params) {
const { uid, type, socketId } = params;
for (let i = 0; i < users.length; i++) {
const user = users[i];
if (user.uid === uid) {
if (user.socketIds.indexOf(socketId) === -1) {
user.socketIds.push(socketId);
}
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
return;
}
}
users.push({
uid,
type,
socketIds: [socketId]
});
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
}
/**
* 关闭页面后,删除对应的socketId
*/
function handlePlatformDisconnect(users, socketId) {
for (let i = 0; i < users.length; i++) {
const user = users[i];
let sidIdx = user.socketIds.indexOf(socketId);
if (sidIdx > -1) {
user.socketIds.splice(sidIdx, 1);
if (user.socketIds.length === 0) {
users.splice(i, 1);
break;
}
}
}
if (process.env.NODE_ENV === 'development') {
forDebugInfo();
}
return;
}
module.exports = {
handleConnect,
handleDisConnect,
handleH5Connect,
handleH5DisConnect,
forDebugInfo,
handlePlatformConnect,
handlePlatformDisconnect
};