mm_os 4.3.7 → 5.0.0
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 +101 -504
- package/index.js +26 -19
- package/lib/loader.js +787 -0
- package/lib/os.js +369 -0
- package/package.json +55 -84
- package/LICENSE +0 -21
- package/README_EN.md +0 -505
- package/adapter/adapter.js +0 -484
- package/adapter/custom_persistence.js +0 -661
- package/adapter/mqtt.js +0 -254
- package/adapter/socket.js +0 -244
- package/adapter/web.js +0 -92
- package/adapter/websocket.js +0 -43
- package/ai.js +0 -130
- package/com/api/README.md +0 -3
- package/com/api/com.json +0 -5
- package/com/api/config.tpl.json +0 -41
- package/com/api/drive.js +0 -732
- package/com/api/index.js +0 -201
- package/com/api/oauth.js +0 -200
- package/com/api/script.tpl.js +0 -32
- package/com/cmd/README.md +0 -11
- package/com/cmd/com.json +0 -5
- package/com/cmd/config.tpl.json +0 -48
- package/com/cmd/drive.js +0 -176
- package/com/cmd/index.js +0 -287
- package/com/cmd/job/config.json +0 -66
- package/com/cmd/job/index.js +0 -241
- package/com/cmd/job/test.js +0 -188
- package/com/cmd/msg.json +0 -48
- package/com/cmd/script.tpl.js +0 -12
- package/com/db/README.md +0 -3
- package/com/db/com.json +0 -5
- package/com/db/drive.js +0 -2206
- package/com/db/index.js +0 -241
- package/com/event/README.md +0 -4
- package/com/event/com.json +0 -5
- package/com/event/config.tpl.json +0 -20
- package/com/event/drive.js +0 -61
- package/com/event/index.js +0 -438
- package/com/event/script.tpl.js +0 -23
- package/com/mqtt/README.md +0 -47
- package/com/mqtt/com.json +0 -5
- package/com/mqtt/config.tpl.json +0 -42
- package/com/mqtt/drive.js +0 -704
- package/com/mqtt/index.js +0 -1052
- package/com/mqtt/index.test.js +0 -503
- package/com/mqtt/script.tpl.js +0 -719
- package/com/nav/README.md +0 -2
- package/com/nav/com.json +0 -5
- package/com/nav/config.tpl.json +0 -84
- package/com/nav/drive.js +0 -702
- package/com/nav/index.js +0 -228
- package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
- package/com/nav/tpl/admin_pc/page_config.vue +0 -280
- package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
- package/com/nav/tpl/admin_pc/page_default.vue +0 -247
- package/com/nav/tpl/admin_pc/page_form.vue +0 -180
- package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
- package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
- package/com/nav/tpl/admin_pc/page_table.vue +0 -296
- package/com/nav/tpl/admin_pc/page_type.vue +0 -321
- package/com/nav/tpl/admin_pc/page_view.vue +0 -124
- package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
- package/com/nav/tpl/dev_pc/page_config.vue +0 -253
- package/com/nav/tpl/dev_pc/page_default.vue +0 -247
- package/com/nav/tpl/dev_pc/page_form.vue +0 -162
- package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
- package/com/nav/tpl/dev_pc/page_table.vue +0 -247
- package/com/nav/tpl/dev_pc/page_type.vue +0 -313
- package/com/nav/tpl/home_pc/page_channel.vue +0 -234
- package/com/nav/tpl/home_pc/page_default.vue +0 -234
- package/com/nav/tpl/home_pc/page_form.vue +0 -137
- package/com/nav/tpl/home_pc/page_list.vue +0 -234
- package/com/nav/tpl/home_pc/page_nav.vue +0 -221
- package/com/nav/tpl/home_pc/page_type.vue +0 -234
- package/com/nav/tpl/home_pc/page_view.vue +0 -125
- package/com/nav/tpl/home_phone/page_channel.vue +0 -234
- package/com/nav/tpl/home_phone/page_default.vue +0 -234
- package/com/nav/tpl/home_phone/page_form.vue +0 -137
- package/com/nav/tpl/home_phone/page_list.vue +0 -234
- package/com/nav/tpl/home_phone/page_nav.vue +0 -237
- package/com/nav/tpl/home_phone/page_type.vue +0 -234
- package/com/nav/tpl/home_phone/page_view.vue +0 -125
- package/com/nav/viewmodel.js +0 -446
- package/com/param/README.md +0 -2
- package/com/param/com.json +0 -5
- package/com/param/config.tpl.json +0 -136
- package/com/param/drive.js +0 -502
- package/com/param/index.js +0 -158
- package/com/param/script.tpl.js +0 -12
- package/com/pendant/README.md +0 -3
- package/com/pendant/com.json +0 -5
- package/com/pendant/config.tpl.json +0 -32
- package/com/pendant/drive.js +0 -204
- package/com/pendant/index.js +0 -467
- package/com/pendant/pendant.html +0 -16
- package/com/pendant/script.tpl.js +0 -18
- package/com/socket/README.md +0 -47
- package/com/socket/com.json +0 -5
- package/com/socket/config.tpl.json +0 -12
- package/com/socket/drive.js +0 -679
- package/com/socket/index.js +0 -402
- package/com/socket/script.tpl.js +0 -41
- package/com/sql/README.md +0 -3
- package/com/sql/com.json +0 -5
- package/com/sql/config.tpl.json +0 -74
- package/com/sql/drive.js +0 -1328
- package/com/sql/index.js +0 -152
- package/com/sql/script.tpl.js +0 -47
- package/com/static/README.md +0 -2
- package/com/static/com.json +0 -5
- package/com/static/config.tpl.json +0 -24
- package/com/static/drive.js +0 -194
- package/com/static/index.js +0 -237
- package/com/static/script.tpl.js +0 -28
- package/com/task/README.md +0 -3
- package/com/task/com.json +0 -5
- package/com/task/config.tpl.json +0 -24
- package/com/task/drive.js +0 -428
- package/com/task/index.js +0 -159
- package/com/task/script.tpl.js +0 -40
- package/com/template/com.json +0 -5
- package/com/template/config.tpl.json +0 -16
- package/com/template/drive.js +0 -81
- package/com/template/index.js +0 -141
- package/com.js +0 -237
- package/common/README.md +0 -2
- package/common/middleware/web_after/index.js +0 -26
- package/common/middleware/web_after/middleware.json +0 -9
- package/common/middleware/web_base/index.js +0 -113
- package/common/middleware/web_base/middleware.json +0 -19
- package/common/middleware/web_before/index.js +0 -33
- package/common/middleware/web_before/middleware.json +0 -9
- package/common/middleware/web_cors/index.js +0 -87
- package/common/middleware/web_cors/middleware.json +0 -24
- package/common/middleware/web_error/index.js +0 -131
- package/common/middleware/web_error/middleware.json +0 -19
- package/common/middleware/web_ip/index.js +0 -15
- package/common/middleware/web_ip/middleware.json +0 -14
- package/common/middleware/web_logger/index.js +0 -156
- package/common/middleware/web_logger/middleware.json +0 -14
- package/common/middleware/web_main/index.js +0 -26
- package/common/middleware/web_main/middleware.json +0 -9
- package/common/middleware/web_static/index.js +0 -73
- package/common/middleware/web_static/middleware.json +0 -54
- package/common/middleware/web_waf/index.js +0 -395
- package/common/middleware/web_waf/middleware.json +0 -13
- package/core/app/config.tpl.json +0 -67
- package/core/app/index.js +0 -711
- package/core/app/script.tpl.js +0 -52
- package/core/channel/index.js +0 -899
- package/core/channel/matcher.js +0 -585
- package/core/com/config.tpl.json +0 -16
- package/core/com/index.js +0 -77
- package/core/com/script.tpl.js +0 -5
- package/core/component/component.js +0 -42
- package/core/component/config.tpl.json +0 -63
- package/core/component/index.js +0 -273
- package/core/component/script.tpl.js +0 -19
- package/core/controller/config.tpl.json +0 -14
- package/core/controller/index.js +0 -371
- package/core/controller/script.tpl.js +0 -27
- package/core/factory/config.tpl.json +0 -14
- package/core/factory/entity.js +0 -275
- package/core/factory/index.js +0 -241
- package/core/factory/script.tpl.js +0 -16
- package/core/game/bat/index.js +0 -137
- package/core/game/bat/world.js +0 -622
- package/core/game/config.tpl.json +0 -16
- package/core/game/entity_admin.js +0 -230
- package/core/game/index.js +0 -186
- package/core/handler/config.tpl.json +0 -22
- package/core/handler/index.js +0 -181
- package/core/handler/script.tpl.js +0 -23
- package/core/logic/config.tpl.json +0 -14
- package/core/logic/index.js +0 -59
- package/core/logic/script.tpl.js +0 -19
- package/core/middleware/config.tpl.json +0 -16
- package/core/middleware/index.js +0 -117
- package/core/middleware/script.tpl.js +0 -37
- package/core/mod/config.tpl.json +0 -22
- package/core/mod/index.js +0 -136
- package/core/mod/script.tpl.js +0 -34
- package/core/model/config.tpl.json +0 -219
- package/core/model/index.js +0 -272
- package/core/model/model.js +0 -27
- package/core/model/script.tpl.js +0 -20
- package/core/notifier/config.tpl.json +0 -14
- package/core/notifier/index.js +0 -77
- package/core/notifier/script.tpl.js +0 -20
- package/core/plugin/config.tpl.json +0 -36
- package/core/plugin/index.js +0 -267
- package/core/plugin/script.tpl.js +0 -51
- package/core/pusher/config.tpl.json +0 -14
- package/core/pusher/index.js +0 -158
- package/core/pusher/script.tpl.js +0 -20
- package/core/room/bat/index.js +0 -170
- package/core/room/bat/room.js +0 -524
- package/core/room/config.tpl.json +0 -20
- package/core/room/index.js +0 -249
- package/core/room/room.js +0 -61
- package/core/scene/config.tpl.json +0 -14
- package/core/scene/index.js +0 -467
- package/core/scene/loop.js +0 -1306
- package/core/scene/map.js +0 -28
- package/core/scene/script.tpl.js +0 -22
- package/core/sender/config.tpl.json +0 -14
- package/core/sender/index.js +0 -79
- package/core/sender/script.tpl.js +0 -20
- package/core/service/config.tpl.json +0 -14
- package/core/service/index.js +0 -100
- package/core/service/script.tpl.js +0 -25
- package/core/store/config.tpl.json +0 -26
- package/core/store/index.js +0 -1794
- package/core/store/script.tpl.js +0 -22
- package/core/system/config.tpl.json +0 -18
- package/core/system/index.js +0 -312
- package/core/system/script.tpl.js +0 -77
- package/core/view/config.tpl.json +0 -14
- package/core/view/index.js +0 -91
- package/core/view/script.tpl.js +0 -20
- package/core/zone/bat/index.js +0 -744
- package/core/zone/config.tpl.json +0 -54
- package/core/zone/index.js +0 -655
- package/core/zone/script.tpl.js +0 -10
- package/core/zone/zone_bat.js +0 -136
- package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
- package/os.js +0 -53
- package/server.js +0 -602
- package/ulits/id_gen.js +0 -134
- package/ulits/word.js +0 -206
package/adapter/adapter.js
DELETED
|
@@ -1,484 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 适配器基类
|
|
3
|
-
* 定义了基础的网络通信接口
|
|
4
|
-
*/
|
|
5
|
-
const { idGen } = require('../ulits/id_gen.js');
|
|
6
|
-
|
|
7
|
-
class Adapter {
|
|
8
|
-
static config = {
|
|
9
|
-
name: 'adapter',
|
|
10
|
-
host: '0.0.0.0',
|
|
11
|
-
port: 8000
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 构造函数
|
|
16
|
-
* @param {object} config 配置参数
|
|
17
|
-
* @param {object} parent 父对象
|
|
18
|
-
*/
|
|
19
|
-
constructor(config, parent) {
|
|
20
|
-
if (parent) {
|
|
21
|
-
this.getParent = function () {
|
|
22
|
-
return parent;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
// 客户端连接列表
|
|
26
|
-
this.clients = new Map();
|
|
27
|
-
// 中间件数组
|
|
28
|
-
this.mw_list = [];
|
|
29
|
-
// 回调函数映射表,使用消息ID作为键
|
|
30
|
-
this.callback = new Map();
|
|
31
|
-
|
|
32
|
-
this.setConfig(config);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
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
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 生成ID
|
|
84
|
-
* @param {string} type ID类型,可选值:'client'(客户端ID)或'msg'(消息ID)
|
|
85
|
-
* @returns {string} 唯一ID
|
|
86
|
-
*/
|
|
87
|
-
Adapter.prototype.genId = function (type = 'client') {
|
|
88
|
-
return idGen.genId(type);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 生成客户端ID
|
|
93
|
-
* @returns {string} 唯一客户端ID
|
|
94
|
-
*/
|
|
95
|
-
Adapter.prototype.genClientId = function () {
|
|
96
|
-
return this.genId('client');
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 生成消息ID
|
|
101
|
-
* @returns {string} 唯一消息ID
|
|
102
|
-
*/
|
|
103
|
-
Adapter.prototype.genMsgId = function () {
|
|
104
|
-
return this.genId('msg');
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 使用中间件
|
|
109
|
-
* @param {Function} middleware 中间件函数,格式: async (ctx, next) => {}
|
|
110
|
-
* @returns {object} this
|
|
111
|
-
*/
|
|
112
|
-
Adapter.prototype.use = function (middleware) {
|
|
113
|
-
if (typeof middleware !== 'function') {
|
|
114
|
-
throw new Error('中间件必须是一个函数');
|
|
115
|
-
}
|
|
116
|
-
this.mw_list.push(middleware);
|
|
117
|
-
return this; // 支持链式调用
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 设置中间件(子类可以重写以提供特定实现)
|
|
122
|
-
*/
|
|
123
|
-
Adapter.prototype._setupMw = function () {
|
|
124
|
-
// 默认中间件设置,子类可以重写
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* 发送消息(需要子类实现)
|
|
129
|
-
* @param {string} clientid 客户端ID
|
|
130
|
-
* @param {string} message 消息内容
|
|
131
|
-
* @returns {void}
|
|
132
|
-
*/
|
|
133
|
-
Adapter.prototype._send = function (clientid, message) {
|
|
134
|
-
this.log('debug', '发送消息', { clientid, message });
|
|
135
|
-
throw new Error('子类必须实现_send方法');
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* 向指定客户端发送消息(JSON 2.0格式)
|
|
140
|
-
* @param {string} clientid 客户端ID
|
|
141
|
-
* @param {string} method 方法名
|
|
142
|
-
* @param {object} params 参数对象
|
|
143
|
-
* @param {Function} cb 可选,接收客户端回复的回调函数
|
|
144
|
-
* @returns {void}
|
|
145
|
-
*/
|
|
146
|
-
Adapter.prototype.send = function (clientid, method, params = {}, cb = null) {
|
|
147
|
-
// 生成唯一消息ID
|
|
148
|
-
let message_id = this.genMsgId();
|
|
149
|
-
|
|
150
|
-
// 构建JSON 2.0格式的消息
|
|
151
|
-
let msg = {
|
|
152
|
-
id: message_id,
|
|
153
|
-
method: method,
|
|
154
|
-
params: params
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
// 转换为字符串
|
|
158
|
-
let message = JSON.stringify(msg);
|
|
159
|
-
|
|
160
|
-
// 如果提供了回调函数,保存起来等待客户端回复
|
|
161
|
-
if (typeof cb === 'function') {
|
|
162
|
-
this.callback.set(message_id, cb);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return this._send(clientid, message);
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* 异步发送消息
|
|
170
|
-
* @param {string} clientid 客户端ID
|
|
171
|
-
* @param {string} method 方法名
|
|
172
|
-
* @param {object} params 参数对象
|
|
173
|
-
* @returns {Promise} 异步操作的结果
|
|
174
|
-
*/
|
|
175
|
-
Adapter.prototype.sendAsync = function (clientid, method, params = {}) {
|
|
176
|
-
return new Promise((resolve, reject) => {
|
|
177
|
-
// 发送消息
|
|
178
|
-
this.send(clientid, method, params, (msg, error) => {
|
|
179
|
-
if (error) {
|
|
180
|
-
reject(error);
|
|
181
|
-
} else {
|
|
182
|
-
resolve(msg);
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* 组合中间件(洋葱模型)
|
|
190
|
-
* @param {object} ctx 上下文对象
|
|
191
|
-
*/
|
|
192
|
-
Adapter.prototype._composeMw = async function (ctx) {
|
|
193
|
-
let index = -1;
|
|
194
|
-
|
|
195
|
-
let dispatch = async (i) => {
|
|
196
|
-
if (i <= index) {
|
|
197
|
-
throw new Error('next() 不能被多次调用');
|
|
198
|
-
}
|
|
199
|
-
index = i;
|
|
200
|
-
|
|
201
|
-
let middleware = this.mw_list[i];
|
|
202
|
-
if (!middleware) {
|
|
203
|
-
return Promise.resolve();
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
try {
|
|
207
|
-
return await middleware(ctx, () => dispatch(i + 1));
|
|
208
|
-
} catch (error) {
|
|
209
|
-
this.log('error', '中间件执行错误:', error);
|
|
210
|
-
throw error;
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
try {
|
|
215
|
-
await dispatch(0);
|
|
216
|
-
this._autoReply(ctx);
|
|
217
|
-
} catch (error) {
|
|
218
|
-
this._runMwError(ctx, error);
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* 自动回复客户端
|
|
224
|
-
* @param {object} ctx 上下文对象
|
|
225
|
-
* @private
|
|
226
|
-
*/
|
|
227
|
-
Adapter.prototype._autoReply = function (ctx) {
|
|
228
|
-
if (ctx.body === undefined || ctx.body === null) return;
|
|
229
|
-
|
|
230
|
-
// 检查原始消息是否是JSON 2.0格式
|
|
231
|
-
if (typeof ctx.message === 'object' && ctx.message.id) {
|
|
232
|
-
// 构建JSON 2.0格式的回复
|
|
233
|
-
let response = {
|
|
234
|
-
id: ctx.message.id,
|
|
235
|
-
result: ctx.body,
|
|
236
|
-
error: null
|
|
237
|
-
};
|
|
238
|
-
this._send(ctx.clientid, JSON.stringify(response));
|
|
239
|
-
} else {
|
|
240
|
-
// 非JSON格式消息,直接发送
|
|
241
|
-
this._send(ctx.clientid, ctx.body.toString());
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* 处理中间件错误
|
|
247
|
-
* @param {object} ctx 上下文对象
|
|
248
|
-
* @param {Error} error 错误对象
|
|
249
|
-
* @private
|
|
250
|
-
*/
|
|
251
|
-
Adapter.prototype._runMwError = function (ctx, error) {
|
|
252
|
-
this.log('error', '中间件链执行错误:', error);
|
|
253
|
-
// 如果是JSON 2.0格式的请求,发送错误回复
|
|
254
|
-
if (typeof ctx.message === 'object' && ctx.message.id) {
|
|
255
|
-
let response = {
|
|
256
|
-
id: ctx.message.id,
|
|
257
|
-
result: null,
|
|
258
|
-
error: { message: error.message }
|
|
259
|
-
};
|
|
260
|
-
this._send(ctx.clientid, JSON.stringify(response));
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* 消息接收处理
|
|
266
|
-
* @param {string} clientid 客户端ID
|
|
267
|
-
* @param {object} socket 客户端socket对象
|
|
268
|
-
* @param {string} data 接收的消息
|
|
269
|
-
*/
|
|
270
|
-
Adapter.prototype._onMsg = function (clientid, socket, data) {
|
|
271
|
-
// 检查参数是否有效
|
|
272
|
-
if (!this._check(clientid, data)) return;
|
|
273
|
-
|
|
274
|
-
// 处理消息数据
|
|
275
|
-
let clientid_str = this._getClientIdStr(clientid);
|
|
276
|
-
let message = this._parseStr(data);
|
|
277
|
-
|
|
278
|
-
// 解析消息并处理
|
|
279
|
-
let msg = this._parseMessage(clientid_str, message);
|
|
280
|
-
|
|
281
|
-
// 创建上下文对象并执行中间件
|
|
282
|
-
let ctx = this._createContext(clientid_str, socket, msg);
|
|
283
|
-
this._composeMw(ctx);
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* 验证参数有效性
|
|
288
|
-
* @param {string} clientid 客户端ID
|
|
289
|
-
* @param {string} data 消息数据
|
|
290
|
-
* @returns {boolean} 参数是否有效
|
|
291
|
-
* @private
|
|
292
|
-
*/
|
|
293
|
-
Adapter.prototype._check = function (clientid, data) {
|
|
294
|
-
if (!clientid) {
|
|
295
|
-
this.log('error', '客户端ID无效:', clientid);
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (!data) {
|
|
300
|
-
this.log('error', '消息数据无效:', data);
|
|
301
|
-
return false;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
return true;
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* 获取客户端ID字符串
|
|
309
|
-
* @param {string|object} clientid 客户端ID
|
|
310
|
-
* @returns {string} 客户端ID字符串
|
|
311
|
-
* @private
|
|
312
|
-
*/
|
|
313
|
-
Adapter.prototype._getClientIdStr = function (clientid) {
|
|
314
|
-
return typeof clientid === 'object' ? clientid.id || 'unknown' : clientid;
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* 解析数据为字符串
|
|
319
|
-
* @param {any} data 原始数据
|
|
320
|
-
* @returns {string} 字符串消息
|
|
321
|
-
* @private
|
|
322
|
-
*/
|
|
323
|
-
Adapter.prototype._parseStr = function (data) {
|
|
324
|
-
if (Buffer.isBuffer(data)) {
|
|
325
|
-
return data.toString();
|
|
326
|
-
} else if (typeof data === 'string') {
|
|
327
|
-
return data;
|
|
328
|
-
} else {
|
|
329
|
-
return String(data);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* 解析消息内容
|
|
335
|
-
* @param {string} clientid_str 客户端ID字符串
|
|
336
|
-
* @param {string} message 消息内容
|
|
337
|
-
* @returns {any} 解析后的消息
|
|
338
|
-
* @private
|
|
339
|
-
*/
|
|
340
|
-
Adapter.prototype._parseMessage = function (clientid_str, message) {
|
|
341
|
-
try {
|
|
342
|
-
let msg = JSON.parse(message);
|
|
343
|
-
// 检查是否是回复消息(具有id属性)
|
|
344
|
-
if (typeof msg === 'object' && msg.id) {
|
|
345
|
-
this._onMessage(clientid_str, msg);
|
|
346
|
-
}
|
|
347
|
-
return msg;
|
|
348
|
-
} catch (error) {
|
|
349
|
-
this.log('warn', `客户端 ${clientid_str} 发送的消息不是有效的JSON格式: ${message}`, error);
|
|
350
|
-
// 如果不是JSON格式,仍传递给中间件处理
|
|
351
|
-
return message;
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* 创建上下文对象
|
|
357
|
-
* @param {string} clientid_str 客户端ID字符串
|
|
358
|
-
* @param {object} socket 客户端socket对象
|
|
359
|
-
* @param {any} msg 消息内容
|
|
360
|
-
* @returns {object} 上下文对象
|
|
361
|
-
* @private
|
|
362
|
-
*/
|
|
363
|
-
Adapter.prototype._createContext = function (clientid_str, socket, msg) {
|
|
364
|
-
return {
|
|
365
|
-
clientid: clientid_str,
|
|
366
|
-
socket: socket,
|
|
367
|
-
message: msg,
|
|
368
|
-
adapter: this,
|
|
369
|
-
response: '',
|
|
370
|
-
body: null,
|
|
371
|
-
// 发送消息给当前客户端
|
|
372
|
-
send: (method, params, cb) => {
|
|
373
|
-
this.send(clientid_str, method, params, cb);
|
|
374
|
-
},
|
|
375
|
-
// 异步发送消息
|
|
376
|
-
sendAsync: (method, params = {}) => {
|
|
377
|
-
return this.sendAsync(clientid_str, method, params);
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* 消息接收处理
|
|
384
|
-
* @param {string} clientid 客户端ID
|
|
385
|
-
* @param {object} msg 接收的消息
|
|
386
|
-
*/
|
|
387
|
-
Adapter.prototype._onMessage = function (clientid, msg) {
|
|
388
|
-
// 查找对应的回调函数
|
|
389
|
-
this.log('debug', '收到消息', { clientid });
|
|
390
|
-
let callback = this.callback.get(msg.id);
|
|
391
|
-
if (callback) {
|
|
392
|
-
// 执行回调函数并移除
|
|
393
|
-
this.callback.delete(msg.id);
|
|
394
|
-
// 回调参数:消息对象,错误对象(如果有)
|
|
395
|
-
let error = msg.error ? new Error(msg.error.message) : null;
|
|
396
|
-
callback(msg, error);
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* 连接关闭处理
|
|
403
|
-
* @param {string} clientid 客户端ID
|
|
404
|
-
*/
|
|
405
|
-
Adapter.prototype._onClose = function (clientid) {
|
|
406
|
-
// 移除客户端连接
|
|
407
|
-
this.clients.delete(clientid);
|
|
408
|
-
this.log('info', `客户端断开连接: ${clientid}`);
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
/**
|
|
412
|
-
* 错误处理
|
|
413
|
-
* @param {string} clientid 客户端ID
|
|
414
|
-
* @param {Error} error 错误对象
|
|
415
|
-
*/
|
|
416
|
-
Adapter.prototype._onError = function (clientid, error) {
|
|
417
|
-
this.log('error', `客户端 ${clientid} 发生错误:`, error);
|
|
418
|
-
// 移除客户端连接
|
|
419
|
-
this.clients.delete(clientid);
|
|
420
|
-
};
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* 停止Adapter服务器(子类可以重写以提供特定实现)
|
|
424
|
-
*/
|
|
425
|
-
Adapter.prototype._stop = function () {
|
|
426
|
-
// 关闭所有客户端连接
|
|
427
|
-
for (let [id, client] of this.clients) {
|
|
428
|
-
try {
|
|
429
|
-
if (client.destroy) {
|
|
430
|
-
// Socket客户端使用destroy方法
|
|
431
|
-
client.destroy();
|
|
432
|
-
} else if (client.close) {
|
|
433
|
-
// WebSocket客户端使用close方法
|
|
434
|
-
client.close();
|
|
435
|
-
}
|
|
436
|
-
} catch (error) {
|
|
437
|
-
this.log('error', `关闭客户端 ${id} 连接时发生错误:`, error);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* 停止Adapter服务器
|
|
444
|
-
*/
|
|
445
|
-
Adapter.prototype.stop = function () {
|
|
446
|
-
this._stop();
|
|
447
|
-
this.clients.clear();
|
|
448
|
-
|
|
449
|
-
// 关闭服务器
|
|
450
|
-
if (this.server) {
|
|
451
|
-
this.server.close(() => {
|
|
452
|
-
this.log('info', '服务器已停止');
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* 广播消息
|
|
459
|
-
* @param {string} message 消息内容
|
|
460
|
-
* @param {Array<string>} ids 可选,客户端ID数组,为空则向所有客户端广播
|
|
461
|
-
*/
|
|
462
|
-
Adapter.prototype.bcast = function (message, ids = null) {
|
|
463
|
-
if (ids && Array.isArray(ids)) {
|
|
464
|
-
// 向指定客户端数组广播
|
|
465
|
-
for (let id of ids) {
|
|
466
|
-
this._send(id, message);
|
|
467
|
-
}
|
|
468
|
-
this.log('debug', `服务器向指定客户端广播消息: ${message}`);
|
|
469
|
-
} else {
|
|
470
|
-
// 向所有客户端广播
|
|
471
|
-
for (let [id] of this.clients) {
|
|
472
|
-
this._send(id, message);
|
|
473
|
-
}
|
|
474
|
-
this.log('debug', `服务器广播消息: ${message}`);
|
|
475
|
-
}
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
/**
|
|
479
|
-
* 启动Adapter服务器
|
|
480
|
-
*/
|
|
481
|
-
Adapter.prototype.start = function () {
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
exports.Adapter = Adapter;
|