mm_os 4.2.4 → 4.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/README_EN.md +2 -2
- package/adapter/adapter.js +105 -52
- package/adapter/custom_persistence.js +32 -31
- package/adapter/mqtt.js +59 -79
- package/adapter/socket.js +188 -58
- package/adapter/web.js +92 -68
- package/adapter/websocket.js +43 -146
- package/com/api/drive.js +732 -708
- package/com/api/index.js +196 -198
- package/com/api/script.tpl.js +24 -24
- package/com/cmd/config.tpl.json +48 -122
- package/com/cmd/drive.js +176 -1548
- package/com/cmd/index.js +287 -1066
- package/com/cmd/job/config.json +66 -0
- package/com/cmd/job/index.js +241 -0
- package/com/cmd/job/test.js +188 -0
- package/com/cmd/script.tpl.js +12 -33
- package/com/db/drive.js +2206 -2019
- package/com/db/index.js +241 -242
- package/com/event/config.tpl.json +3 -1
- package/com/event/drive.js +3 -1
- package/com/event/index.js +73 -46
- package/com/event/script.tpl.js +22 -22
- package/com/mqtt/config.tpl.json +16 -8
- package/com/mqtt/drive.js +696 -676
- package/com/mqtt/index.js +1012 -822
- package/com/mqtt/index.test.js +503 -0
- package/com/mqtt/mm_mqtt.js +446 -420
- package/com/mqtt/script.tpl.js +3 -7
- package/com/nav/drive.js +7 -7
- package/com/nav/index.js +228 -231
- package/com/param/index.js +19 -17
- package/com/pendant/index.js +467 -441
- package/com/socket/drive.js +679 -651
- package/com/socket/index.js +94 -48
- package/com/sql/drive.js +1283 -1259
- package/com/sql/index.js +152 -150
- package/com/static/drive.js +1 -1
- package/com/static/index.js +232 -226
- package/com/static/script.tpl.js +25 -25
- package/com/task/drive.js +428 -405
- package/com/task/index.js +154 -148
- package/com/task/script.tpl.js +37 -34
- package/com/template/drive.js +1 -1
- package/com/template/index.js +141 -138
- package/com.js +87 -6
- package/common/middleware/web_after/index.js +5 -11
- package/common/middleware/web_base/index.js +1 -1
- package/common/middleware/web_cors/index.js +1 -1
- package/common/middleware/web_error/index.js +131 -119
- package/common/middleware/web_error/middleware.json +2 -1
- package/common/middleware/web_logger/index.js +1 -1
- package/common/middleware/web_main/index.js +2 -0
- package/common/middleware/web_static/index.js +2 -2
- package/common/middleware/web_waf/index.js +1 -1
- package/core/app/config.tpl.json +4 -4
- package/core/app/index.js +710 -534
- package/core/middleware/index.js +1 -9
- package/core/mod/config.tpl.json +1 -1
- package/core/model/config.tpl.json +1 -1
- package/core/plugin/config.tpl.json +15 -3
- package/core/plugin/index.js +31 -0
- package/core/pusher/index.js +158 -161
- package/core/scene/loop.js +1306 -1255
- package/core/store/index.js +1794 -1755
- package/core/store/sql.js +1488 -1464
- package/core/zone/bat/index.js +744 -728
- package/core/zone/config.tpl.json +3 -3
- package/core/zone/index.js +655 -616
- package/os.js +0 -4
- package/package.json +16 -16
- package/server.js +583 -427
- package/com/cmd/nlp.js +0 -525
- package/core/app/index copy.js +0 -611
- package/software.js +0 -194
- package/tcp.js +0 -279
package/README.md
CHANGED
package/README_EN.md
CHANGED
|
@@ -110,7 +110,7 @@ const server = new MM_os({
|
|
|
110
110
|
async function startServer() {
|
|
111
111
|
await server.init();
|
|
112
112
|
await server.start();
|
|
113
|
-
console.log('
|
|
113
|
+
console.log('服务启动成功');
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
startServer();
|
|
@@ -427,7 +427,7 @@ MM_OS supports multiple configuration options:
|
|
|
427
427
|
|
|
428
428
|
```json
|
|
429
429
|
{
|
|
430
|
-
"name": "
|
|
430
|
+
"name": "mm_os",
|
|
431
431
|
"title": "Server System",
|
|
432
432
|
"log": {
|
|
433
433
|
"level": "debug",
|
package/adapter/adapter.js
CHANGED
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
* 适配器基类
|
|
3
3
|
* 定义了基础的网络通信接口
|
|
4
4
|
*/
|
|
5
|
-
const { Mod } = require('mm_machine');
|
|
6
5
|
const { idGen } = require('../ulits/id_gen.js');
|
|
7
6
|
|
|
8
|
-
class Adapter
|
|
7
|
+
class Adapter {
|
|
9
8
|
static config = {
|
|
10
9
|
name: 'adapter',
|
|
11
10
|
host: '0.0.0.0',
|
|
@@ -18,17 +17,68 @@ class Adapter extends Mod {
|
|
|
18
17
|
* @param {object} parent 父对象
|
|
19
18
|
*/
|
|
20
19
|
constructor(config, parent) {
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
if (parent) {
|
|
21
|
+
this.getParent = function () {
|
|
22
|
+
return parent;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
23
25
|
// 客户端连接列表
|
|
24
26
|
this.clients = new Map();
|
|
25
27
|
// 中间件数组
|
|
26
28
|
this.mw_list = [];
|
|
27
29
|
// 回调函数映射表,使用消息ID作为键
|
|
28
30
|
this.callback = new Map();
|
|
31
|
+
|
|
32
|
+
this.setConfig(config);
|
|
29
33
|
}
|
|
30
34
|
}
|
|
31
35
|
|
|
36
|
+
/**
|
|
37
|
+
* 设置配置参数
|
|
38
|
+
* @param {object} config 配置参数
|
|
39
|
+
*/
|
|
40
|
+
Adapter.prototype.setConfig = function (config) {
|
|
41
|
+
this.config = { ...Adapter.config, ...config };
|
|
42
|
+
this._preset();
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
Adapter.prototype._preset = function () {
|
|
46
|
+
// 默认预设,子类可以重写
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
Adapter.prototype.getParent = function () {
|
|
50
|
+
return null;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 初始化适配器
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
Adapter.prototype._init = function (...args) {
|
|
58
|
+
return this;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 初始化适配器
|
|
63
|
+
* @param {...*} args 初始化参数
|
|
64
|
+
* @returns {object} this
|
|
65
|
+
*/
|
|
66
|
+
Adapter.prototype.init = function (...args) {
|
|
67
|
+
// 默认初始化,子类可以重写
|
|
68
|
+
return this._init(...args);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 记录日志
|
|
73
|
+
* @param {string} level 日志级别,如 'debug', 'info', 'warn', 'error'
|
|
74
|
+
* @param {string} message 日志消息
|
|
75
|
+
* @param {...*} args 可选,日志消息的参数
|
|
76
|
+
*/
|
|
77
|
+
Adapter.prototype.log = function (level, message, ...args) {
|
|
78
|
+
let logger = this.getParent().getLogger();
|
|
79
|
+
logger[level](`[${this.constructor.name}] ${message}`, ...args);
|
|
80
|
+
};
|
|
81
|
+
|
|
32
82
|
/**
|
|
33
83
|
* 生成ID
|
|
34
84
|
* @param {string} type ID类型,可选值:'client'(客户端ID)或'msg'(消息ID)
|
|
@@ -76,24 +126,24 @@ Adapter.prototype._setupMw = function () {
|
|
|
76
126
|
|
|
77
127
|
/**
|
|
78
128
|
* 发送消息(需要子类实现)
|
|
79
|
-
* @param {string}
|
|
129
|
+
* @param {string} clientid 客户端ID
|
|
80
130
|
* @param {string} message 消息内容
|
|
81
131
|
* @returns {void}
|
|
82
132
|
*/
|
|
83
|
-
Adapter.prototype._send = function (
|
|
84
|
-
this.log('debug', '发送消息', {
|
|
133
|
+
Adapter.prototype._send = function (clientid, message) {
|
|
134
|
+
this.log('debug', '发送消息', { clientid, message });
|
|
85
135
|
throw new Error('子类必须实现_send方法');
|
|
86
136
|
};
|
|
87
137
|
|
|
88
138
|
/**
|
|
89
139
|
* 向指定客户端发送消息(JSON 2.0格式)
|
|
90
|
-
* @param {string}
|
|
140
|
+
* @param {string} clientid 客户端ID
|
|
91
141
|
* @param {string} method 方法名
|
|
92
142
|
* @param {object} params 参数对象
|
|
93
143
|
* @param {Function} cb 可选,接收客户端回复的回调函数
|
|
94
144
|
* @returns {void}
|
|
95
145
|
*/
|
|
96
|
-
Adapter.prototype.send = function (
|
|
146
|
+
Adapter.prototype.send = function (clientid, method, params = {}, cb = null) {
|
|
97
147
|
// 生成唯一消息ID
|
|
98
148
|
let message_id = this.genMsgId();
|
|
99
149
|
|
|
@@ -112,20 +162,20 @@ Adapter.prototype.send = function (client_id, method, params = {}, cb = null) {
|
|
|
112
162
|
this.callback.set(message_id, cb);
|
|
113
163
|
}
|
|
114
164
|
|
|
115
|
-
return this._send(
|
|
165
|
+
return this._send(clientid, message);
|
|
116
166
|
};
|
|
117
167
|
|
|
118
168
|
/**
|
|
119
169
|
* 异步发送消息
|
|
120
|
-
* @param {string}
|
|
170
|
+
* @param {string} clientid 客户端ID
|
|
121
171
|
* @param {string} method 方法名
|
|
122
172
|
* @param {object} params 参数对象
|
|
123
173
|
* @returns {Promise} 异步操作的结果
|
|
124
174
|
*/
|
|
125
|
-
Adapter.prototype.sendAsync = function (
|
|
175
|
+
Adapter.prototype.sendAsync = function (clientid, method, params = {}) {
|
|
126
176
|
return new Promise((resolve, reject) => {
|
|
127
177
|
// 发送消息
|
|
128
|
-
this.send(
|
|
178
|
+
this.send(clientid, method, params, (msg, error) => {
|
|
129
179
|
if (error) {
|
|
130
180
|
reject(error);
|
|
131
181
|
} else {
|
|
@@ -185,10 +235,10 @@ Adapter.prototype._autoReply = function (ctx) {
|
|
|
185
235
|
result: ctx.body,
|
|
186
236
|
error: null
|
|
187
237
|
};
|
|
188
|
-
this._send(ctx.
|
|
238
|
+
this._send(ctx.clientid, JSON.stringify(response));
|
|
189
239
|
} else {
|
|
190
240
|
// 非JSON格式消息,直接发送
|
|
191
|
-
this._send(ctx.
|
|
241
|
+
this._send(ctx.clientid, ctx.body.toString());
|
|
192
242
|
}
|
|
193
243
|
};
|
|
194
244
|
|
|
@@ -207,44 +257,42 @@ Adapter.prototype._runMwError = function (ctx, error) {
|
|
|
207
257
|
result: null,
|
|
208
258
|
error: { message: error.message }
|
|
209
259
|
};
|
|
210
|
-
this._send(ctx.
|
|
260
|
+
this._send(ctx.clientid, JSON.stringify(response));
|
|
211
261
|
}
|
|
212
262
|
};
|
|
213
263
|
|
|
214
264
|
/**
|
|
215
265
|
* 消息接收处理
|
|
216
|
-
* @param {string}
|
|
266
|
+
* @param {string} clientid 客户端ID
|
|
217
267
|
* @param {object} socket 客户端socket对象
|
|
218
268
|
* @param {string} data 接收的消息
|
|
219
269
|
*/
|
|
220
|
-
Adapter.prototype._onMsg = function (
|
|
270
|
+
Adapter.prototype._onMsg = function (clientid, socket, data) {
|
|
221
271
|
// 检查参数是否有效
|
|
222
|
-
if (!this._check(
|
|
272
|
+
if (!this._check(clientid, data)) return;
|
|
223
273
|
|
|
224
274
|
// 处理消息数据
|
|
225
|
-
let
|
|
275
|
+
let clientid_str = this._getClientIdStr(clientid);
|
|
226
276
|
let message = this._parseStr(data);
|
|
227
277
|
|
|
228
|
-
this.log('debug', `客户端 ${client_id_str} 发送数据: ${message}`);
|
|
229
|
-
|
|
230
278
|
// 解析消息并处理
|
|
231
|
-
let msg = this._parseMessage(
|
|
279
|
+
let msg = this._parseMessage(clientid_str, message);
|
|
232
280
|
|
|
233
281
|
// 创建上下文对象并执行中间件
|
|
234
|
-
let ctx = this._createContext(
|
|
282
|
+
let ctx = this._createContext(clientid_str, socket, msg);
|
|
235
283
|
this._composeMw(ctx);
|
|
236
284
|
};
|
|
237
285
|
|
|
238
286
|
/**
|
|
239
287
|
* 验证参数有效性
|
|
240
|
-
* @param {string}
|
|
288
|
+
* @param {string} clientid 客户端ID
|
|
241
289
|
* @param {string} data 消息数据
|
|
242
290
|
* @returns {boolean} 参数是否有效
|
|
243
291
|
* @private
|
|
244
292
|
*/
|
|
245
|
-
Adapter.prototype._check = function (
|
|
246
|
-
if (!
|
|
247
|
-
this.log('error', '客户端ID无效:',
|
|
293
|
+
Adapter.prototype._check = function (clientid, data) {
|
|
294
|
+
if (!clientid) {
|
|
295
|
+
this.log('error', '客户端ID无效:', clientid);
|
|
248
296
|
return false;
|
|
249
297
|
}
|
|
250
298
|
|
|
@@ -258,12 +306,12 @@ Adapter.prototype._check = function (client_id, data) {
|
|
|
258
306
|
|
|
259
307
|
/**
|
|
260
308
|
* 获取客户端ID字符串
|
|
261
|
-
* @param {string|object}
|
|
309
|
+
* @param {string|object} clientid 客户端ID
|
|
262
310
|
* @returns {string} 客户端ID字符串
|
|
263
311
|
* @private
|
|
264
312
|
*/
|
|
265
|
-
Adapter.prototype._getClientIdStr = function (
|
|
266
|
-
return typeof
|
|
313
|
+
Adapter.prototype._getClientIdStr = function (clientid) {
|
|
314
|
+
return typeof clientid === 'object' ? clientid.id || 'unknown' : clientid;
|
|
267
315
|
};
|
|
268
316
|
|
|
269
317
|
/**
|
|
@@ -284,21 +332,21 @@ Adapter.prototype._parseStr = function (data) {
|
|
|
284
332
|
|
|
285
333
|
/**
|
|
286
334
|
* 解析消息内容
|
|
287
|
-
* @param {string}
|
|
335
|
+
* @param {string} clientid_str 客户端ID字符串
|
|
288
336
|
* @param {string} message 消息内容
|
|
289
337
|
* @returns {any} 解析后的消息
|
|
290
338
|
* @private
|
|
291
339
|
*/
|
|
292
|
-
Adapter.prototype._parseMessage = function (
|
|
340
|
+
Adapter.prototype._parseMessage = function (clientid_str, message) {
|
|
293
341
|
try {
|
|
294
342
|
let msg = JSON.parse(message);
|
|
295
343
|
// 检查是否是回复消息(具有id属性)
|
|
296
344
|
if (typeof msg === 'object' && msg.id) {
|
|
297
|
-
this._onMessage(
|
|
345
|
+
this._onMessage(clientid_str, msg);
|
|
298
346
|
}
|
|
299
347
|
return msg;
|
|
300
348
|
} catch (error) {
|
|
301
|
-
this.log('warn', `客户端 ${
|
|
349
|
+
this.log('warn', `客户端 ${clientid_str} 发送的消息不是有效的JSON格式: ${message}`, error);
|
|
302
350
|
// 如果不是JSON格式,仍传递给中间件处理
|
|
303
351
|
return message;
|
|
304
352
|
}
|
|
@@ -306,15 +354,15 @@ Adapter.prototype._parseMessage = function (client_id_str, message) {
|
|
|
306
354
|
|
|
307
355
|
/**
|
|
308
356
|
* 创建上下文对象
|
|
309
|
-
* @param {string}
|
|
357
|
+
* @param {string} clientid_str 客户端ID字符串
|
|
310
358
|
* @param {object} socket 客户端socket对象
|
|
311
359
|
* @param {any} msg 消息内容
|
|
312
360
|
* @returns {object} 上下文对象
|
|
313
361
|
* @private
|
|
314
362
|
*/
|
|
315
|
-
Adapter.prototype._createContext = function (
|
|
363
|
+
Adapter.prototype._createContext = function (clientid_str, socket, msg) {
|
|
316
364
|
return {
|
|
317
|
-
|
|
365
|
+
clientid: clientid_str,
|
|
318
366
|
socket: socket,
|
|
319
367
|
message: msg,
|
|
320
368
|
adapter: this,
|
|
@@ -322,23 +370,23 @@ Adapter.prototype._createContext = function (client_id_str, socket, msg) {
|
|
|
322
370
|
body: null,
|
|
323
371
|
// 发送消息给当前客户端
|
|
324
372
|
send: (method, params, cb) => {
|
|
325
|
-
this.send(
|
|
373
|
+
this.send(clientid_str, method, params, cb);
|
|
326
374
|
},
|
|
327
375
|
// 异步发送消息
|
|
328
376
|
sendAsync: (method, params = {}) => {
|
|
329
|
-
return this.sendAsync(
|
|
377
|
+
return this.sendAsync(clientid_str, method, params);
|
|
330
378
|
}
|
|
331
379
|
};
|
|
332
380
|
};
|
|
333
381
|
|
|
334
382
|
/**
|
|
335
383
|
* 消息接收处理
|
|
336
|
-
* @param {string}
|
|
384
|
+
* @param {string} clientid 客户端ID
|
|
337
385
|
* @param {object} msg 接收的消息
|
|
338
386
|
*/
|
|
339
|
-
Adapter.prototype._onMessage = function (
|
|
387
|
+
Adapter.prototype._onMessage = function (clientid, msg) {
|
|
340
388
|
// 查找对应的回调函数
|
|
341
|
-
this.log('debug', '收到消息', {
|
|
389
|
+
this.log('debug', '收到消息', { clientid });
|
|
342
390
|
let callback = this.callback.get(msg.id);
|
|
343
391
|
if (callback) {
|
|
344
392
|
// 执行回调函数并移除
|
|
@@ -352,26 +400,25 @@ Adapter.prototype._onMessage = function (client_id, msg) {
|
|
|
352
400
|
|
|
353
401
|
/**
|
|
354
402
|
* 连接关闭处理
|
|
355
|
-
* @param {string}
|
|
403
|
+
* @param {string} clientid 客户端ID
|
|
356
404
|
*/
|
|
357
|
-
Adapter.prototype._onClose = function (
|
|
405
|
+
Adapter.prototype._onClose = function (clientid) {
|
|
358
406
|
// 移除客户端连接
|
|
359
|
-
this.clients.delete(
|
|
360
|
-
this.log('info', `客户端断开连接: ${
|
|
407
|
+
this.clients.delete(clientid);
|
|
408
|
+
this.log('info', `客户端断开连接: ${clientid}`);
|
|
361
409
|
};
|
|
362
410
|
|
|
363
411
|
/**
|
|
364
412
|
* 错误处理
|
|
365
|
-
* @param {string}
|
|
413
|
+
* @param {string} clientid 客户端ID
|
|
366
414
|
* @param {Error} error 错误对象
|
|
367
415
|
*/
|
|
368
|
-
Adapter.prototype._onError = function (
|
|
369
|
-
this.log('error', `客户端 ${
|
|
416
|
+
Adapter.prototype._onError = function (clientid, error) {
|
|
417
|
+
this.log('error', `客户端 ${clientid} 发生错误:`, error);
|
|
370
418
|
// 移除客户端连接
|
|
371
|
-
this.clients.delete(
|
|
419
|
+
this.clients.delete(clientid);
|
|
372
420
|
};
|
|
373
421
|
|
|
374
|
-
|
|
375
422
|
/**
|
|
376
423
|
* 停止Adapter服务器(子类可以重写以提供特定实现)
|
|
377
424
|
*/
|
|
@@ -428,4 +475,10 @@ Adapter.prototype.bcast = function (message, ids = null) {
|
|
|
428
475
|
}
|
|
429
476
|
};
|
|
430
477
|
|
|
478
|
+
/**
|
|
479
|
+
* 启动Adapter服务器
|
|
480
|
+
*/
|
|
481
|
+
Adapter.prototype.start = function () {
|
|
482
|
+
};
|
|
483
|
+
|
|
431
484
|
exports.Adapter = Adapter;
|
|
@@ -69,8 +69,9 @@ Persist.prototype._getKeysByPattern = function(pattern) {
|
|
|
69
69
|
* @param {Function} cb - 回调函数
|
|
70
70
|
*/
|
|
71
71
|
Persist.prototype.addSub = function(client, sub, cb) {
|
|
72
|
+
console.log('addSub', client.id);
|
|
72
73
|
try {
|
|
73
|
-
if (!client || !client.
|
|
74
|
+
if (!client || !client.id) {
|
|
74
75
|
throw new Error('无效的客户端对象');
|
|
75
76
|
}
|
|
76
77
|
|
|
@@ -78,7 +79,7 @@ Persist.prototype.addSub = function(client, sub, cb) {
|
|
|
78
79
|
throw new Error('无效的订阅对象');
|
|
79
80
|
}
|
|
80
81
|
|
|
81
|
-
let key = this._getCacheKey(`subscription:${client.
|
|
82
|
+
let key = this._getCacheKey(`subscription:${client.id}:${sub.topic}`);
|
|
82
83
|
this._cache.set(key, sub, this.config.ttl);
|
|
83
84
|
|
|
84
85
|
if (cb) cb(null);
|
|
@@ -95,7 +96,7 @@ Persist.prototype.addSub = function(client, sub, cb) {
|
|
|
95
96
|
*/
|
|
96
97
|
Persist.prototype.removeSub = function(client, sub, cb) {
|
|
97
98
|
try {
|
|
98
|
-
if (!client || !client.
|
|
99
|
+
if (!client || !client.id) {
|
|
99
100
|
throw new Error('无效的客户端对象');
|
|
100
101
|
}
|
|
101
102
|
|
|
@@ -103,7 +104,7 @@ Persist.prototype.removeSub = function(client, sub, cb) {
|
|
|
103
104
|
throw new Error('无效的订阅对象');
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
let key = this._getCacheKey(`subscription:${client.
|
|
107
|
+
let key = this._getCacheKey(`subscription:${client.id}:${sub.topic}`);
|
|
107
108
|
this._cache.delete(key);
|
|
108
109
|
|
|
109
110
|
if (cb) cb(null);
|
|
@@ -119,11 +120,11 @@ Persist.prototype.removeSub = function(client, sub, cb) {
|
|
|
119
120
|
*/
|
|
120
121
|
Persist.prototype.removeSubs = function(client, cb) {
|
|
121
122
|
try {
|
|
122
|
-
if (!client || !client.
|
|
123
|
+
if (!client || !client.id) {
|
|
123
124
|
throw new Error('无效的客户端对象');
|
|
124
125
|
}
|
|
125
126
|
|
|
126
|
-
let pat = `subscription:${client.
|
|
127
|
+
let pat = `subscription:${client.id}:*`;
|
|
127
128
|
let keys = this._getKeysByPattern(pat);
|
|
128
129
|
|
|
129
130
|
// 同步执行所有删除操作
|
|
@@ -144,11 +145,11 @@ Persist.prototype.removeSubs = function(client, cb) {
|
|
|
144
145
|
*/
|
|
145
146
|
Persist.prototype.getSubs = function(client, cb) {
|
|
146
147
|
try {
|
|
147
|
-
if (!client || !client.
|
|
148
|
+
if (!client || !client.id) {
|
|
148
149
|
throw new Error('无效的客户端对象');
|
|
149
150
|
}
|
|
150
151
|
|
|
151
|
-
let pattern = `subscription:${client.
|
|
152
|
+
let pattern = `subscription:${client.id}:*`;
|
|
152
153
|
let keys = this._getKeysByPattern(pattern);
|
|
153
154
|
|
|
154
155
|
// 同步执行所有获取操作
|
|
@@ -185,7 +186,7 @@ Persist.prototype.subsByTopic = function(topic, cb) {
|
|
|
185
186
|
let sub = this._cache.get(this._getCacheKey(key));
|
|
186
187
|
if (sub) {
|
|
187
188
|
return {
|
|
188
|
-
|
|
189
|
+
clientId: cid,
|
|
189
190
|
sub: sub
|
|
190
191
|
};
|
|
191
192
|
}
|
|
@@ -271,7 +272,7 @@ Persist.prototype.getRetained = function(topic, cb) {
|
|
|
271
272
|
*/
|
|
272
273
|
Persist.prototype.storePacket = function(client, packet, callback) {
|
|
273
274
|
try {
|
|
274
|
-
if (!client || !client.
|
|
275
|
+
if (!client || !client.id) {
|
|
275
276
|
throw new Error('无效的客户端对象');
|
|
276
277
|
}
|
|
277
278
|
|
|
@@ -279,7 +280,7 @@ Persist.prototype.storePacket = function(client, packet, callback) {
|
|
|
279
280
|
throw new Error('无效的消息包');
|
|
280
281
|
}
|
|
281
282
|
|
|
282
|
-
let key = this._getCacheKey(`packet:${client.
|
|
283
|
+
let key = this._getCacheKey(`packet:${client.id}:${packet.msgId}`);
|
|
283
284
|
this._cache.set(key, packet, this.config.ttl);
|
|
284
285
|
|
|
285
286
|
if (callback) callback(null);
|
|
@@ -296,7 +297,7 @@ Persist.prototype.storePacket = function(client, packet, callback) {
|
|
|
296
297
|
*/
|
|
297
298
|
Persist.prototype.removePacket = function(client, packet, callback) {
|
|
298
299
|
try {
|
|
299
|
-
if (!client || !client.
|
|
300
|
+
if (!client || !client.id) {
|
|
300
301
|
throw new Error('无效的客户端对象');
|
|
301
302
|
}
|
|
302
303
|
|
|
@@ -304,7 +305,7 @@ Persist.prototype.removePacket = function(client, packet, callback) {
|
|
|
304
305
|
throw new Error('无效的消息包');
|
|
305
306
|
}
|
|
306
307
|
|
|
307
|
-
let key = this._getCacheKey(`packet:${client.
|
|
308
|
+
let key = this._getCacheKey(`packet:${client.id}:${packet.msgId}`);
|
|
308
309
|
this._cache.delete(key);
|
|
309
310
|
|
|
310
311
|
if (callback) callback(null);
|
|
@@ -321,7 +322,7 @@ Persist.prototype.removePacket = function(client, packet, callback) {
|
|
|
321
322
|
*/
|
|
322
323
|
Persist.prototype.getPacket = function(client, packet, callback) {
|
|
323
324
|
try {
|
|
324
|
-
if (!client || !client.
|
|
325
|
+
if (!client || !client.id) {
|
|
325
326
|
throw new Error('无效的客户端对象');
|
|
326
327
|
}
|
|
327
328
|
|
|
@@ -329,7 +330,7 @@ Persist.prototype.getPacket = function(client, packet, callback) {
|
|
|
329
330
|
throw new Error('无效的消息包');
|
|
330
331
|
}
|
|
331
332
|
|
|
332
|
-
let key = this._getCacheKey(`packet:${client.
|
|
333
|
+
let key = this._getCacheKey(`packet:${client.id}:${packet.msgId}`);
|
|
333
334
|
|
|
334
335
|
if (callback) callback(null, this._cache.get(key));
|
|
335
336
|
} catch (error) {
|
|
@@ -344,11 +345,11 @@ Persist.prototype.getPacket = function(client, packet, callback) {
|
|
|
344
345
|
*/
|
|
345
346
|
Persist.prototype.getPackets = function(client, callback) {
|
|
346
347
|
try {
|
|
347
|
-
if (!client || !client.
|
|
348
|
+
if (!client || !client.id) {
|
|
348
349
|
throw new Error('无效的客户端对象');
|
|
349
350
|
}
|
|
350
351
|
|
|
351
|
-
let pattern = `packet:${client.
|
|
352
|
+
let pattern = `packet:${client.id}:*`;
|
|
352
353
|
let keys = this._getKeysByPattern(pattern);
|
|
353
354
|
|
|
354
355
|
// 同步执行所有获取操作
|
|
@@ -399,7 +400,7 @@ Persist.prototype.delOutdatedPacket = function(packet, callback) {
|
|
|
399
400
|
*/
|
|
400
401
|
Persist.prototype.storeWill = function(client, packet, callback) {
|
|
401
402
|
try {
|
|
402
|
-
if (!client || !client.
|
|
403
|
+
if (!client || !client.id) {
|
|
403
404
|
throw new Error('无效的客户端对象');
|
|
404
405
|
}
|
|
405
406
|
|
|
@@ -407,7 +408,7 @@ Persist.prototype.storeWill = function(client, packet, callback) {
|
|
|
407
408
|
throw new Error('无效的消息包');
|
|
408
409
|
}
|
|
409
410
|
|
|
410
|
-
let key = this._getCacheKey(`will:${client.
|
|
411
|
+
let key = this._getCacheKey(`will:${client.id}`);
|
|
411
412
|
this._cache.set(key, packet, this.config.ttl);
|
|
412
413
|
|
|
413
414
|
if (callback) callback(null);
|
|
@@ -423,11 +424,11 @@ Persist.prototype.storeWill = function(client, packet, callback) {
|
|
|
423
424
|
*/
|
|
424
425
|
Persist.prototype.removeWill = function(client, callback) {
|
|
425
426
|
try {
|
|
426
|
-
if (!client || !client.
|
|
427
|
+
if (!client || !client.id) {
|
|
427
428
|
throw new Error('无效的客户端对象');
|
|
428
429
|
}
|
|
429
430
|
|
|
430
|
-
let key = this._getCacheKey(`will:${client.
|
|
431
|
+
let key = this._getCacheKey(`will:${client.id}`);
|
|
431
432
|
this._cache.delete(key);
|
|
432
433
|
|
|
433
434
|
if (callback) callback(null);
|
|
@@ -443,11 +444,11 @@ Persist.prototype.removeWill = function(client, callback) {
|
|
|
443
444
|
*/
|
|
444
445
|
Persist.prototype.getWill = function(client, callback) {
|
|
445
446
|
try {
|
|
446
|
-
if (!client || !client.
|
|
447
|
+
if (!client || !client.id) {
|
|
447
448
|
throw new Error('无效的客户端对象');
|
|
448
449
|
}
|
|
449
450
|
|
|
450
|
-
let key = this._getCacheKey(`will:${client.
|
|
451
|
+
let key = this._getCacheKey(`will:${client.id}`);
|
|
451
452
|
let will = this._cache.get(key);
|
|
452
453
|
|
|
453
454
|
if (callback) callback(null, will);
|
|
@@ -466,7 +467,7 @@ Persist.prototype.getWill = function(client, callback) {
|
|
|
466
467
|
*/
|
|
467
468
|
Persist.prototype.storeOfflinePacket = function(client, packet, callback) {
|
|
468
469
|
try {
|
|
469
|
-
if (!client || !client.
|
|
470
|
+
if (!client || !client.id) {
|
|
470
471
|
throw new Error('无效的客户端对象');
|
|
471
472
|
}
|
|
472
473
|
|
|
@@ -474,7 +475,7 @@ Persist.prototype.storeOfflinePacket = function(client, packet, callback) {
|
|
|
474
475
|
throw new Error('无效的消息包');
|
|
475
476
|
}
|
|
476
477
|
|
|
477
|
-
let key = this._getCacheKey(`offline:${client.
|
|
478
|
+
let key = this._getCacheKey(`offline:${client.id}:${packet.msgId}`);
|
|
478
479
|
this._cache.set(key, packet, this.config.ttl);
|
|
479
480
|
|
|
480
481
|
if (callback) callback(null);
|
|
@@ -490,11 +491,11 @@ Persist.prototype.storeOfflinePacket = function(client, packet, callback) {
|
|
|
490
491
|
*/
|
|
491
492
|
Persist.prototype.getOfflinePackets = function(client, callback) {
|
|
492
493
|
try {
|
|
493
|
-
if (!client || !client.
|
|
494
|
+
if (!client || !client.id) {
|
|
494
495
|
throw new Error('无效的客户端对象');
|
|
495
496
|
}
|
|
496
497
|
|
|
497
|
-
let pattern = `offline:${client.
|
|
498
|
+
let pattern = `offline:${client.id}:*`;
|
|
498
499
|
let keys = this._getKeysByPattern(pattern);
|
|
499
500
|
|
|
500
501
|
// 同步执行所有获取操作
|
|
@@ -553,7 +554,7 @@ Persist.prototype._procSubs = function(self, stream) {
|
|
|
553
554
|
if (sub) {
|
|
554
555
|
return {
|
|
555
556
|
type: 'subscription',
|
|
556
|
-
|
|
557
|
+
clientId: cid,
|
|
557
558
|
sub: sub
|
|
558
559
|
};
|
|
559
560
|
}
|
|
@@ -581,7 +582,7 @@ Persist.prototype._procWills = function(self, stream) {
|
|
|
581
582
|
if (will) {
|
|
582
583
|
return {
|
|
583
584
|
type: 'will',
|
|
584
|
-
|
|
585
|
+
clientId: cid,
|
|
585
586
|
will: will
|
|
586
587
|
};
|
|
587
588
|
}
|
|
@@ -609,7 +610,7 @@ Persist.prototype._procInflightMsgs = function(self, stream) {
|
|
|
609
610
|
if (packet) {
|
|
610
611
|
return {
|
|
611
612
|
type: 'inflight',
|
|
612
|
-
|
|
613
|
+
clientId: cid,
|
|
613
614
|
packet: packet
|
|
614
615
|
};
|
|
615
616
|
}
|
|
@@ -629,7 +630,7 @@ Persist.prototype.createStream = function() {
|
|
|
629
630
|
let self = this;
|
|
630
631
|
|
|
631
632
|
return new Readable({
|
|
632
|
-
|
|
633
|
+
objectMode: true,
|
|
633
634
|
|
|
634
635
|
read() {
|
|
635
636
|
try {
|