@bigtyphoon/melo 1.7.6
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/LICENSE +22 -0
- package/README.md +55 -0
- package/bin/commadtest.ts +10 -0
- package/bin/commands/add.ts +43 -0
- package/bin/commands/init.ts +292 -0
- package/bin/commands/kill.ts +21 -0
- package/bin/commands/list.ts +60 -0
- package/bin/commands/masterha.ts +40 -0
- package/bin/commands/restart.ts +48 -0
- package/bin/commands/start.ts +65 -0
- package/bin/commands/stop.ts +26 -0
- package/bin/melo.ts +30 -0
- package/bin/utils/constants.ts +27 -0
- package/bin/utils/utils.ts +130 -0
- package/dist/bin/commadtest.js +9 -0
- package/dist/bin/commands/add.js +40 -0
- package/dist/bin/commands/init.js +279 -0
- package/dist/bin/commands/kill.js +21 -0
- package/dist/bin/commands/list.js +65 -0
- package/dist/bin/commands/masterha.js +36 -0
- package/dist/bin/commands/restart.js +45 -0
- package/dist/bin/commands/start.js +58 -0
- package/dist/bin/commands/stop.js +20 -0
- package/dist/bin/melo.js +26 -0
- package/dist/bin/utils/constants.js +28 -0
- package/dist/bin/utils/utils.js +134 -0
- package/dist/lib/application.js +888 -0
- package/dist/lib/common/manager/appManager.js +112 -0
- package/dist/lib/common/manager/taskManager.js +39 -0
- package/dist/lib/common/remote/backend/msgRemote.js +63 -0
- package/dist/lib/common/remote/frontend/channelRemote.js +78 -0
- package/dist/lib/common/remote/frontend/sessionRemote.js +76 -0
- package/dist/lib/common/service/backendSessionService.js +337 -0
- package/dist/lib/common/service/channelService.js +514 -0
- package/dist/lib/common/service/connectionService.js +95 -0
- package/dist/lib/common/service/filterService.js +112 -0
- package/dist/lib/common/service/handlerService.js +187 -0
- package/dist/lib/common/service/sessionService.js +610 -0
- package/dist/lib/components/backendSession.js +14 -0
- package/dist/lib/components/channel.js +13 -0
- package/dist/lib/components/connection.js +12 -0
- package/dist/lib/components/connector.js +437 -0
- package/dist/lib/components/dictionary.js +93 -0
- package/dist/lib/components/master.js +39 -0
- package/dist/lib/components/monitor.js +25 -0
- package/dist/lib/components/protobuf.js +156 -0
- package/dist/lib/components/proxy.js +236 -0
- package/dist/lib/components/pushScheduler.js +62 -0
- package/dist/lib/components/remote.js +127 -0
- package/dist/lib/components/server.js +63 -0
- package/dist/lib/components/session.js +20 -0
- package/dist/lib/connectors/commands/handshake.js +119 -0
- package/dist/lib/connectors/commands/heartbeat.js +67 -0
- package/dist/lib/connectors/commands/kick.js +15 -0
- package/dist/lib/connectors/common/coder.js +90 -0
- package/dist/lib/connectors/common/handler.js +57 -0
- package/dist/lib/connectors/hybrid/IHybridSocket.js +3 -0
- package/dist/lib/connectors/hybrid/switcher.js +100 -0
- package/dist/lib/connectors/hybrid/tcpprocessor.js +40 -0
- package/dist/lib/connectors/hybrid/tcpsocket.js +171 -0
- package/dist/lib/connectors/hybrid/wsprocessor.js +49 -0
- package/dist/lib/connectors/hybridconnector.js +89 -0
- package/dist/lib/connectors/hybridsocket.js +139 -0
- package/dist/lib/connectors/mqtt/generate.js +113 -0
- package/dist/lib/connectors/mqtt/mqttadaptor.js +81 -0
- package/dist/lib/connectors/mqtt/protocol.js +48 -0
- package/dist/lib/connectors/mqttconnector.js +107 -0
- package/dist/lib/connectors/mqttsocket.js +59 -0
- package/dist/lib/connectors/sioconnector.js +135 -0
- package/dist/lib/connectors/siosocket.js +69 -0
- package/dist/lib/connectors/udpconnector.js +76 -0
- package/dist/lib/connectors/udpsocket.js +93 -0
- package/dist/lib/filters/handler/serial.js +44 -0
- package/dist/lib/filters/handler/time.js +32 -0
- package/dist/lib/filters/handler/timeout.js +45 -0
- package/dist/lib/filters/handler/toobusy.js +36 -0
- package/dist/lib/filters/rpc/rpcLog.js +43 -0
- package/dist/lib/filters/rpc/toobusy.js +41 -0
- package/dist/lib/index.js +81 -0
- package/dist/lib/interfaces/IComponent.js +3 -0
- package/dist/lib/interfaces/IConnector.js +3 -0
- package/dist/lib/interfaces/IHandlerFilter.js +3 -0
- package/dist/lib/interfaces/ILifeCycle.js +3 -0
- package/dist/lib/interfaces/IPlugin.js +3 -0
- package/dist/lib/interfaces/IPushScheduler.js +3 -0
- package/dist/lib/interfaces/ISocket.js +3 -0
- package/dist/lib/interfaces/IStore.js +3 -0
- package/dist/lib/interfaces/define.js +3 -0
- package/dist/lib/master/master.js +129 -0
- package/dist/lib/master/starter.js +236 -0
- package/dist/lib/master/watchdog.js +120 -0
- package/dist/lib/melo.js +125 -0
- package/dist/lib/modules/console.js +436 -0
- package/dist/lib/modules/masterwatcher.js +98 -0
- package/dist/lib/modules/monitorwatcher.js +124 -0
- package/dist/lib/modules/onlineUser.js +69 -0
- package/dist/lib/modules/restartNotifyModule.js +107 -0
- package/dist/lib/modules/watchServer.js +737 -0
- package/dist/lib/monitor/monitor.js +80 -0
- package/dist/lib/pushSchedulers/buffer.js +96 -0
- package/dist/lib/pushSchedulers/direct.js +58 -0
- package/dist/lib/pushSchedulers/multi.js +80 -0
- package/dist/lib/server/server.js +500 -0
- package/dist/lib/util/appUtil.js +306 -0
- package/dist/lib/util/constants.js +117 -0
- package/dist/lib/util/countDownLatch.js +51 -0
- package/dist/lib/util/events.js +20 -0
- package/dist/lib/util/handlerHelper.js +8 -0
- package/dist/lib/util/log.js +14 -0
- package/dist/lib/util/moduleUtil.js +101 -0
- package/dist/lib/util/pathUtil.js +134 -0
- package/dist/lib/util/remoterHelper.js +8 -0
- package/dist/lib/util/utils.js +358 -0
- package/dist/test/application.js +522 -0
- package/dist/test/config/log4js.json +28 -0
- package/dist/test/config/master.json +9 -0
- package/dist/test/config/servers.json +6 -0
- package/dist/test/filters/handler/serial.js +41 -0
- package/dist/test/filters/handler/time.js +41 -0
- package/dist/test/filters/handler/timeout.js +41 -0
- package/dist/test/filters/handler/toobusy.js +57 -0
- package/dist/test/filters/rpc/rpcLog.js +22 -0
- package/dist/test/filters/rpc/toobusy.js +39 -0
- package/dist/test/manager/mockChannelManager.js +77 -0
- package/dist/test/manager/taskManager.js +68 -0
- package/dist/test/mock-base/app/servers/other-file +0 -0
- package/dist/test/mock-plugin/components/mockPlugin.js +10 -0
- package/dist/test/mock-plugin/events/mockEvent.js +12 -0
- package/dist/test/modules/console.js +242 -0
- package/dist/test/pomelo.js +19 -0
- package/dist/test/remote/channelRemote.js +159 -0
- package/dist/test/service/channel.js +134 -0
- package/dist/test/service/channelService.js +216 -0
- package/dist/test/service/connectionService.js +114 -0
- package/dist/test/service/filterService.js +144 -0
- package/dist/test/service/handlerService.js +65 -0
- package/dist/test/service/sessionService.js +387 -0
- package/dist/test/util/countDownLatch.js +70 -0
- package/dist/test/util/pathUtil.js +108 -0
- package/dist/test/util/utils.js +140 -0
- package/lib/application.ts +1240 -0
- package/lib/common/manager/appManager.ts +118 -0
- package/lib/common/manager/taskManager.ts +50 -0
- package/lib/common/remote/backend/msgRemote.ts +134 -0
- package/lib/common/remote/frontend/channelRemote.ts +91 -0
- package/lib/common/remote/frontend/sessionRemote.ts +91 -0
- package/lib/common/service/backendSessionService.ts +388 -0
- package/lib/common/service/channelService.ts +609 -0
- package/lib/common/service/connectionService.ts +112 -0
- package/lib/common/service/filterService.ts +118 -0
- package/lib/common/service/handlerService.ts +224 -0
- package/lib/common/service/sessionService.ts +731 -0
- package/lib/components/backendSession.ts +14 -0
- package/lib/components/channel.ts +11 -0
- package/lib/components/connection.ts +13 -0
- package/lib/components/connector.ts +533 -0
- package/lib/components/dictionary.ts +121 -0
- package/lib/components/master.ts +41 -0
- package/lib/components/monitor.ts +30 -0
- package/lib/components/protobuf.ts +208 -0
- package/lib/components/proxy.ts +282 -0
- package/lib/components/pushScheduler.ts +70 -0
- package/lib/components/remote.ts +166 -0
- package/lib/components/server.ts +71 -0
- package/lib/components/session.ts +22 -0
- package/lib/connectors/commands/handshake.ts +155 -0
- package/lib/connectors/commands/heartbeat.ts +83 -0
- package/lib/connectors/commands/kick.ts +11 -0
- package/lib/connectors/common/coder.ts +93 -0
- package/lib/connectors/common/handler.ts +62 -0
- package/lib/connectors/hybrid/IHybridSocket.ts +9 -0
- package/lib/connectors/hybrid/switcher.ts +142 -0
- package/lib/connectors/hybrid/tcpprocessor.ts +43 -0
- package/lib/connectors/hybrid/tcpsocket.ts +223 -0
- package/lib/connectors/hybrid/wsprocessor.ts +57 -0
- package/lib/connectors/hybridconnector.ts +134 -0
- package/lib/connectors/hybridsocket.ts +168 -0
- package/lib/connectors/mqtt/generate.ts +103 -0
- package/lib/connectors/mqtt/mqttadaptor.ts +114 -0
- package/lib/connectors/mqtt/protocol.ts +49 -0
- package/lib/connectors/mqttconnector.ts +134 -0
- package/lib/connectors/mqttsocket.ts +79 -0
- package/lib/connectors/sioconnector.ts +161 -0
- package/lib/connectors/siosocket.ts +85 -0
- package/lib/connectors/udpconnector.ts +113 -0
- package/lib/connectors/udpsocket.ts +110 -0
- package/lib/filters/handler/serial.ts +46 -0
- package/lib/filters/handler/time.ts +35 -0
- package/lib/filters/handler/timeout.ts +50 -0
- package/lib/filters/handler/toobusy.ts +37 -0
- package/lib/filters/rpc/rpcLog.ts +42 -0
- package/lib/filters/rpc/toobusy.ts +41 -0
- package/lib/index.ts +74 -0
- package/lib/interfaces/IComponent.ts +47 -0
- package/lib/interfaces/IConnector.ts +20 -0
- package/lib/interfaces/IHandlerFilter.ts +15 -0
- package/lib/interfaces/ILifeCycle.ts +16 -0
- package/lib/interfaces/IPlugin.ts +65 -0
- package/lib/interfaces/IPushScheduler.ts +52 -0
- package/lib/interfaces/ISocket.ts +26 -0
- package/lib/interfaces/IStore.ts +10 -0
- package/lib/interfaces/define.ts +15 -0
- package/lib/master/master.ts +148 -0
- package/lib/master/starter.ts +234 -0
- package/lib/master/watchdog.ts +135 -0
- package/lib/melo.ts +152 -0
- package/lib/modules/console.ts +465 -0
- package/lib/modules/masterwatcher.ts +120 -0
- package/lib/modules/monitorwatcher.ts +151 -0
- package/lib/modules/onlineUser.ts +78 -0
- package/lib/modules/restartNotifyModule.ts +128 -0
- package/lib/modules/watchServer.ts +766 -0
- package/lib/monitor/monitor.ts +99 -0
- package/lib/pushSchedulers/buffer.ts +117 -0
- package/lib/pushSchedulers/direct.ts +65 -0
- package/lib/pushSchedulers/multi.ts +94 -0
- package/lib/server/server.ts +554 -0
- package/lib/util/appUtil.ts +313 -0
- package/lib/util/constants.ts +154 -0
- package/lib/util/countDownLatch.ts +72 -0
- package/lib/util/events.ts +15 -0
- package/lib/util/handlerHelper.ts +5 -0
- package/lib/util/log.ts +11 -0
- package/lib/util/moduleUtil.ts +110 -0
- package/lib/util/pathUtil.ts +132 -0
- package/lib/util/remoterHelper.ts +68 -0
- package/lib/util/utils.ts +365 -0
- package/package.json +93 -0
- package/template/game-server/.vscode/launch.json +27 -0
- package/template/game-server/app/servers/connector/handler/entryHandler.ts +50 -0
- package/template/game-server/app/servers/connector/remote/authRemoter.ts +36 -0
- package/template/game-server/app.ts +30 -0
- package/template/game-server/app.ts.mqtt +32 -0
- package/template/game-server/app.ts.sio +36 -0
- package/template/game-server/app.ts.sio.wss +34 -0
- package/template/game-server/app.ts.udp +31 -0
- package/template/game-server/app.ts.wss +40 -0
- package/template/game-server/config/adminServer.json +5 -0
- package/template/game-server/config/adminUser.json +22 -0
- package/template/game-server/config/clientProtos.json +1 -0
- package/template/game-server/config/dictionary.json +1 -0
- package/template/game-server/config/log4js.json +150 -0
- package/template/game-server/config/master.json +8 -0
- package/template/game-server/config/serverProtos.json +1 -0
- package/template/game-server/config/servers.json +12 -0
- package/template/game-server/copy.js +5 -0
- package/template/game-server/package.json +28 -0
- package/template/game-server/preload.ts +40 -0
- package/template/game-server/tsconfig.json +36 -0
- package/template/npm-install.bat +4 -0
- package/template/npm-install.sh +5 -0
- package/template/readme.md +31 -0
- package/template/shared/server.crt +15 -0
- package/template/shared/server.key +15 -0
- package/template/web-server/app.js +25 -0
- package/template/web-server/app.js.https +36 -0
- package/template/web-server/bin/component.bat +1 -0
- package/template/web-server/bin/component.sh +1 -0
- package/template/web-server/package.json +10 -0
- package/template/web-server/public/css/base.css +76 -0
- package/template/web-server/public/image/logo.png +0 -0
- package/template/web-server/public/image/sp.png +0 -0
- package/template/web-server/public/index.html +57 -0
- package/template/web-server/public/index.html.sio +58 -0
- package/template/web-server/public/js/lib/build/build.js +1730 -0
- package/template/web-server/public/js/lib/build/build.js.wss +1721 -0
- package/template/web-server/public/js/lib/component.json +6 -0
- package/template/web-server/public/js/lib/local/boot/component.json +11 -0
- package/template/web-server/public/js/lib/local/boot/index.js +11 -0
- package/template/web-server/public/js/lib/meloclient.js +456 -0
- package/template/web-server/public/js/lib/meloclient.js.wss +456 -0
- package/template/web-server/public/js/lib/socket.io.js +3 -0
- package/test/application.ts +607 -0
- package/test/filters/handler/serial.ts +47 -0
- package/test/filters/handler/time.ts +47 -0
- package/test/filters/handler/timeout.ts +46 -0
- package/test/filters/handler/toobusy.ts +59 -0
- package/test/filters/rpc/rpcLog.ts +20 -0
- package/test/filters/rpc/toobusy.ts +40 -0
- package/test/manager/mockChannelManager.ts +92 -0
- package/test/manager/taskManager.ts +78 -0
- package/test/mock-base/app/servers/other-file +0 -0
- package/test/mock-plugin/components/mockPlugin.ts +8 -0
- package/test/mock-plugin/events/mockEvent.ts +12 -0
- package/test/modules/console.ts +264 -0
- package/test/pomelo.ts +18 -0
- package/test/remote/channelRemote.ts +176 -0
- package/test/service/channel.ts +161 -0
- package/test/service/channelService.ts +243 -0
- package/test/service/connectionService.ts +142 -0
- package/test/service/filterService.ts +165 -0
- package/test/service/handlerService.ts +77 -0
- package/test/service/sessionService.ts +464 -0
- package/test/util/countDownLatch.ts +81 -0
- package/test/util/pathUtil.ts +122 -0
- package/test/util/utils.ts +165 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Component for monitor.
|
|
3
|
+
* Load and start monitor client.
|
|
4
|
+
*/
|
|
5
|
+
import { getLogger } from '@bigtyphoon/melo-logger';
|
|
6
|
+
import * as admin from '@bigtyphoon/melo-admin';
|
|
7
|
+
import * as moduleUtil from '../util/moduleUtil';
|
|
8
|
+
import * as utils from '../util/utils';
|
|
9
|
+
import * as Constants from '../util/constants';
|
|
10
|
+
import { Application } from '../application';
|
|
11
|
+
import { ConsoleService, IModule } from '@bigtyphoon/melo-admin';
|
|
12
|
+
import { ServerInfo } from '../util/constants';
|
|
13
|
+
import { ServerStartArgs } from '../util/appUtil';
|
|
14
|
+
import { MasterInfo } from '../index';
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
let logger = getLogger('melo', path.basename(__filename));
|
|
17
|
+
|
|
18
|
+
export interface MonitorOptions {
|
|
19
|
+
closeWatcher ?: boolean;
|
|
20
|
+
monitorAgentClientFactory?: admin.IMonitorAgentClientFactory;
|
|
21
|
+
}
|
|
22
|
+
export class Monitor {
|
|
23
|
+
app: Application;
|
|
24
|
+
serverInfo: ServerInfo;
|
|
25
|
+
masterInfo: ServerStartArgs;
|
|
26
|
+
modules: IModule[] = [];
|
|
27
|
+
closeWatcher: any;
|
|
28
|
+
monitorConsole: ConsoleService;
|
|
29
|
+
monitorOpts: admin.MonitorConsoleServiceOpts;
|
|
30
|
+
|
|
31
|
+
constructor(app: Application, opts ?: MonitorOptions) {
|
|
32
|
+
opts = opts || {};
|
|
33
|
+
this.app = app;
|
|
34
|
+
this.serverInfo = app.getCurServer();
|
|
35
|
+
this.masterInfo = app.getMaster();
|
|
36
|
+
this.closeWatcher = opts.closeWatcher;
|
|
37
|
+
|
|
38
|
+
this.monitorOpts = {
|
|
39
|
+
id: this.serverInfo.id,
|
|
40
|
+
type: this.app.getServerType(),
|
|
41
|
+
host: this.masterInfo.host,
|
|
42
|
+
port: this.masterInfo.port,
|
|
43
|
+
info: this.serverInfo,
|
|
44
|
+
env: this.app.get(Constants.RESERVED.ENV),
|
|
45
|
+
authServer: app.get('adminAuthServerMonitor'), // auth server function
|
|
46
|
+
monitorAgentClientFactory: opts.monitorAgentClientFactory
|
|
47
|
+
};
|
|
48
|
+
this.monitorConsole = admin.createMonitorConsole(this.monitorOpts);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
start(cb: (err?: Error) => void) {
|
|
52
|
+
moduleUtil.registerDefaultModules(false, this.app, this.closeWatcher);
|
|
53
|
+
this.startConsole(cb);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
startConsole(cb: (err?: Error) => void) {
|
|
57
|
+
moduleUtil.loadModules(this, this.monitorConsole);
|
|
58
|
+
|
|
59
|
+
let self = this;
|
|
60
|
+
this.monitorConsole.start(function (err) {
|
|
61
|
+
if (err) {
|
|
62
|
+
utils.invokeCallback(cb, err);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
moduleUtil.startModules(self.modules, function (err) {
|
|
66
|
+
utils.invokeCallback(cb, err);
|
|
67
|
+
return;
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
this.monitorConsole.on('error', function (err) {
|
|
72
|
+
if (!!err) {
|
|
73
|
+
logger.error('monitorConsole encounters with error: %j', err.stack);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
stop(cb: () => void) {
|
|
80
|
+
this.monitorConsole.stop();
|
|
81
|
+
this.modules = [];
|
|
82
|
+
process.nextTick(function () {
|
|
83
|
+
utils.invokeCallback(cb);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// monitor reconnect to master
|
|
88
|
+
reconnect(masterInfo: MasterInfo) {
|
|
89
|
+
let self = this;
|
|
90
|
+
this.stop(function () {
|
|
91
|
+
self.monitorOpts.host = masterInfo.host;
|
|
92
|
+
self.monitorOpts.port = masterInfo.port;
|
|
93
|
+
self.monitorConsole = admin.createMonitorConsole(self.monitorOpts);
|
|
94
|
+
self.startConsole(function () {
|
|
95
|
+
logger.info('restart modules for server : %j finish.', self.app.serverId);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import * as utils from '../util/utils';
|
|
2
|
+
import { Application } from '../application';
|
|
3
|
+
import { SID } from '../util/constants';
|
|
4
|
+
import { ScheduleOptions, BroadcastOptions } from '../interfaces/IPushScheduler';
|
|
5
|
+
import { Session } from '../index';
|
|
6
|
+
let DEFAULT_FLUSH_INTERVAL = 20;
|
|
7
|
+
|
|
8
|
+
export interface BufferPushSchedulerOptions {
|
|
9
|
+
flushInterval?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class BufferPushScheduler {
|
|
13
|
+
app: Application;
|
|
14
|
+
flushInterval: number;
|
|
15
|
+
sessions: { [sid: number]: Session[] } = {}; // sid -> msg queue
|
|
16
|
+
tid: NodeJS.Timeout = null;
|
|
17
|
+
|
|
18
|
+
constructor(app: Application, opts?: BufferPushSchedulerOptions) {
|
|
19
|
+
|
|
20
|
+
opts = opts || {};
|
|
21
|
+
this.app = app;
|
|
22
|
+
this.flushInterval = opts.flushInterval || DEFAULT_FLUSH_INTERVAL;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async start() {
|
|
26
|
+
this.tid = setInterval(this.flush.bind(this), this.flushInterval);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async stop() {
|
|
30
|
+
if (this.tid) {
|
|
31
|
+
clearInterval(this.tid);
|
|
32
|
+
this.tid = null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
schedule(reqId: number, route: string, msg: any, recvs: SID[], opts: ScheduleOptions, cb: () => void) {
|
|
37
|
+
opts = opts || {};
|
|
38
|
+
if (opts.type === 'broadcast') {
|
|
39
|
+
this.doBroadcast( msg, opts.userOptions);
|
|
40
|
+
} else {
|
|
41
|
+
this.doBatchPush( msg, recvs);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
process.nextTick(function () {
|
|
45
|
+
utils.invokeCallback(cb);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
flush() {
|
|
50
|
+
let sessionService = this.app.get('sessionService');
|
|
51
|
+
let queue, session;
|
|
52
|
+
for (let sid in this.sessions) {
|
|
53
|
+
session = sessionService.get(Number(sid));
|
|
54
|
+
if (!session) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
queue = this.sessions[sid];
|
|
59
|
+
if (!queue || queue.length === 0) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
session.sendBatch(queue);
|
|
64
|
+
this.sessions[sid] = [];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
doBroadcast(msg: any, opts: BroadcastOptions) {
|
|
70
|
+
let channelService = this.app.get('channelService');
|
|
71
|
+
let sessionService = this.app.get('sessionService');
|
|
72
|
+
|
|
73
|
+
if (opts.binded) {
|
|
74
|
+
sessionService.forEachBindedSession( (session) => {
|
|
75
|
+
if (channelService.broadcastFilter &&
|
|
76
|
+
!channelService.broadcastFilter(session, msg, opts.filterParam)) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this.enqueue( session, msg);
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
sessionService.forEachSession( (session) => {
|
|
84
|
+
if (channelService.broadcastFilter &&
|
|
85
|
+
!channelService.broadcastFilter(session, msg, opts.filterParam)) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.enqueue( session, msg);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
doBatchPush(msg: any, recvs: SID[]) {
|
|
94
|
+
let sessionService = this.app.get('sessionService');
|
|
95
|
+
let session;
|
|
96
|
+
for (let i = 0, l = recvs.length; i < l; i++) {
|
|
97
|
+
session = sessionService.get(recvs[i]);
|
|
98
|
+
if (session) {
|
|
99
|
+
this.enqueue( session, msg);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
enqueue(session: Session, msg: any) {
|
|
105
|
+
let queue = this.sessions[session.id];
|
|
106
|
+
if (!queue) {
|
|
107
|
+
queue = this.sessions[session.id] = [];
|
|
108
|
+
session.once('closed', this.onClose.bind(this));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
queue.push(msg);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
onClose(session: Session) {
|
|
115
|
+
delete this.sessions[session.id];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as utils from '../util/utils';
|
|
2
|
+
import { Application } from '../application';
|
|
3
|
+
import { IPushScheduler, ScheduleOptions, BroadcastOptions } from '../interfaces/IPushScheduler';
|
|
4
|
+
import { SID } from '../util/constants';
|
|
5
|
+
|
|
6
|
+
export class DirectPushScheduler implements IPushScheduler {
|
|
7
|
+
async start() {
|
|
8
|
+
}
|
|
9
|
+
async stop() {
|
|
10
|
+
}
|
|
11
|
+
app: Application;
|
|
12
|
+
constructor(app: Application, opts ?: {}) {
|
|
13
|
+
opts = opts || {};
|
|
14
|
+
this.app = app;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
schedule(reqId: number, route: string, msg: any, recvs: SID[], opts: ScheduleOptions, cb: (err?: Error) => void) {
|
|
19
|
+
opts = opts || {};
|
|
20
|
+
if (opts.type === 'broadcast') {
|
|
21
|
+
this.doBroadcast(msg, opts.userOptions);
|
|
22
|
+
} else {
|
|
23
|
+
this.doBatchPush(msg, recvs);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (cb) {
|
|
27
|
+
process.nextTick(function () {
|
|
28
|
+
utils.invokeCallback(cb);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
doBroadcast(msg: any, opts: BroadcastOptions) {
|
|
34
|
+
let channelService = this.app.get('channelService');
|
|
35
|
+
let sessionService = this.app.get('sessionService');
|
|
36
|
+
|
|
37
|
+
if (opts.binded) {
|
|
38
|
+
sessionService.forEachBindedSession(function (session) {
|
|
39
|
+
if (channelService.broadcastFilter &&
|
|
40
|
+
!channelService.broadcastFilter(session, msg, opts.filterParam)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
sessionService.sendMessageByUid(session.uid, msg);
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
sessionService.forEachSession(function (session) {
|
|
48
|
+
if (channelService.broadcastFilter &&
|
|
49
|
+
!channelService.broadcastFilter(session, msg, opts.filterParam)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
sessionService.sendMessage(session.id, msg);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
doBatchPush(msg: any, recvs: SID[]) {
|
|
59
|
+
let sessionService = this.app.get('sessionService');
|
|
60
|
+
for (let i = 0, l = recvs.length; i < l; i++) {
|
|
61
|
+
sessionService.sendMessage(recvs[i], msg);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { IPushScheduler, ScheduleOptions, IPushSchedulerOrCtor, MultiPushSchedulerOptions, IPushSelector } from '../interfaces/IPushScheduler';
|
|
2
|
+
import { Application } from '../application';
|
|
3
|
+
import { getLogger } from '@bigtyphoon/melo-logger';
|
|
4
|
+
import { SID } from '../util/constants';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
let logger = getLogger('melo', path.basename(__filename));
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export class MultiPushScheduler implements IPushScheduler {
|
|
11
|
+
app: Application;
|
|
12
|
+
|
|
13
|
+
selector: IPushSelector;
|
|
14
|
+
|
|
15
|
+
scheduler: {[id: number]: IPushScheduler};
|
|
16
|
+
|
|
17
|
+
constructor(app: Application, opts: MultiPushSchedulerOptions) {
|
|
18
|
+
opts = opts || {};
|
|
19
|
+
let scheduler = opts.scheduler;
|
|
20
|
+
if (Array.isArray(scheduler)) {
|
|
21
|
+
this.scheduler = {};
|
|
22
|
+
for(let sch of scheduler) {
|
|
23
|
+
if (typeof sch.scheduler === 'function') {
|
|
24
|
+
this.scheduler[sch.id] = new sch.scheduler(app, sch.options);
|
|
25
|
+
} else {
|
|
26
|
+
this.scheduler[sch.id] = sch.scheduler;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if(typeof opts.selector === 'function') {
|
|
31
|
+
throw new Error('MultiPushScheduler必须提供selector参数');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this.selector = opts.selector;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
throw new Error('MultiPushScheduler必须提供scheduler参数');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.app = app;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Component lifecycle callback
|
|
47
|
+
*
|
|
48
|
+
* @param {Function} cb
|
|
49
|
+
* @return {Void}
|
|
50
|
+
*/
|
|
51
|
+
async start() {
|
|
52
|
+
for (let k in this.scheduler) {
|
|
53
|
+
let sch = this.scheduler[k];
|
|
54
|
+
if (typeof sch.start === 'function') {
|
|
55
|
+
await sch.start();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Component lifecycle callback
|
|
62
|
+
*
|
|
63
|
+
* @param {Function} cb
|
|
64
|
+
* @return {Void}
|
|
65
|
+
*/
|
|
66
|
+
async stop() {
|
|
67
|
+
for (let k in this.scheduler) {
|
|
68
|
+
let sch = this.scheduler[k];
|
|
69
|
+
if (typeof sch.stop === 'function') {
|
|
70
|
+
await sch.stop();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Schedule how the message to send.
|
|
77
|
+
*
|
|
78
|
+
* @param {Number} reqId request id
|
|
79
|
+
* @param {String} route route string of the message
|
|
80
|
+
* @param {Object} msg message content after encoded
|
|
81
|
+
* @param {Array} recvs array of receiver's session id
|
|
82
|
+
* @param {Object} opts options
|
|
83
|
+
*/
|
|
84
|
+
schedule(reqId: number, route: string, msg: any, recvs: SID[], opts: ScheduleOptions, cb: (err?: Error) => void) {
|
|
85
|
+
let self = this;
|
|
86
|
+
let id = self.selector(reqId, route, msg, recvs, opts);
|
|
87
|
+
|
|
88
|
+
if (self.scheduler[id]) {
|
|
89
|
+
self.scheduler[id].schedule(reqId, route, msg, recvs, opts, cb);
|
|
90
|
+
} else {
|
|
91
|
+
logger.error('invalid pushScheduler id, id: %j', id);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|