mm_os 4.2.6 → 4.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/README_EN.md +2 -2
- package/adapter/adapter.js +105 -52
- package/adapter/custom_persistence.js +32 -31
- package/adapter/mqtt.js +59 -79
- package/adapter/socket.js +188 -58
- package/adapter/web.js +92 -68
- package/adapter/websocket.js +43 -146
- package/com/api/drive.js +732 -708
- package/com/api/index.js +196 -198
- package/com/cmd/config.tpl.json +48 -122
- package/com/cmd/drive.js +176 -1548
- package/com/cmd/index.js +287 -1066
- package/com/cmd/job/config.json +66 -0
- package/com/cmd/job/index.js +241 -0
- package/com/cmd/job/test.js +188 -0
- package/com/cmd/script.tpl.js +12 -33
- package/com/db/drive.js +2206 -2019
- package/com/db/index.js +241 -242
- package/com/event/config.tpl.json +3 -1
- package/com/event/drive.js +3 -1
- package/com/event/index.js +73 -49
- package/com/event/script.tpl.js +1 -1
- package/com/mqtt/config.tpl.json +16 -8
- package/com/mqtt/drive.js +696 -676
- package/com/mqtt/index.js +1012 -822
- package/com/mqtt/index.test.js +503 -0
- package/com/mqtt/mm_mqtt.js +446 -420
- package/com/mqtt/script.tpl.js +3 -7
- package/com/nav/drive.js +7 -7
- package/com/nav/index.js +228 -231
- package/com/param/index.js +19 -17
- package/com/pendant/index.js +467 -441
- package/com/socket/drive.js +679 -616
- package/com/socket/index.js +94 -48
- package/com/sql/drive.js +1283 -1259
- package/com/sql/index.js +152 -150
- package/com/static/drive.js +1 -1
- package/com/static/index.js +232 -226
- package/com/task/drive.js +428 -405
- package/com/task/index.js +154 -148
- package/com/template/drive.js +1 -1
- package/com/template/index.js +141 -138
- package/com.js +87 -6
- package/common/middleware/web_after/index.js +4 -10
- package/common/middleware/web_base/index.js +1 -1
- package/common/middleware/web_cors/index.js +1 -1
- package/common/middleware/web_error/index.js +131 -119
- package/common/middleware/web_error/middleware.json +2 -1
- package/common/middleware/web_logger/index.js +1 -1
- package/common/middleware/web_main/index.js +2 -0
- package/common/middleware/web_static/index.js +2 -2
- package/common/middleware/web_waf/index.js +1 -1
- package/core/app/config.tpl.json +4 -4
- package/core/app/index.js +710 -534
- package/core/middleware/index.js +1 -9
- package/core/mod/config.tpl.json +1 -1
- package/core/model/config.tpl.json +1 -1
- package/core/plugin/config.tpl.json +15 -3
- package/core/plugin/index.js +31 -0
- package/core/pusher/index.js +158 -161
- package/core/scene/loop.js +1306 -1255
- package/core/store/index.js +1794 -1755
- package/core/store/sql.js +1488 -1464
- package/core/zone/bat/index.js +744 -728
- package/core/zone/config.tpl.json +3 -3
- package/core/zone/index.js +655 -616
- package/os.js +0 -4
- package/package.json +16 -16
- package/server.js +583 -427
- package/com/cmd/nlp.js +0 -525
- package/core/app/index copy.js +0 -611
- package/software.js +0 -194
- package/tcp.js +0 -279
package/server.js
CHANGED
|
@@ -1,427 +1,583 @@
|
|
|
1
|
-
require('mm_expand');
|
|
2
|
-
const {
|
|
3
|
-
|
|
4
|
-
} = require('
|
|
5
|
-
const {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const {
|
|
9
|
-
|
|
10
|
-
} = require('
|
|
11
|
-
const {
|
|
12
|
-
|
|
13
|
-
} = require('
|
|
14
|
-
|
|
15
|
-
const {
|
|
16
|
-
const {
|
|
17
|
-
|
|
18
|
-
const {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
connect_timeout: 5000,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
//
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
//
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
//
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
*
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
*
|
|
398
|
-
*/
|
|
399
|
-
Server.prototype.
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
1
|
+
require('mm_expand');
|
|
2
|
+
const { Log } = require('mm_logs');
|
|
3
|
+
const { cacheAdmin } = require('mm_cache');
|
|
4
|
+
const { sqlAdmin } = require('mm_sql');
|
|
5
|
+
const { Config } = require('mm_config');
|
|
6
|
+
const { Drive, Manager } = require('mm_machine');
|
|
7
|
+
|
|
8
|
+
const { Web } = require('./adapter/web.js');
|
|
9
|
+
const { WebSocket } = require('./adapter/websocket.js');
|
|
10
|
+
const { Socket } = require('./adapter/socket.js');
|
|
11
|
+
const { Mqtt } = require('./adapter/mqtt.js');
|
|
12
|
+
|
|
13
|
+
const { Middleware } = require('./core/middleware/index.js');
|
|
14
|
+
const { Game } = require('./core/game/index.js');
|
|
15
|
+
const { App } = require('./core/app/index.js');
|
|
16
|
+
const { Mod } = require('./core/mod/index.js');
|
|
17
|
+
|
|
18
|
+
const { Com } = require('./com.js');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 服务器类
|
|
22
|
+
* 负责管理整个应用的服务端架构
|
|
23
|
+
*/
|
|
24
|
+
class Server extends Drive {
|
|
25
|
+
static config = {
|
|
26
|
+
name: 'os',
|
|
27
|
+
title: 'mm服务端系统',
|
|
28
|
+
log: {
|
|
29
|
+
level: 'debug',
|
|
30
|
+
file: false,
|
|
31
|
+
},
|
|
32
|
+
sys: {
|
|
33
|
+
// 服务端名称
|
|
34
|
+
name: 'mm',
|
|
35
|
+
// 服务端中文名
|
|
36
|
+
title: '超级美眉',
|
|
37
|
+
// 系统使用的语言
|
|
38
|
+
lang: 'zh_CN',
|
|
39
|
+
// 定时任务
|
|
40
|
+
task: false,
|
|
41
|
+
// 是否启用AI功能
|
|
42
|
+
ai: false,
|
|
43
|
+
},
|
|
44
|
+
mqtt: {
|
|
45
|
+
hostname: '127.0.0.1',
|
|
46
|
+
port: 1883,
|
|
47
|
+
state: true,
|
|
48
|
+
auth_enabled: true,
|
|
49
|
+
auth_users: [
|
|
50
|
+
// {
|
|
51
|
+
// clientid: 'server',
|
|
52
|
+
// username: 'admin',
|
|
53
|
+
// password: 'asd123'
|
|
54
|
+
// }
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
mqtt_client: {
|
|
58
|
+
state: true,
|
|
59
|
+
hostname: '127.0.0.1',
|
|
60
|
+
port: 1883,
|
|
61
|
+
protocol: 'mqtt',
|
|
62
|
+
client_id: 'server',
|
|
63
|
+
username: 'admin',
|
|
64
|
+
password: 'asd123',
|
|
65
|
+
clean: false,
|
|
66
|
+
},
|
|
67
|
+
socket: {
|
|
68
|
+
host: '0.0.0.0',
|
|
69
|
+
port: 8200,
|
|
70
|
+
state: false,
|
|
71
|
+
},
|
|
72
|
+
sql: {
|
|
73
|
+
// mysql、sqlite
|
|
74
|
+
way: 'mysql',
|
|
75
|
+
},
|
|
76
|
+
sqlite: {
|
|
77
|
+
dir: './db/'.fullname(),
|
|
78
|
+
user: 'root',
|
|
79
|
+
password: '',
|
|
80
|
+
database: 'mm',
|
|
81
|
+
charset: 'utf8mb4',
|
|
82
|
+
connect_timeout: 5000,
|
|
83
|
+
connect_limit: 500,
|
|
84
|
+
},
|
|
85
|
+
mysql: {
|
|
86
|
+
host: '127.0.0.1',
|
|
87
|
+
port: 3306,
|
|
88
|
+
user: 'root',
|
|
89
|
+
password: 'Asd159357',
|
|
90
|
+
database: '',
|
|
91
|
+
charset: 'utf8mb4',
|
|
92
|
+
timezone: '+08:00',
|
|
93
|
+
connect_timeout: 5000,
|
|
94
|
+
connect_limit: 500,
|
|
95
|
+
},
|
|
96
|
+
cache: {
|
|
97
|
+
// 缓存方式memory、redis、mongodb
|
|
98
|
+
way: 'redis',
|
|
99
|
+
},
|
|
100
|
+
// 缓存基础配置
|
|
101
|
+
cachebase: {
|
|
102
|
+
dir: './cache/'.fullname(),
|
|
103
|
+
expire: 0,
|
|
104
|
+
max: 0,
|
|
105
|
+
persist: false, // 是否启用持久化
|
|
106
|
+
save_interval: 60000, // 保存间隔(毫秒)
|
|
107
|
+
},
|
|
108
|
+
// 缓存配置
|
|
109
|
+
redis: {
|
|
110
|
+
// 服务器地址
|
|
111
|
+
host: 'localhost',
|
|
112
|
+
// 端口号
|
|
113
|
+
port: 6379,
|
|
114
|
+
// 密码
|
|
115
|
+
password: 'asd159357',
|
|
116
|
+
// 数据库
|
|
117
|
+
database: 0,
|
|
118
|
+
// 前缀
|
|
119
|
+
prefix: 'mm_',
|
|
120
|
+
// 连接超时时间(ms)
|
|
121
|
+
connect_timeout: 5000,
|
|
122
|
+
// 重试次数
|
|
123
|
+
retry_count: 3,
|
|
124
|
+
// 重试间隔(ms)
|
|
125
|
+
retry_delay: 1000,
|
|
126
|
+
},
|
|
127
|
+
// 数据库配置
|
|
128
|
+
mongodb: {
|
|
129
|
+
// 服务器地址
|
|
130
|
+
host: 'localhost',
|
|
131
|
+
// 端口号
|
|
132
|
+
port: 27017,
|
|
133
|
+
// 数据库名
|
|
134
|
+
database: 'mm',
|
|
135
|
+
// 用户名
|
|
136
|
+
user: '',
|
|
137
|
+
// 密码
|
|
138
|
+
password: '',
|
|
139
|
+
// 连接选项 (MongoDB驱动7.x版本支持的参数)
|
|
140
|
+
min_pool_size: 10,
|
|
141
|
+
max_pool_size: 100,
|
|
142
|
+
connect_timeout_ms: 10000,
|
|
143
|
+
server_selection_timeout_ms: 5000,
|
|
144
|
+
retry_writes: true,
|
|
145
|
+
},
|
|
146
|
+
web: {
|
|
147
|
+
state: 1,
|
|
148
|
+
// 访问地址
|
|
149
|
+
host: '127.0.0.1',
|
|
150
|
+
// 访问端口号
|
|
151
|
+
port: 8000,
|
|
152
|
+
// 是否启用websocket
|
|
153
|
+
socket: false,
|
|
154
|
+
// 是否启用压缩
|
|
155
|
+
compress: false,
|
|
156
|
+
// 是否启用事件
|
|
157
|
+
event: true,
|
|
158
|
+
// 是否启用事件
|
|
159
|
+
log: true,
|
|
160
|
+
// 跨域
|
|
161
|
+
cos: {
|
|
162
|
+
status: true,
|
|
163
|
+
origin: '*',
|
|
164
|
+
// "headers": "Content-Type,Content-Length,Authorization,Accept,X-Requested-With,x-auth-token,token,clientid,appid,apikey,user_id,x-forwarded-for,x-real-ip,User-Agent,cache-control,pragma,accept-encoding,connection,host"
|
|
165
|
+
},
|
|
166
|
+
// 代理转发
|
|
167
|
+
proxy: {},
|
|
168
|
+
// static 配置
|
|
169
|
+
static: {
|
|
170
|
+
// 是否开启静态文件处理
|
|
171
|
+
state: 1,
|
|
172
|
+
// 前端缓存时间,单位秒
|
|
173
|
+
max_age: 7200,
|
|
174
|
+
// 是否开启缓存
|
|
175
|
+
cache: true,
|
|
176
|
+
// 后端缓存时间,单位秒
|
|
177
|
+
cache_age: 0,
|
|
178
|
+
// 是否开启immutable缓存
|
|
179
|
+
immutable: true,
|
|
180
|
+
// 是否开启隐藏文件处理
|
|
181
|
+
hidden: false,
|
|
182
|
+
// 是否格式化文件
|
|
183
|
+
format: true,
|
|
184
|
+
// 是否指定文件扩展名
|
|
185
|
+
extensions: false,
|
|
186
|
+
// 是否开启brotli压缩
|
|
187
|
+
brotli: false,
|
|
188
|
+
// 是否开启gzip压缩
|
|
189
|
+
gzip: false,
|
|
190
|
+
// 静态文件根目录
|
|
191
|
+
root: './static',
|
|
192
|
+
// 编译vue文件,启动后会将vue转为js,可让前端通过 import xxx from './xxx.vue' 引入
|
|
193
|
+
compile_vue: true,
|
|
194
|
+
// 编译markdown文件,启动后会将markdown转为html
|
|
195
|
+
compile_md: true,
|
|
196
|
+
// 是否将files中的文件的js转换为amd格式
|
|
197
|
+
convert_amd: false,
|
|
198
|
+
// 指定路径文件需要转换
|
|
199
|
+
src_path: '/src',
|
|
200
|
+
// 需要转换的文件扩展名
|
|
201
|
+
files: ['.js', '.vue', '.html'],
|
|
202
|
+
// 是否启用文件修改监听(实时检测文件修改)
|
|
203
|
+
watch: true,
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 构造函数
|
|
210
|
+
* @param {object} config 配置参数
|
|
211
|
+
* @param parent
|
|
212
|
+
*/
|
|
213
|
+
constructor(config, parent) {
|
|
214
|
+
super(config, parent);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 预置
|
|
220
|
+
*/
|
|
221
|
+
Server.prototype._preset = function () {
|
|
222
|
+
// Server不加载脚本文件,清除从配置继承的main与script_file
|
|
223
|
+
this.config.main = '';
|
|
224
|
+
this.script_file = '';
|
|
225
|
+
|
|
226
|
+
/** == 管理层 == */
|
|
227
|
+
// 管理器
|
|
228
|
+
this.manager = {};
|
|
229
|
+
// 通知器
|
|
230
|
+
this.notifier = {};
|
|
231
|
+
// 消息发送器
|
|
232
|
+
this.sender = {};
|
|
233
|
+
// 广播器
|
|
234
|
+
this.pusher = {};
|
|
235
|
+
|
|
236
|
+
/** == 应用层 == */
|
|
237
|
+
// 适配器
|
|
238
|
+
this.adapter = {};
|
|
239
|
+
// 中间件
|
|
240
|
+
this.middleware = {};
|
|
241
|
+
// 应用
|
|
242
|
+
this.app = {};
|
|
243
|
+
// 模组,用于功能/玩法扩展
|
|
244
|
+
this.mod = {};
|
|
245
|
+
// 游戏
|
|
246
|
+
this.game = {};
|
|
247
|
+
|
|
248
|
+
/** == 基础设施 == */
|
|
249
|
+
// 日志
|
|
250
|
+
this.setLogger($.log || console);
|
|
251
|
+
// 数据库
|
|
252
|
+
this.sql = null;
|
|
253
|
+
// 缓存
|
|
254
|
+
this.cache = null;
|
|
255
|
+
// 文件管理
|
|
256
|
+
this.filer = null;
|
|
257
|
+
// 初始化工具
|
|
258
|
+
this._createTool();
|
|
259
|
+
// 初始化基础设施
|
|
260
|
+
this._createBase();
|
|
261
|
+
// 创建适配器
|
|
262
|
+
this._createAdapter();
|
|
263
|
+
// 使用统一处理错误
|
|
264
|
+
this._useError();
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* 获取管理器
|
|
269
|
+
* @returns {object} 管理器实例
|
|
270
|
+
*/
|
|
271
|
+
Server.prototype.getManager = async function () {
|
|
272
|
+
return this.manager;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 加载核心模块
|
|
277
|
+
*/
|
|
278
|
+
Server.prototype._loadCore = async function () {
|
|
279
|
+
await this._initManager();
|
|
280
|
+
let arr = [this._loadSources()];
|
|
281
|
+
if (this.config.sys.ai) {
|
|
282
|
+
var { initAI } = require('./ai.js'.fullname(__dirname));
|
|
283
|
+
arr.push(initAI(this));
|
|
284
|
+
}
|
|
285
|
+
await Promise.all(arr);
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* 创建管理器实例
|
|
290
|
+
* @param {string} name 管理器名称
|
|
291
|
+
* @param {string} title 管理器标题
|
|
292
|
+
* @param {Function} cls 管理器类
|
|
293
|
+
* @returns {object} 管理器实例
|
|
294
|
+
*/
|
|
295
|
+
Server.prototype._createManager = function (name, title, cls) {
|
|
296
|
+
var manager = new Manager(
|
|
297
|
+
{
|
|
298
|
+
name: name,
|
|
299
|
+
title: title,
|
|
300
|
+
filename: name + '.json',
|
|
301
|
+
tpl_dir: `./core/${name}/`.fullname(__dirname),
|
|
302
|
+
base_dir: '',
|
|
303
|
+
dir: `./${name}`.fullname(),
|
|
304
|
+
},
|
|
305
|
+
this,
|
|
306
|
+
this[name],
|
|
307
|
+
cls,
|
|
308
|
+
);
|
|
309
|
+
// 设置到对应的管理器属性
|
|
310
|
+
this.manager[name] = manager;
|
|
311
|
+
return manager;
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
Server.prototype._initMiddlewareManager = async function () {
|
|
315
|
+
var manager = new Manager(
|
|
316
|
+
{
|
|
317
|
+
name: 'middleware',
|
|
318
|
+
title: '中间件',
|
|
319
|
+
filename: 'middleware.json',
|
|
320
|
+
tpl_dir: `./core/middleware/`.fullname(__dirname),
|
|
321
|
+
base_dir: './common/middleware'.fullname(__dirname),
|
|
322
|
+
dir: `/middleware`.fullname(),
|
|
323
|
+
},
|
|
324
|
+
this,
|
|
325
|
+
this.middleware,
|
|
326
|
+
Middleware,
|
|
327
|
+
);
|
|
328
|
+
this.manager.middleware = manager;
|
|
329
|
+
return manager;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* 初始化管理器
|
|
334
|
+
*/
|
|
335
|
+
Server.prototype._initManager = async function () {
|
|
336
|
+
// 管理器配置列表
|
|
337
|
+
var infos = [
|
|
338
|
+
{ name: 'game', title: '游戏', Module: Game },
|
|
339
|
+
{ name: 'app', title: '应用', Module: App },
|
|
340
|
+
{ name: 'mod', title: '模组', Module: Mod },
|
|
341
|
+
];
|
|
342
|
+
|
|
343
|
+
// 管理器实例集合
|
|
344
|
+
var promises = [];
|
|
345
|
+
|
|
346
|
+
// 使用for循环初始化所有管理器
|
|
347
|
+
for (var i = 0; i < infos.length; i++) {
|
|
348
|
+
var config = infos[i];
|
|
349
|
+
var manager = this._createManager(config.name, config.title, config.Module);
|
|
350
|
+
promises.push(manager.do('load'));
|
|
351
|
+
}
|
|
352
|
+
// 初始化中间件管理器
|
|
353
|
+
var midd = await this._initMiddlewareManager();
|
|
354
|
+
promises.push(midd.do('load'));
|
|
355
|
+
await Promise.all(promises);
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* 加载核心模块
|
|
360
|
+
*/
|
|
361
|
+
Server.prototype._loadSources = async function () {
|
|
362
|
+
let manager = await this.getManager();
|
|
363
|
+
await Promise.all([
|
|
364
|
+
manager.game.runAll('load'),
|
|
365
|
+
manager.app.runAll('load'),
|
|
366
|
+
manager.mod.runAll('load'),
|
|
367
|
+
manager.middleware.runAll('load'),
|
|
368
|
+
]);
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 初始化全局错误处理机制
|
|
373
|
+
*/
|
|
374
|
+
Server.prototype._useError = function () {
|
|
375
|
+
var _this = this;
|
|
376
|
+
|
|
377
|
+
// 全局未捕获异常处理
|
|
378
|
+
process.on('uncaughtException', (error) => {
|
|
379
|
+
// ERR_STREAM_PREMATURE_CLOSE 是客户端断开连接导致的,无需记录
|
|
380
|
+
if (error && error.code === 'ERR_STREAM_PREMATURE_CLOSE') {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
_this.log('error', '全局未捕获异常:', error);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
// 全局未处理Promise拒绝处理
|
|
387
|
+
process.on('unhandledRejection', (reason) => {
|
|
388
|
+
// ERR_STREAM_PREMATURE_CLOSE 同样忽略
|
|
389
|
+
if (reason && reason.code === 'ERR_STREAM_PREMATURE_CLOSE') {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
_this.log('error', '全局未处理Promise拒绝:', reason);
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* 初始化
|
|
398
|
+
*/
|
|
399
|
+
Server.prototype._initCore = async function () {
|
|
400
|
+
// 初始化管理器
|
|
401
|
+
await this._initSources();
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* 初始化核心模块
|
|
406
|
+
*/
|
|
407
|
+
Server.prototype._initSources = async function () {
|
|
408
|
+
let manager = await this.getManager();
|
|
409
|
+
await Promise.all([
|
|
410
|
+
manager.game.runAll('init', this, this.getEventer(), this.getLogger()),
|
|
411
|
+
manager.app.runAll('init', this, this.getEventer(), this.getLogger()),
|
|
412
|
+
manager.mod.runAll('init', this, this.getEventer(), this.getLogger()),
|
|
413
|
+
manager.middleware.runWait(
|
|
414
|
+
'init',
|
|
415
|
+
this.adapter,
|
|
416
|
+
this.getEventer(),
|
|
417
|
+
this.getLogger(),
|
|
418
|
+
),
|
|
419
|
+
]);
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* 初始化基础设施
|
|
424
|
+
*/
|
|
425
|
+
Server.prototype._createBase = function () {
|
|
426
|
+
// 获取数据库配置
|
|
427
|
+
var sql = { type: 'mysql', ...this.config.sql };
|
|
428
|
+
if (this.config.mysql) {
|
|
429
|
+
sql.mysql = this.config.mysql;
|
|
430
|
+
}
|
|
431
|
+
if (this.config.sqlite) {
|
|
432
|
+
sql.sqlite = this.config.sqlite;
|
|
433
|
+
}
|
|
434
|
+
$.sqlAdmin = sqlAdmin;
|
|
435
|
+
this.sql = sqlAdmin('sys', sql);
|
|
436
|
+
$.sql = this.sql;
|
|
437
|
+
|
|
438
|
+
// 获取缓存配置
|
|
439
|
+
var cache = { type: 'memory', ...this.config.cache };
|
|
440
|
+
if (this.config.cachebase) {
|
|
441
|
+
cache.cachebase = this.config.cachebase;
|
|
442
|
+
}
|
|
443
|
+
if (this.config.redis) {
|
|
444
|
+
cache = { ...cache, ...this.config.redis };
|
|
445
|
+
}
|
|
446
|
+
if (this.config.mongodb) {
|
|
447
|
+
cache.mongodb = this.config.mongodb;
|
|
448
|
+
}
|
|
449
|
+
this.cache = cacheAdmin('sys', cache);
|
|
450
|
+
$.cache = this.cache;
|
|
451
|
+
|
|
452
|
+
this.filer = $.file;
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* 初始化工具
|
|
457
|
+
*/
|
|
458
|
+
Server.prototype._createTool = function () {
|
|
459
|
+
$.log = new Log(this.config.log);
|
|
460
|
+
this.setLogger($.log);
|
|
461
|
+
$.config = new Config({
|
|
462
|
+
file: '/config.json'.fullname(),
|
|
463
|
+
});
|
|
464
|
+
$.config.load();
|
|
465
|
+
$.config.setOptions(this.config);
|
|
466
|
+
$.config.saveAsync();
|
|
467
|
+
$.com = new Com();
|
|
468
|
+
$.com.loads();
|
|
469
|
+
|
|
470
|
+
// 模板引擎
|
|
471
|
+
const Http = require('mm_https').Http;
|
|
472
|
+
$.Http = Http;
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* 创建适配器
|
|
477
|
+
*/
|
|
478
|
+
Server.prototype._createAdapter = async function () {
|
|
479
|
+
let config = this.config;
|
|
480
|
+
var web = new Web(config.web, this);
|
|
481
|
+
let adapter = this.adapter;
|
|
482
|
+
adapter.web = await web.init();
|
|
483
|
+
if (config.web.socket) {
|
|
484
|
+
adapter.ws = await new WebSocket(config.web, this).init(web);
|
|
485
|
+
}
|
|
486
|
+
if (config.mqtt && config.mqtt.state) {
|
|
487
|
+
adapter.mqtt = await new Mqtt(config.mqtt, this).init();
|
|
488
|
+
}
|
|
489
|
+
if (config.socket && config.socket.state) {
|
|
490
|
+
adapter.socket = await new Socket(config.socket || {}, this).init();
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* 启动适配器
|
|
496
|
+
*/
|
|
497
|
+
Server.prototype._startAdapter = async function () {
|
|
498
|
+
let adapter = this.adapter;
|
|
499
|
+
|
|
500
|
+
let promises = [];
|
|
501
|
+
for (let key in adapter) {
|
|
502
|
+
if (adapter[key]) {
|
|
503
|
+
promises.push(adapter[key].start());
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
await Promise.all(promises);
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* 启动基础设施
|
|
511
|
+
*/
|
|
512
|
+
Server.prototype._startBase = async function () {
|
|
513
|
+
await Promise.all([$.cache.connect(), $.sql.open()]);
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* 启动
|
|
518
|
+
*/
|
|
519
|
+
Server.prototype._startCore = async function () {
|
|
520
|
+
await this._startBase();
|
|
521
|
+
// 启动适配器
|
|
522
|
+
await this._startAdapter();
|
|
523
|
+
// 启动核心模块
|
|
524
|
+
await this._startSources();
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* 启动核心模块
|
|
529
|
+
*/
|
|
530
|
+
Server.prototype._startSources = async function () {
|
|
531
|
+
let manager = await this.getManager();
|
|
532
|
+
await Promise.all([
|
|
533
|
+
manager.game.runAll('start', this, this.getEventer(), this.getLogger()),
|
|
534
|
+
manager.app.runAll('start', this, this.getEventer(), this.getLogger()),
|
|
535
|
+
]);
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* 停止基础设施
|
|
540
|
+
*/
|
|
541
|
+
Server.prototype._stopBase = async function () {
|
|
542
|
+
await Promise.all([$.cache.close(), $.sql.close()]);
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* 停止 - 私有方法
|
|
547
|
+
*/
|
|
548
|
+
Server.prototype._stopCore = async function () {
|
|
549
|
+
await this._stopBase();
|
|
550
|
+
await this._stopSources();
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* 停止核心模块
|
|
555
|
+
*/
|
|
556
|
+
Server.prototype._stopSources = async function () {
|
|
557
|
+
let manager = await this.getManager();
|
|
558
|
+
await Promise.all([manager.game.runAll('stop'), manager.app.runAll('stop')]);
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* 运行
|
|
563
|
+
* @returns {object} 服务器实例
|
|
564
|
+
*/
|
|
565
|
+
Server.prototype.run = async function () {
|
|
566
|
+
// 初始化
|
|
567
|
+
console.time('加载');
|
|
568
|
+
await this.do('load');
|
|
569
|
+
console.timeEnd('加载');
|
|
570
|
+
|
|
571
|
+
// 初始化
|
|
572
|
+
console.time('初始化');
|
|
573
|
+
await this.do('init');
|
|
574
|
+
console.timeEnd('初始化');
|
|
575
|
+
|
|
576
|
+
// 启动
|
|
577
|
+
console.time('启动');
|
|
578
|
+
await this.do('start');
|
|
579
|
+
console.timeEnd('启动');
|
|
580
|
+
return this;
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
exports.Server = Server;
|