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
@@ -1,679 +0,0 @@
1
- const Item = require('mm_machine').Drive;
2
-
3
- // 提供一个全局方法容器
4
- if (!$.methods) {
5
- $.methods = {};
6
- }
7
-
8
- /**
9
- * websocket驱动类
10
- * @augments {Item}
11
- * @class
12
- */
13
- class Drive extends Item {
14
- static config = {
15
- // webscoket请求路由地址
16
- path: '',
17
- // 名称
18
- name: '',
19
- // websocket标题
20
- title: '',
21
- // 描述,用来介绍该websocket程序是做什么用的
22
- description: '',
23
- // 同步消息循环发送的时间间隔
24
- interval: 1000,
25
- // 心跳间隔时间, 单位: ms, 默认30秒
26
- ping_interval: 30000,
27
- // 心跳超时时间, 单位: ms, 默认10秒
28
- ping_timeout: 10000,
29
- };
30
- /**
31
- * 构造函数
32
- * @param {object} config 配置参数
33
- * @param {object} parent 父对象
34
- * @class
35
- */
36
- constructor(config, parent) {
37
- super({ ...Drive.config, ...config }, parent);
38
- }
39
- }
40
-
41
- /**
42
- * 获取驱动实例的全局存储键名
43
- * @returns {string} 存储键名
44
- * @private
45
- */
46
- Drive.prototype._getStorageKey = function () {
47
- return 'socket_drive_' + (this.config.name || 'default');
48
- };
49
-
50
- /**
51
- * 确保数据引用有效(热更新后调用)
52
- * @private
53
- */
54
- Drive.prototype._ensureRefs = function () {
55
- var storage_key = this._getStorageKey();
56
-
57
- // 检查全局存储是否存在
58
- if (!$.sockets || !$.sockets[storage_key]) {
59
- // 如果全局存储不存在,重新初始化
60
- this._preset();
61
- return;
62
- }
63
-
64
- // 确保数据引用正确
65
- this._storage = $.sockets[storage_key];
66
-
67
- // 重新建立数据引用
68
- if (!this.clients || this.clients !== this._storage.clients) {
69
- this.clients = this._storage.clients;
70
- }
71
-
72
- if (!this.list_msg || this.list_msg !== this._storage.list_msg) {
73
- this.list_msg = this._storage.list_msg;
74
- }
75
-
76
- if (!this.dict_msg || this.dict_msg !== this._storage.dict_msg) {
77
- this.dict_msg = this._storage.dict_msg;
78
- }
79
- };
80
-
81
- /**
82
- * 预设配置
83
- */
84
- Drive.prototype._preset = function () {
85
- // 开放给前端调用的函数
86
- this.methods = { ...$.methods };
87
-
88
- // 初始化全局存储结构
89
- var storage_key = this._getStorageKey();
90
- if (!$.sockets) {
91
- $.sockets = {};
92
- }
93
-
94
- if (!$.sockets[storage_key]) {
95
- $.sockets[storage_key] = {
96
- clients: {},
97
- list_msg: [],
98
- dict_msg: {},
99
- };
100
- }
101
-
102
- // 使用全局存储的数据
103
- this._storage = $.sockets[storage_key];
104
-
105
- // 保持对全局数据的引用(为了向后兼容)
106
- this.clients = this._storage.clients;
107
- this.list_msg = this._storage.list_msg;
108
- this.dict_msg = this._storage.dict_msg;
109
- };
110
-
111
- /**
112
- * 获取session ID
113
- * @param {object} ctx HTTP上下文
114
- * @returns {string} 返回用户的uuid
115
- */
116
- Drive.prototype.getToken = async function (ctx) {
117
- var hd = ctx.request.header;
118
- var uuid = await ctx.cookies.get('mm:uuid');
119
- if (!uuid) {
120
- uuid = hd[''];
121
- }
122
- if (!uuid) {
123
- var agent = hd['user-agent'];
124
- if (!agent) {
125
- agent = 'mm';
126
- }
127
- var start = agent.md5().substring(0, 32);
128
- var stamp = Date.parse(new Date()) / 1000;
129
- uuid = (ctx.ip + '_' + stamp).aesEncode(start);
130
- }
131
- return uuid;
132
- };
133
-
134
- /**
135
- * 收到消息时处理函数
136
- * @param {string} body_str 消息正文字符串
137
- * @param {object} ctx http上下文
138
- * @param {string} token 临时访问牌
139
- */
140
- Drive.prototype.onMessage = async function (body_str, ctx, token) {
141
- // 解析消息内容
142
- var msg = {};
143
- if (
144
- body_str &&
145
- (body_str.indexOf('[') === 0 || body_str.indexOf('{') === 0)
146
- ) {
147
- try {
148
- msg = JSON.parse(body_str);
149
- } catch (error) {
150
- console.error('Socket消息结构体不对:', error);
151
- }
152
- }
153
-
154
- // 处理回复消息(如果有ID字段)
155
- if (msg && msg.id) {
156
- // 调用驱动的消息回复处理
157
- this._handleResponse(msg);
158
- }
159
-
160
- var ret = await this.run(body_str, ctx, token);
161
- if (ret) {
162
- var ws = ctx.websocket;
163
- if (typeof ret === 'object') {
164
- ws.send(JSON.stringify(ret));
165
- } else {
166
- ws.send(ret);
167
- }
168
- }
169
- };
170
-
171
- /**
172
- * 状态变更通知
173
- * @param {string} type 通知类型
174
- * @param {string} body_str 消息正文字符串
175
- * @param {object} ctx http上下文
176
- * @param {string} token 临时访问牌
177
- * @returns {boolean} 返回true表示做状态修改, 例如关闭时为true, 会删除该客户端
178
- */
179
- Drive.prototype.noticy = async function (type, body_str, ctx, token) {
180
- // this.log('debug', '通知:', '关闭了');
181
- return true;
182
- };
183
-
184
- /**
185
- * 关闭连接时处理函数
186
- * @param {string} body_str 消息正文字符串
187
- * @param {object} ctx http上下文
188
- * @param {string} token 临时访问牌
189
- */
190
- Drive.prototype.onClose = async function (body_str, ctx, token) {
191
- var del = await this.noticy('close', ctx, token);
192
- if (del) {
193
- var lt = this.clients[token];
194
- if (lt) {
195
- var index = lt.indexOf(ctx);
196
- if (index !== -1) {
197
- lt.splice(index, 1);
198
- }
199
- // 当该 token 下没有连接时,清理空数组
200
- if (lt.length === 0) {
201
- delete this.clients[token];
202
- }
203
- }
204
- // 关闭WebSocket连接并清理事件监听器
205
- if (ctx.websocket) {
206
- try {
207
- ctx.websocket.removeAllListeners();
208
- ctx.websocket.close();
209
- } catch (e) {
210
- // 忽略关闭错误
211
- }
212
- }
213
- }
214
- };
215
-
216
- /**
217
- * 设置websocket
218
- * @param {object} ctx http上下文
219
- * @param {string} token 临时访问牌
220
- */
221
- Drive.prototype.setSocket = function (ctx, token) {
222
- var ws = ctx.websocket;
223
-
224
- // 增加消息队列和索引
225
- ws.list_msg = [];
226
- ws.dict_msg = {};
227
-
228
- /**
229
- * 设置发送请求
230
- * @param {string} method 方法名称
231
- * @param {object} params 请求参数
232
- * @param {Function} func 回调函数
233
- */
234
- var _this = this;
235
- ws.req = async function (method, params, func) {
236
- var key = _this.config.name + '';
237
- var data = {
238
- id: key + new Date().getTime() + Math.random(),
239
- method: method,
240
- params: params,
241
- };
242
- this.send(JSON.stringify(data));
243
-
244
- if (func) {
245
- data.func = func;
246
- this.list_msg.push(data);
247
- this.dict_msg[data.id] = data;
248
- }
249
- };
250
-
251
- // 设置事件 —— 获取消息时和socket关闭时
252
- ws.on('message', async (body_str) => {
253
- await _this.onMessage(body_str, ctx, token);
254
- });
255
-
256
- ws.on('close', async (body_str) => {
257
- await _this.onClose(body_str, ctx, token);
258
- });
259
-
260
- // 设置心跳检测, 防止死连接
261
- if (this.config.ping_interval > 0) {
262
- ws._ping_timer = setInterval(() => {
263
- if (ws.readyState !== 1) {
264
- clearInterval(ws._ping_timer);
265
- return;
266
- }
267
- ws.ping();
268
- ws._pong_timeout = setTimeout(() => {
269
- clearInterval(ws._ping_timer);
270
- ws.terminate();
271
- }, this.config.ping_timeout);
272
- }, this.config.ping_interval);
273
-
274
- ws.on('pong', () => {
275
- if (ws._pong_timeout) {
276
- clearTimeout(ws._pong_timeout);
277
- ws._pong_timeout = null;
278
- }
279
- });
280
- }
281
- };
282
-
283
- /**
284
- * 握手成功, 发送首条返回内容
285
- * @param {object} ctx http 上下文
286
- * @param {string} token 临时访问牌
287
- */
288
- Drive.prototype.success = function (ctx, token) {
289
- var ret = $.ret.bl(true, 'connection succeeded');
290
- // 首次响应加上身份牌
291
- ret.result.token = token;
292
- // ID为0表示连接成功
293
- ret.id = 0;
294
- ctx.websocket.send(JSON.stringify(ret));
295
- };
296
-
297
- /**
298
- * 添加客户端
299
- * @param {object} ctx 请求上下文
300
- */
301
- Drive.prototype.add = async function (ctx) {
302
- var token = await this.getToken(ctx);
303
- if (!this.clients[token]) {
304
- this.clients[token] = [];
305
- }
306
- this.setSocket(ctx, token);
307
- this.success(ctx, token);
308
- this.clients[token].push(ctx);
309
- };
310
-
311
- /**
312
- * 发送消息到指定客户端
313
- * @param {string} token 客户端token
314
- * @param {string} method 方法名称
315
- * @param {object} params 请求参数
316
- * @returns {boolean} 发送是否成功
317
- */
318
- Drive.prototype.send = function (token, method, params) {
319
- // 确保数据引用有效(热更新保护)
320
- this._ensureRefs();
321
-
322
- var list = this.clients[token];
323
- if (list && list.length > 0) {
324
- var data = {
325
- method: method,
326
- params: params,
327
- };
328
-
329
- for (var i = 0; i < list.length; i++) {
330
- var ctx = list[i];
331
- var ws = ctx.websocket;
332
- if (ws && ws.readyState === 1) {
333
- // WebSocket.OPEN
334
- ws.send(JSON.stringify(data));
335
- }
336
- }
337
- return true;
338
- }
339
- return false;
340
- };
341
-
342
- /**
343
- * 发送消息到所有连接的客户端
344
- * @param {string} method 方法名称
345
- * @param {object} params 请求参数
346
- */
347
- Drive.prototype.sendAll = function (method, params) {
348
- // 确保数据引用有效(热更新保护)
349
- this._ensureRefs();
350
-
351
- var data = {
352
- method: method,
353
- params: params,
354
- };
355
-
356
- for (var token in this.clients) {
357
- var list = this.clients[token];
358
- for (var i = 0; i < list.length; i++) {
359
- var ctx = list[i];
360
- var ws = ctx.websocket;
361
- if (ws && ws.readyState === 1) {
362
- // WebSocket.OPEN
363
- ws.send(JSON.stringify(data));
364
- }
365
- }
366
- }
367
- };
368
-
369
- /**
370
- * 发送请求到指定客户端
371
- * @param {string} token 客户端token
372
- * @param {string} method 方法名称
373
- * @param {object} params 请求参数
374
- * @param {Function} func 回调函数
375
- * @param {number} timeout 超时时间(毫秒)
376
- * @returns {boolean} 发送是否成功
377
- */
378
- Drive.prototype.req = function (token, method, params, func, timeout = 0) {
379
- // 确保数据引用有效(热更新保护)
380
- this._ensureRefs();
381
-
382
- var list = this.clients[token];
383
- if (list && list.length > 0) {
384
- var data = {
385
- id: this._genMsgId(),
386
- method: method,
387
- params: params,
388
- };
389
-
390
- if (func) {
391
- data.func = func;
392
- data.timestamp = Date.now();
393
- data.timeout = timeout || 3000;
394
-
395
- // 将消息添加到驱动的消息队列
396
- this.list_msg.push(data);
397
- this.dict_msg[data.id] = data;
398
-
399
- // 设置超时移除定时器
400
- var _this = this;
401
- data.timer = setTimeout(() => {
402
- _this._delMsgById(data.id);
403
- }, data.timeout);
404
- }
405
-
406
- for (var i = 0; i < list.length; i++) {
407
- var ctx = list[i];
408
- var ws = ctx.websocket;
409
- if (ws && ws.readyState === 1) {
410
- // WebSocket.OPEN
411
- ws.send(JSON.stringify(data));
412
- }
413
- }
414
- return true;
415
- }
416
- return false;
417
- };
418
-
419
- /**
420
- * 发送请求到所有连接的客户端
421
- * @param {string} method 方法名称
422
- * @param {object} params 请求参数
423
- * @param {Function} func 回调函数
424
- * @param {number} timeout 超时时间(毫秒)
425
- */
426
- Drive.prototype.reqAll = function (method, params, func, timeout = 0) {
427
- // 确保数据引用有效(热更新保护)
428
- this._ensureRefs();
429
-
430
- var data = {
431
- id: this._genMsgId(),
432
- method: method,
433
- params: params,
434
- };
435
-
436
- if (func) {
437
- data.func = func;
438
- data.timestamp = Date.now();
439
- data.timeout = timeout || 3000;
440
-
441
- // 将消息添加到驱动的消息队列
442
- this.list_msg.push(data);
443
- this.dict_msg[data.id] = data;
444
-
445
- // 设置超时移除定时器
446
- var _this = this;
447
- data.timer = setTimeout(() => {
448
- _this._delMsgById(data.id);
449
- }, data.timeout);
450
- }
451
-
452
- for (var token in this.clients) {
453
- var list = this.clients[token];
454
- for (var i = 0; i < list.length; i++) {
455
- var ctx = list[i];
456
- var ws = ctx.websocket;
457
- if (ws && ws.readyState === 1) {
458
- // WebSocket.OPEN
459
- ws.send(JSON.stringify(data));
460
- }
461
- }
462
- }
463
- };
464
-
465
- /**
466
- * 生成消息ID
467
- * @returns {string} 消息ID
468
- * @private
469
- */
470
- Drive.prototype._genMsgId = function () {
471
- return (
472
- 'drive_' +
473
- Date.parse(new Date()) +
474
- '_' +
475
- Math.random().toString(36).substr(2, 9)
476
- );
477
- };
478
-
479
- /**
480
- * 根据ID移除消息
481
- * @param {string} msg_id 消息ID
482
- * @private
483
- */
484
- Drive.prototype._delMsgById = function (msg_id) {
485
- // 确保数据引用有效(热更新保护)
486
- this._ensureRefs();
487
-
488
- var o = this.dict_msg[msg_id];
489
- if (!o) {
490
- return;
491
- }
492
- // 清除定时器
493
- if (o.timer) {
494
- clearTimeout(o.timer);
495
- }
496
- delete this.dict_msg[msg_id];
497
-
498
- // 从数组中移除 (保持list_msg与dict_msg同步)
499
- for (var i = 0; i < this.list_msg.length; i++) {
500
- if (this.list_msg[i].id === msg_id) {
501
- this.list_msg.splice(i, 1);
502
- break;
503
- }
504
- }
505
- };
506
-
507
- /**
508
- * 处理消息回复
509
- * @param {object} msg 回复消息
510
- * @private
511
- */
512
- Drive.prototype._handleResponse = function (msg) {
513
- // 确保数据引用有效(热更新保护)
514
- this._ensureRefs();
515
-
516
- if (!msg || !msg.id) {
517
- return;
518
- }
519
-
520
- var o = this.dict_msg[msg.id];
521
- if (o && o.func) {
522
- try {
523
- // 执行回调函数
524
- o.func(msg);
525
- } catch (error) {
526
- console.error('Socket驱动回调函数执行错误:', error);
527
- }
528
-
529
- // 从列表中移除已处理的消息
530
- this._delMsgById(o.id);
531
- }
532
- };
533
-
534
- /**
535
- * 执行
536
- * @param {string} body 正文字符串
537
- * @param {object} ctx 请求上下文
538
- * @param {string} token 临时访问牌
539
- * @returns {object} 返回执行结果
540
- */
541
- Drive.prototype.run = async function (body, ctx, token) {
542
- try {
543
- var by = body.toString();
544
- var ws = ctx.websocket;
545
- var json = by.toJson();
546
- var req = ctx.request;
547
- var request = { headers: req.headers, query: req.query, token: token };
548
- if (!json) {
549
- return await this.main(by, ws, request);
550
- }
551
- return await this._handleJson(json, ws, request);
552
- } catch (err) {
553
- this.log('error', 'webscoket 错误', err);
554
- return $.ret.error(10000, '代码错误!原因:' + err.toString());
555
- }
556
- };
557
-
558
- /**
559
- * 处理 JSON 请求
560
- * @param {object} json JSON 对象
561
- * @param {object} ws WebSocket
562
- * @param {object} request 请求对象
563
- * @returns {object} 返回处理结果
564
- * @private
565
- */
566
- Drive.prototype._handleJson = async function (json, ws, request) {
567
- var { id, method } = json;
568
- if (json.result && id) {
569
- return await this._handleRet(json, ws);
570
- }
571
- if (method && this.methods[method]) {
572
- return await this._callMethod(json, ws, request);
573
- }
574
- return await this.main(json, ws, request);
575
- };
576
-
577
- /**
578
- * 处理结果回调
579
- * @param {object} json JSON 对象
580
- * @param {object} ws WebSocket
581
- * @private
582
- */
583
- /* eslint-disable no-param-reassign */
584
- Drive.prototype._handleRet = async function (json, ws) {
585
- var id = json.id;
586
- var o = ws.dict_msg[id];
587
- if (o) {
588
- await o.func(json.result);
589
- delete ws.dict_msg[id];
590
- var lt = ws.list_msg;
591
- for (var i = 0; i < lt.length; i++) {
592
- if (id === lt[i].id) {
593
- lt.splice(i, 1);
594
- return;
595
- }
596
- }
597
- }
598
- };
599
- /* eslint-enable no-param-reassign */
600
-
601
- /**
602
- * 调用方法
603
- * @param {object} json JSON 对象
604
- * @param {object} ws WebSocket
605
- * @param {object} request 请求对象
606
- * @returns {object} 返回调用结果
607
- * @private
608
- */
609
- Drive.prototype._callMethod = async function (json, ws, request) {
610
- var { id, method } = json;
611
- var result = await this.methods[method](json.params, ws, request);
612
- if (!result) {
613
- return;
614
- }
615
- if (typeof result == 'object' && !Array.isArray(result)) {
616
- return { id, ...result };
617
- }
618
- var ret = { result };
619
- if (id) {
620
- ret.id = id;
621
- }
622
- return ret;
623
- };
624
-
625
- /**
626
- * 加载完成时
627
- */
628
- Drive.prototype.loadAfter = function () {
629
- var m = this.methods;
630
-
631
- /**
632
- * 获取所有方法
633
- * @param {object} params 参数
634
- * @param {object} ws Websocket服务
635
- * @param {object} request 请求协议头
636
- * @returns {object} 返回执行结果
637
- */
638
- m.getMethods = async function (params, ws, request) {
639
- return Object.keys(m);
640
- };
641
- };
642
-
643
- /**
644
- * 获取插件
645
- * @param {string} app 应用名称
646
- * @param {string} name 插件名称
647
- * @returns {object} 返回获取到的插件
648
- */
649
- Drive.prototype.plugin = function (app, name) {
650
- var l = $.slash;
651
- var app_name = app || this.config_file.between('app' + l, l);
652
- var plugin_name = name || this.config_file.between('plugin' + l, l);
653
- var plus;
654
- var plugins = $.pool.plugin[app_name];
655
- if (plugins) {
656
- plus = plugins.get(plugin_name);
657
- }
658
- return plus;
659
- };
660
-
661
- /**
662
- * 获取模型
663
- * @param {string} type 模型类型
664
- * @returns {object} 返回获取到的模型
665
- */
666
- Drive.prototype.getModel = function (type) {
667
- let model = { ...this.config };
668
- let dir = this.getDir();
669
- let l = $.slash;
670
- let app_name = dir.between('app' + l, l);
671
- let plugin_name = dir.between('plugin' + l, l);
672
- let name = dir.basename();
673
- model.app = app_name;
674
- model.plugin = plugin_name;
675
- model.name = model.name || name;
676
- return model;
677
- };
678
-
679
- module.exports = Drive;