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.
Files changed (232) hide show
  1. package/README.md +101 -504
  2. package/index.js +26 -19
  3. package/lib/loader.js +787 -0
  4. package/lib/os.js +369 -0
  5. package/package.json +55 -84
  6. package/LICENSE +0 -21
  7. package/README_EN.md +0 -505
  8. package/adapter/adapter.js +0 -484
  9. package/adapter/custom_persistence.js +0 -661
  10. package/adapter/mqtt.js +0 -254
  11. package/adapter/socket.js +0 -244
  12. package/adapter/web.js +0 -92
  13. package/adapter/websocket.js +0 -43
  14. package/ai.js +0 -130
  15. package/com/api/README.md +0 -3
  16. package/com/api/com.json +0 -5
  17. package/com/api/config.tpl.json +0 -41
  18. package/com/api/drive.js +0 -732
  19. package/com/api/index.js +0 -201
  20. package/com/api/oauth.js +0 -200
  21. package/com/api/script.tpl.js +0 -32
  22. package/com/cmd/README.md +0 -11
  23. package/com/cmd/com.json +0 -5
  24. package/com/cmd/config.tpl.json +0 -48
  25. package/com/cmd/drive.js +0 -176
  26. package/com/cmd/index.js +0 -287
  27. package/com/cmd/job/config.json +0 -66
  28. package/com/cmd/job/index.js +0 -241
  29. package/com/cmd/job/test.js +0 -188
  30. package/com/cmd/msg.json +0 -48
  31. package/com/cmd/script.tpl.js +0 -12
  32. package/com/db/README.md +0 -3
  33. package/com/db/com.json +0 -5
  34. package/com/db/drive.js +0 -2206
  35. package/com/db/index.js +0 -241
  36. package/com/event/README.md +0 -4
  37. package/com/event/com.json +0 -5
  38. package/com/event/config.tpl.json +0 -20
  39. package/com/event/drive.js +0 -61
  40. package/com/event/index.js +0 -438
  41. package/com/event/script.tpl.js +0 -23
  42. package/com/mqtt/README.md +0 -47
  43. package/com/mqtt/com.json +0 -5
  44. package/com/mqtt/config.tpl.json +0 -42
  45. package/com/mqtt/drive.js +0 -704
  46. package/com/mqtt/index.js +0 -1052
  47. package/com/mqtt/index.test.js +0 -503
  48. package/com/mqtt/script.tpl.js +0 -719
  49. package/com/nav/README.md +0 -2
  50. package/com/nav/com.json +0 -5
  51. package/com/nav/config.tpl.json +0 -84
  52. package/com/nav/drive.js +0 -702
  53. package/com/nav/index.js +0 -228
  54. package/com/nav/tpl/admin_pc/page_channel.vue +0 -305
  55. package/com/nav/tpl/admin_pc/page_config.vue +0 -280
  56. package/com/nav/tpl/admin_pc/page_config_form.vue +0 -194
  57. package/com/nav/tpl/admin_pc/page_default.vue +0 -247
  58. package/com/nav/tpl/admin_pc/page_form.vue +0 -180
  59. package/com/nav/tpl/admin_pc/page_lang.vue +0 -264
  60. package/com/nav/tpl/admin_pc/page_nav.vue +0 -321
  61. package/com/nav/tpl/admin_pc/page_table.vue +0 -296
  62. package/com/nav/tpl/admin_pc/page_type.vue +0 -321
  63. package/com/nav/tpl/admin_pc/page_view.vue +0 -124
  64. package/com/nav/tpl/dev_pc/page_channel.vue +0 -253
  65. package/com/nav/tpl/dev_pc/page_config.vue +0 -253
  66. package/com/nav/tpl/dev_pc/page_default.vue +0 -247
  67. package/com/nav/tpl/dev_pc/page_form.vue +0 -162
  68. package/com/nav/tpl/dev_pc/page_nav.vue +0 -253
  69. package/com/nav/tpl/dev_pc/page_table.vue +0 -247
  70. package/com/nav/tpl/dev_pc/page_type.vue +0 -313
  71. package/com/nav/tpl/home_pc/page_channel.vue +0 -234
  72. package/com/nav/tpl/home_pc/page_default.vue +0 -234
  73. package/com/nav/tpl/home_pc/page_form.vue +0 -137
  74. package/com/nav/tpl/home_pc/page_list.vue +0 -234
  75. package/com/nav/tpl/home_pc/page_nav.vue +0 -221
  76. package/com/nav/tpl/home_pc/page_type.vue +0 -234
  77. package/com/nav/tpl/home_pc/page_view.vue +0 -125
  78. package/com/nav/tpl/home_phone/page_channel.vue +0 -234
  79. package/com/nav/tpl/home_phone/page_default.vue +0 -234
  80. package/com/nav/tpl/home_phone/page_form.vue +0 -137
  81. package/com/nav/tpl/home_phone/page_list.vue +0 -234
  82. package/com/nav/tpl/home_phone/page_nav.vue +0 -237
  83. package/com/nav/tpl/home_phone/page_type.vue +0 -234
  84. package/com/nav/tpl/home_phone/page_view.vue +0 -125
  85. package/com/nav/viewmodel.js +0 -446
  86. package/com/param/README.md +0 -2
  87. package/com/param/com.json +0 -5
  88. package/com/param/config.tpl.json +0 -136
  89. package/com/param/drive.js +0 -502
  90. package/com/param/index.js +0 -158
  91. package/com/param/script.tpl.js +0 -12
  92. package/com/pendant/README.md +0 -3
  93. package/com/pendant/com.json +0 -5
  94. package/com/pendant/config.tpl.json +0 -32
  95. package/com/pendant/drive.js +0 -204
  96. package/com/pendant/index.js +0 -467
  97. package/com/pendant/pendant.html +0 -16
  98. package/com/pendant/script.tpl.js +0 -18
  99. package/com/socket/README.md +0 -47
  100. package/com/socket/com.json +0 -5
  101. package/com/socket/config.tpl.json +0 -12
  102. package/com/socket/drive.js +0 -679
  103. package/com/socket/index.js +0 -402
  104. package/com/socket/script.tpl.js +0 -41
  105. package/com/sql/README.md +0 -3
  106. package/com/sql/com.json +0 -5
  107. package/com/sql/config.tpl.json +0 -74
  108. package/com/sql/drive.js +0 -1328
  109. package/com/sql/index.js +0 -152
  110. package/com/sql/script.tpl.js +0 -47
  111. package/com/static/README.md +0 -2
  112. package/com/static/com.json +0 -5
  113. package/com/static/config.tpl.json +0 -24
  114. package/com/static/drive.js +0 -194
  115. package/com/static/index.js +0 -237
  116. package/com/static/script.tpl.js +0 -28
  117. package/com/task/README.md +0 -3
  118. package/com/task/com.json +0 -5
  119. package/com/task/config.tpl.json +0 -24
  120. package/com/task/drive.js +0 -428
  121. package/com/task/index.js +0 -159
  122. package/com/task/script.tpl.js +0 -40
  123. package/com/template/com.json +0 -5
  124. package/com/template/config.tpl.json +0 -16
  125. package/com/template/drive.js +0 -81
  126. package/com/template/index.js +0 -141
  127. package/com.js +0 -237
  128. package/common/README.md +0 -2
  129. package/common/middleware/web_after/index.js +0 -26
  130. package/common/middleware/web_after/middleware.json +0 -9
  131. package/common/middleware/web_base/index.js +0 -113
  132. package/common/middleware/web_base/middleware.json +0 -19
  133. package/common/middleware/web_before/index.js +0 -33
  134. package/common/middleware/web_before/middleware.json +0 -9
  135. package/common/middleware/web_cors/index.js +0 -87
  136. package/common/middleware/web_cors/middleware.json +0 -24
  137. package/common/middleware/web_error/index.js +0 -131
  138. package/common/middleware/web_error/middleware.json +0 -19
  139. package/common/middleware/web_ip/index.js +0 -15
  140. package/common/middleware/web_ip/middleware.json +0 -14
  141. package/common/middleware/web_logger/index.js +0 -156
  142. package/common/middleware/web_logger/middleware.json +0 -14
  143. package/common/middleware/web_main/index.js +0 -26
  144. package/common/middleware/web_main/middleware.json +0 -9
  145. package/common/middleware/web_static/index.js +0 -73
  146. package/common/middleware/web_static/middleware.json +0 -54
  147. package/common/middleware/web_waf/index.js +0 -395
  148. package/common/middleware/web_waf/middleware.json +0 -13
  149. package/core/app/config.tpl.json +0 -67
  150. package/core/app/index.js +0 -711
  151. package/core/app/script.tpl.js +0 -52
  152. package/core/channel/index.js +0 -899
  153. package/core/channel/matcher.js +0 -585
  154. package/core/com/config.tpl.json +0 -16
  155. package/core/com/index.js +0 -77
  156. package/core/com/script.tpl.js +0 -5
  157. package/core/component/component.js +0 -42
  158. package/core/component/config.tpl.json +0 -63
  159. package/core/component/index.js +0 -273
  160. package/core/component/script.tpl.js +0 -19
  161. package/core/controller/config.tpl.json +0 -14
  162. package/core/controller/index.js +0 -371
  163. package/core/controller/script.tpl.js +0 -27
  164. package/core/factory/config.tpl.json +0 -14
  165. package/core/factory/entity.js +0 -275
  166. package/core/factory/index.js +0 -241
  167. package/core/factory/script.tpl.js +0 -16
  168. package/core/game/bat/index.js +0 -137
  169. package/core/game/bat/world.js +0 -622
  170. package/core/game/config.tpl.json +0 -16
  171. package/core/game/entity_admin.js +0 -230
  172. package/core/game/index.js +0 -186
  173. package/core/handler/config.tpl.json +0 -22
  174. package/core/handler/index.js +0 -181
  175. package/core/handler/script.tpl.js +0 -23
  176. package/core/logic/config.tpl.json +0 -14
  177. package/core/logic/index.js +0 -59
  178. package/core/logic/script.tpl.js +0 -19
  179. package/core/middleware/config.tpl.json +0 -16
  180. package/core/middleware/index.js +0 -117
  181. package/core/middleware/script.tpl.js +0 -37
  182. package/core/mod/config.tpl.json +0 -22
  183. package/core/mod/index.js +0 -136
  184. package/core/mod/script.tpl.js +0 -34
  185. package/core/model/config.tpl.json +0 -219
  186. package/core/model/index.js +0 -272
  187. package/core/model/model.js +0 -27
  188. package/core/model/script.tpl.js +0 -20
  189. package/core/notifier/config.tpl.json +0 -14
  190. package/core/notifier/index.js +0 -77
  191. package/core/notifier/script.tpl.js +0 -20
  192. package/core/plugin/config.tpl.json +0 -36
  193. package/core/plugin/index.js +0 -267
  194. package/core/plugin/script.tpl.js +0 -51
  195. package/core/pusher/config.tpl.json +0 -14
  196. package/core/pusher/index.js +0 -158
  197. package/core/pusher/script.tpl.js +0 -20
  198. package/core/room/bat/index.js +0 -170
  199. package/core/room/bat/room.js +0 -524
  200. package/core/room/config.tpl.json +0 -20
  201. package/core/room/index.js +0 -249
  202. package/core/room/room.js +0 -61
  203. package/core/scene/config.tpl.json +0 -14
  204. package/core/scene/index.js +0 -467
  205. package/core/scene/loop.js +0 -1306
  206. package/core/scene/map.js +0 -28
  207. package/core/scene/script.tpl.js +0 -22
  208. package/core/sender/config.tpl.json +0 -14
  209. package/core/sender/index.js +0 -79
  210. package/core/sender/script.tpl.js +0 -20
  211. package/core/service/config.tpl.json +0 -14
  212. package/core/service/index.js +0 -100
  213. package/core/service/script.tpl.js +0 -25
  214. package/core/store/config.tpl.json +0 -26
  215. package/core/store/index.js +0 -1794
  216. package/core/store/script.tpl.js +0 -22
  217. package/core/system/config.tpl.json +0 -18
  218. package/core/system/index.js +0 -312
  219. package/core/system/script.tpl.js +0 -77
  220. package/core/view/config.tpl.json +0 -14
  221. package/core/view/index.js +0 -91
  222. package/core/view/script.tpl.js +0 -20
  223. package/core/zone/bat/index.js +0 -744
  224. package/core/zone/config.tpl.json +0 -54
  225. package/core/zone/index.js +0 -655
  226. package/core/zone/script.tpl.js +0 -10
  227. package/core/zone/zone_bat.js +0 -136
  228. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +0 -24
  229. package/os.js +0 -53
  230. package/server.js +0 -602
  231. package/ulits/id_gen.js +0 -134
  232. package/ulits/word.js +0 -206
