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