package/adapter/mqtt.js DELETED
@@ -1,254 +0,0 @@
1
- const { Aedes } = require('aedes');
2
- const net = require('net');
3
- const { Adapter } = require('./adapter');
4
- const { Persist } = require('./custom_persistence.js');
5
-
6
- /**
7
- * MQTT适配器
8
- */
9
- class Mqtt extends Adapter {
10
- static config = {
11
- host: '127.0.0.1',
12
- port: 1883,
13
- cache: true,
14
- prefix: 'mqtt_',
15
- ttl: 3600000,
16
- auth_enabled: false,
17
- auth_users: [], // 用户名密码列表,格式: { clientid: 'server', username: 'user', password: 'pass'}
18
- };
19
- /**
20
- * 构造函数
21
- * @param {object} config 配置参数
22
- * @param {object} parent 父对象
23
- */
24
- constructor(config, parent) {
25
- super({ ...Mqtt.config, ...config }, parent);
26
- }
27
- }
28
-
29
- /**
30
- * 初始化适配器
31
- * @param {object} cache 缓存管理器
32
- * @returns {object} 适配器实例
33
- * @private
34
- */
35
- Mqtt.prototype._init = async function (cache) {
36
- // 创建自定义持久化实例
37
- let persist = null;
38
- if (this.web && cache) {
39
- persist = new Persist(this.config, cache);
40
- this.log('info', 'MQTT使用自定义持久化');
41
- }
42
- // 创建aedes实例
43
- // 注意: aedes v1构造函数不初始化persistence,需要调用listen()
44
- this.broker = new Aedes({
45
- persistence: persist,
46
- preConnect: function (client, packet, callback) {
47
- callback(null, true);
48
- }
49
- });
50
- // 必须调用listen()初始化persistence,否则客户端连接时会崩溃
51
- await this.broker.listen();
52
- // 创建TCP服务器
53
- this.web = net.createServer(this.broker.handle);
54
-
55
- // 配置身份验证
56
- this._setupAuth();
57
-
58
- // 监听客户端连接
59
- this.broker.on('client', (client) => {
60
- // 存储客户端连接
61
- this.clients.set(client.id, client);
62
-
63
- // 记录连接日志
64
- this.log('info', `MQTT客户端连接成功: ${client.id}`);
65
- });
66
- return this;
67
- };
68
-
69
- /**
70
- * 检查用户是否存在
71
- * @param {string} clientid 客户端ID
72
- * @param {string} username 用户名
73
- * @param {string} password 密码
74
- * @returns {Promise<boolean>} 是否存在
75
- */
76
- Mqtt.prototype.checkUser = async function (clientid, username, password) {
77
- this.log('debug', '检查用户', clientid, username);
78
- let pass = password ? password.toString() : '';
79
- // 查找匹配的用户
80
- // 在实际应用中,这里可以改为异步数据库查询
81
- // 例如: return await db.query('SELECT * FROM users WHERE username = ? AND password = ?', [username, password_str]);
82
- return this.config.auth_users.find(u => u.clientid === clientid && u.username === username && u.password === pass);
83
- };
84
-
85
- /**
86
- * 验证客户端身份
87
- * @param {object} client 客户端对象
88
- * @param {string} username 用户名
89
- * @param {object} password 密码
90
- * @returns {Promise<boolean>} 是否验证通过
91
- */
92
- Mqtt.prototype.auth = async function (client, username, password) {
93
- // 查找匹配的用户
94
- return await this.checkUser(client.id, username, password);
95
- };
96
-
97
- /**
98
- * 配置身份验证
99
- * @private
100
- */
101
- Mqtt.prototype._setupAuth = function () {
102
- // 如果启用了身份验证
103
- if (this.config.auth_enabled) {
104
- // 监听认证事件
105
- this.broker.authenticate = async (client, username, password, callback) => {
106
- try {
107
- // 转换密码格式为字符串
108
- let password_str = password ? password.toString() : '';
109
-
110
- // // 记录认证尝试
111
- // this.log('debug', `MQTT认证尝试: clientId=${client.id}, username=${username}, password=${password_str ? '[存在]' : '[不存在]'}`);
112
-
113
- // 如果提供了自定义验证函数,则使用自定义验证
114
- let user = await this.auth(client, username, password_str);
115
- if (user) {
116
- // this.log('info', `MQTT认证成功: clientId=${client.id}, username=${username}`);
117
- return callback(null, true);
118
- } else {
119
- // this.log('warn', `MQTT认证失败: clientId=${client.id}, username=${username}, 用户名或密码错误`);
120
- return callback(new Error('用户名或密码错误'));
121
- }
122
- } catch (error) {
123
- this.log('error', `MQTT认证错误: clientId=${client.id}`, error);
124
- return callback(new Error('认证服务内部错误'));
125
- }
126
- };
127
- }
128
- };
129
-
130
- /**
131
- * 启动MQTT服务器
132
- * @returns {Promise} 返回Promise,等待服务端启动完成
133
- */
134
- Mqtt.prototype.start = function () {
135
- var _this = this;
136
- return new Promise((resolve, reject) => {
137
- let {
138
- host,
139
- port
140
- } = _this.config;
141
-
142
- _this.web.listen(port, host, () => {
143
- _this.log('info', `MQTT服务端启动!访问地址:mqtt://${host}:${port}`);
144
- resolve();
145
- });
146
-
147
- // 监听服务器错误
148
- _this.web.on('error', (error) => {
149
- _this.log('error', 'MQTT服务器启动失败:', error);
150
- reject(error);
151
- });
152
- });
153
- };
154
-
155
- /**
156
- * 发布消息到指定主题
157
- * @param {string} topic 主题
158
- * @param {string} message 消息内容
159
- * @param {object} [options] 发布选项
160
- * @param {number} [options.qos=0] QoS级别(0, 1, 2)
161
- * @param {boolean} [options.retain=false] 是否为retain消息
162
- * @returns {boolean} 发布是否成功
163
- */
164
- Mqtt.prototype.publish = function (topic, message, options) {
165
- try {
166
- // 默认选项
167
- const OPTS = {qos: 0,
168
- retain: false, ...options || {}};
169
-
170
- this.broker.publish({
171
- topic: topic,
172
- payload: message,
173
- qos: OPTS.qos,
174
- retain: OPTS.retain
175
- });
176
-
177
- this.log('debug', `MQTT服务器发布消息到主题 ${topic}: ${message}, qos: ${OPTS.qos}, retain: ${OPTS.retain}`);
178
- return true;
179
- } catch (error) {
180
- this.log('error', 'MQTT发布消息失败:', error);
181
- return false;
182
- }
183
- };
184
-
185
- /**
186
- * 订阅主题
187
- * @param {string} topic 主题
188
- * @param {Function} callback 回调函数
189
- */
190
- Mqtt.prototype.subscribe = function (topic, callback) {
191
- this.broker.subscribe(topic, callback);
192
- };
193
-
194
- /**
195
- * 取消订阅主题
196
- * @param {string} topic 主题
197
- * @param {Function} callback 回调函数
198
- */
199
- Mqtt.prototype.unsubscribe = function (topic, callback) {
200
- this.broker.unsubscribe(topic, callback);
201
- };
202
-
203
- /**
204
- * 停止服务器
205
- */
206
- Mqtt.prototype.stop = function () {
207
- // 调用父类的_stop方法
208
- this._stop();
209
-
210
- // 关闭服务器
211
- this.broker.close(() => {
212
- // 清空客户端集合
213
- this.clients.clear();
214
- this.log('info', 'MQTT服务器已停止');
215
- });
216
- };
217
-
218
- /**
219
- * 停止Adapter服务器
220
- * @private
221
- */
222
- Mqtt.prototype._stop = function () {
223
- // 关闭所有客户端连接
224
- for (let [clientid, client] of this.clients) {
225
- try {
226
- // 使用MQTT客户端的断开连接方法
227
- if (client && client.close) {
228
- client.close();
229
- }
230
- } catch (error) {
231
- this.log('error', `关闭MQTT客户端 ${clientid} 失败:`, error);
232
- }
233
- }
234
- };
235
-
236
- /**
237
- * 监听MQTT事件
238
- * @param {string} event 事件名称
239
- * @param {...*} args 事件参数
240
- */
241
- Mqtt.prototype.on = function (event, ...args) {
242
- this.broker.on(event, ...args);
243
- };
244
-
245
- /**
246
- * 取消监听MQTT事件
247
- * @param {string} event 事件名称
248
- * @param {...*} args 事件参数
249
- */
250
- Mqtt.prototype.off = function (event, ...args) {
251
- this.broker.off(event, ...args);
252
- };
253
-
254
- exports.Mqtt = Mqtt;
package/adapter/socket.js DELETED
@@ -1,244 +0,0 @@
1
- const net = require('net');
2
- const { Adapter } = require('./adapter');
3
-
4
- /**
5
- * Socket适配器
6
- */
7
- class Socket extends Adapter {
8
- static config = {
9
- host: '0.0.0.0',
10
- port: 8200
11
- };
12
- constructor(config, parent) {
13
- super({ ...Socket.config, ...config }, parent);
14
- }
15
- }
16
-
17
- /**
18
- * 初始化 - 供外部调用
19
- * @param {object} server 服务器实例
20
- * @returns {object} 适配器实例
21
- */
22
- Socket.prototype.init = function (server) {
23
- if (server) {
24
- this.getServer = function () { return server; };
25
- }
26
- this._middleware = [];
27
- return this;
28
- };
29
-
30
- /**
31
- * 注册中间件
32
- * @param {Function} middleware async (ctx, next) => {}
33
- */
34
- Socket.prototype.use = function (middleware) {
35
- this._middleware.push(middleware);
36
- };
37
-
38
- /**
39
- * 启动适配器
40
- */
41
- Socket.prototype.start = function () {
42
- var self = this;
43
- var { host, port } = this.config;
44
-
45
- this.web = net.createServer(function (socket) {
46
- self._onConn(socket);
47
- });
48
-
49
- this.web.listen(port, host, function () {
50
- self.log('info', 'Socket服务已启动!监听地址:' + host + ':' + port);
51
- });
52
-
53
- this.web.on('error', function (error) {
54
- self.log('error', 'Socket启动失败:', error);
55
- });
56
- };
57
- /**
58
- * 客户端连接处理
59
- * @param {net.Socket} socket TCP socket
60
- */
61
- Socket.prototype._onConn = function (socket) {
62
- var self = this;
63
- var client_ip = socket.remoteAddress || '127.0.0.1';
64
- var clientid = 'tcp_' + this.genClientId();
65
-
66
- this.clients.set(clientid, socket);
67
- this.log('info', 'Socket客户端连接成功:', client_ip, clientid);
68
-
69
- // 缓存未处理完的数据(TCP 粘包处理)
70
- var buffer = '';
71
-
72
- socket.on('data', function (data) {
73
- buffer += data.toString();
74
-
75
- // 尝试按换行符分割完整消息
76
- var lines = buffer.split('\n');
77
- // 最后一段可能是未完成的消息,保留到 buffer
78
- buffer = lines.pop() || '';
79
-
80
- for (var i = 0; i < lines.length; i++) {
81
- var line = lines[i].trim();
82
- if (!line) continue;
83
- self._onData(line, socket, clientid);
84
- }
85
- });
86
-
87
- socket.on('close', function () {
88
- self._onClose(clientid);
89
- self.clients.delete(clientid);
90
- });
91
-
92
- socket.on('error', function (err) {
93
- self.log('error', 'Socket客户端错误:', clientid, err);
94
- self.clients.delete(clientid);
95
- });
96
- };
97
-
98
- /**
99
- * 收到一行完整数据
100
- * @param {string} body_str 消息内容
101
- * @param {net.Socket} socket TCP socket
102
- * @param {string} clientid 客户端ID
103
- */
104
- Socket.prototype._onData = function (body_str, socket, clientid) {
105
- var self = this;
106
-
107
- // 尝试解析 JSON
108
- var json;
109
- if (body_str.indexOf('{') === 0 || body_str.indexOf('[') === 0) {
110
- try {
111
- json = JSON.parse(body_str);
112
- } catch (e) {
113
- // 非 JSON 格式,按原始消息处理
114
- }
115
- }
116
-
117
- // 创建最小上下文
118
- var ctx = {
119
- clientid: clientid,
120
- socket: socket,
121
- message: json || body_str,
122
- ip: socket.remoteAddress || '127.0.0.1',
123
- send: function (data) {
124
- var str = typeof data === 'object' ? JSON.stringify(data) : data;
125
- if (socket.writable) {
126
- socket.write(str + '\n');
127
- }
128
- },
129
- close: function () {
130
- socket.end();
131
- }
132
- };
133
-
134
- if (json && json.method) {
135
- // 有 method 字段,走 methods 分发
136
- this._dispatchMethod(json, ctx);
137
- } else {
138
- // 走中间件链
139
- this._runMiddleware(ctx);
140
- }
141
- };
142
-
143
- /**
144
- * 分发方法调用
145
- * @param {object} json {id, method, params}
146
- * @param {object} ctx 上下文
147
- */
148
- Socket.prototype._dispatchMethod = function (json, ctx) {
149
- var self = this;
150
- var { id, method, params } = json;
151
-
152
- // 如果有 result 字段且是回复消息,触发回调
153
- if (json.result && id) {
154
- // 交由中间件处理(未来可扩展回调)
155
- this._runMiddleware(ctx);
156
- return;
157
- }
158
-
159
- // 查找方法
160
- // 先从 adapter 自身的 methods 找,再从全局 $.methods 找
161
- var handler = this.methods && this.methods[method];
162
- if (!handler) {
163
- handler = $.methods && $.methods[method];
164
- }
165
-
166
- if (handler) {
167
- // 异步执行方法
168
- Promise.resolve().then(function () {
169
- return handler(params, ctx);
170
- }).then(function (result) {
171
- if (result !== undefined && result !== null && id) {
172
- var ret = { id: id };
173
- if (typeof result === 'object' && !Array.isArray(result)) {
174
- Object.assign(ret, result);
175
- } else {
176
- ret.result = result;
177
- }
178
- ctx.send(ret);
179
- }
180
- }).catch(function (err) {
181
- self.log('error', 'Socket方法执行错误:', err);
182
- if (id) {
183
- var err_ret = { id: id, error: { message: err.message || '执行失败' } };
184
- ctx.send(err_ret);
185
- }
186
- });
187
- } else {
188
- // 未找到方法,走中间件链
189
- this._runMiddleware(ctx);
190
- }
191
- };
192
-
193
- /**
194
- * 运行中间件链
195
- * @param {object} ctx 上下文
196
- */
197
- Socket.prototype._runMiddleware = function (ctx) {
198
- var mw = this._middleware;
199
- if (mw.length === 0) return;
200
-
201
- var idx = 0;
202
- var self = this;
203
- function next() {
204
- var fn = mw[idx++];
205
- if (fn) {
206
- return Promise.resolve(fn(ctx, next));
207
- }
208
- return Promise.resolve();
209
- }
210
- next().catch(function (err) {
211
- self.log('error', 'Socket中间件错误:', err);
212
- });
213
- };
214
-
215
- /**
216
- * 发送消息
217
- * @param {string} clientid 客户端ID
218
- * @param {string} message 消息内容
219
- * @returns {boolean} 发送是否成功
220
- */
221
- Socket.prototype._send = function (clientid, message) {
222
- let socket = this.clients.get(clientid);
223
- if (socket && !socket.destroyed) {
224
- var str = typeof message === 'object' ? JSON.stringify(message) : message;
225
- socket.write(str + '\n');
226
- return true;
227
- }
228
- return false;
229
- };
230
-
231
- /**
232
- * 停止适配器
233
- */
234
- Socket.prototype.stop = function () {
235
- this._stop();
236
- if (this.web) {
237
- this.web.close(function () {
238
- this.clients.clear();
239
- this.log('info', 'Socket服务已停止');
240
- }.bind(this));
241
- }
242
- };
243
-
244
- exports.Socket = Socket;
package/adapter/web.js DELETED
@@ -1,92 +0,0 @@
1
- const Koa = require('koa');
2
- const { Adapter } = require('./adapter');
3
-
4
- /**
5
- * Web适配器类
6
- * 基于Koa框架的HTTP服务器适配器
7
- */
8
- class Web extends Adapter {
9
- static config = {
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
- super({ ...Web.config, ...config }, parent);
21
- }
22
- }
23
-
24
- /**
25
- * 预置
26
- */
27
- Web.prototype._preset = function () {
28
- this.web = new Koa(this.config);
29
- // 设置cookie签名密钥
30
- this.web.keys = ['mos-secret-key-2024', 'mos-backup-key-2024'];
31
-
32
- // 添加客户端断开处理中间件(必须在最前面)
33
- this.web.use(async (ctx, next) => {
34
- try {
35
- await next();
36
- } catch (err) {
37
- if (err.code === 'ERR_STREAM_PREMATURE_CLOSE') {
38
- // 客户端断开连接,无需处理
39
- return;
40
- }
41
- throw err;
42
- }
43
- });
44
- };
45
-
46
- /**
47
- * 使用中间件
48
- * @param {Function} middleware 中间件函数
49
- */
50
- Web.prototype.use = function (middleware) {
51
- this.web.use(middleware);
52
- };
53
-
54
- /**
55
- * 获取当前中间件处理函数
56
- * @returns {Function} 组合后的中间件函数
57
- */
58
- Web.prototype._getFn = function () {
59
- return this.web.callback();
60
- };
61
-
62
- /**
63
- * 初始化
64
- * @param {object} server 服务器实例
65
- * @returns {object} 适配器实例
66
- */
67
- Web.prototype.init = function (server) {
68
- if (server) {
69
- this.getServer = function () {
70
- return server;
71
- };
72
- }
73
- return this;
74
- };
75
-
76
- /**
77
- * 启动
78
- */
79
- Web.prototype.start = function () {
80
- var { host, port } = this.config;
81
- this.server = this.web.listen(port, host, () => {
82
- this.log('info', `web服务端已启动!访问网址:http://${host}:${port}`);
83
- });
84
-
85
- // 配置服务器超时参数
86
- this.server.timeout = 60000; // 请求超时 60s(默认120s)
87
- this.server.headersTimeout = 65000; // 请求头超时(需大于timeout)
88
- this.server.keepAliveTimeout = 65000; // Keep-Alive 超时
89
- this.server.requestTimeout = 60000; // Node.js 14+ 请求超时
90
- };
91
-
92
- exports.Web = Web;
@@ -1,43 +0,0 @@
1
- const Koa = require('koa');
2
- const websocket = require('koa-websocket');
3
- const { Adapter } = require('./adapter.js');
4
-
5
- /**
6
- * WebSocket适配器
7
- */
8
- class WebSocket extends Adapter {
9
- static config = { host: '0.0.0.0', port: 8300 };
10
- constructor(config, parent) {
11
- super({ ...WebSocket.config, ...config }, parent);
12
- }
13
- }
14
-
15
- /**
16
- * 初始化
17
- * @param {object} web Web适配器实例
18
- */
19
- WebSocket.prototype.init = function (web) {
20
- this._web_adapter = web;
21
-
22
- // 用 koa-websocket 包装真正的 Koa 应用
23
- if (web && web.web) {
24
- this._web = websocket(web.web);
25
- } else if (web) {
26
- this._web = websocket(web);
27
- } else {
28
- this._web = websocket(new Koa());
29
- }
30
-
31
- this.broker = this._web.ws;
32
- return this;
33
- };
34
-
35
- /**
36
- * 注册中间件给 koa-websocket
37
- * @param {Function} middleware 中间件函数
38
- */
39
- WebSocket.prototype.use = function (middleware) {
40
- this.broker.use(middleware);
41
- };
42
-
43
- exports.WebSocket = WebSocket;