@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,388 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* backend session service for backend session
|
|
3
|
+
*/
|
|
4
|
+
import * as utils from '../../util/utils';
|
|
5
|
+
import { Application } from '../../application';
|
|
6
|
+
import { IComponent } from '../../interfaces/IComponent';
|
|
7
|
+
import { SID, FRONTENDID, ServerInfo } from '../../util/constants';
|
|
8
|
+
import { ISession } from './sessionService';
|
|
9
|
+
|
|
10
|
+
let EXPORTED_FIELDS = ['id', 'frontendId', 'uid', 'settings'];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Service that maintains backend sessions and the communication with frontend
|
|
14
|
+
* servers.
|
|
15
|
+
*
|
|
16
|
+
* BackendSessionService would be created in each server process and maintains
|
|
17
|
+
* backend sessions for current process and communicates with the relative
|
|
18
|
+
* frontend servers.
|
|
19
|
+
*
|
|
20
|
+
* BackendSessionService instance could be accessed by
|
|
21
|
+
* `app.get('backendSessionService')` or app.backendSessionService.
|
|
22
|
+
*
|
|
23
|
+
* @class
|
|
24
|
+
* @constructor
|
|
25
|
+
*/
|
|
26
|
+
export class BackendSessionService implements IComponent {
|
|
27
|
+
app: Application;
|
|
28
|
+
name: string;
|
|
29
|
+
|
|
30
|
+
constructor(app: Application) {
|
|
31
|
+
this.app = app;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
create(opts: any) {
|
|
35
|
+
if (!opts) {
|
|
36
|
+
throw new Error('opts should not be empty.');
|
|
37
|
+
}
|
|
38
|
+
return new BackendSession(opts, this);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get backend session by frontend server id and session id.
|
|
43
|
+
*
|
|
44
|
+
* @param {String} frontendId frontend server id that session attached
|
|
45
|
+
* @param {String} sid session id
|
|
46
|
+
* @param {Function} cb callback function. args: cb(err, BackendSession)
|
|
47
|
+
*
|
|
48
|
+
* @memberOf BackendSessionService
|
|
49
|
+
*/
|
|
50
|
+
get(frontendId: string, sid: number, cb: (err: Error | null, result?: BackendSession) => void) {
|
|
51
|
+
let namespace = 'sys';
|
|
52
|
+
let service = 'sessionRemote';
|
|
53
|
+
let method = 'getBackendSessionBySid';
|
|
54
|
+
let args = [sid];
|
|
55
|
+
rpcInvoke(this.app, frontendId, namespace, service, method,
|
|
56
|
+
args, BackendSessionCB.bind(null, this, cb));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get backend sessions by frontend server id and user id.
|
|
61
|
+
*
|
|
62
|
+
* @param {String} frontendId frontend server id that session attached
|
|
63
|
+
* @param {String} uid user id binded with the session
|
|
64
|
+
* @param {Function} cb callback function. args: cb(err, BackendSessions)
|
|
65
|
+
*
|
|
66
|
+
* @memberOf BackendSessionService
|
|
67
|
+
*/
|
|
68
|
+
getByUid(frontendId: string, uid: string, cb: (err: Error | null, result?: BackendSession[]) => void) {
|
|
69
|
+
let namespace = 'sys';
|
|
70
|
+
let service = 'sessionRemote';
|
|
71
|
+
let method = 'getBackendSessionsByUid';
|
|
72
|
+
let args = [uid];
|
|
73
|
+
rpcInvoke(this.app, frontendId, namespace, service, method,
|
|
74
|
+
args, BackendSessionCB.bind(null, this, cb));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Kick a session by session id.
|
|
79
|
+
*
|
|
80
|
+
* @param {String} frontendId cooperating frontend server id
|
|
81
|
+
* @param {Number} sid session id
|
|
82
|
+
* @param {Function} cb callback function
|
|
83
|
+
*
|
|
84
|
+
* @memberOf BackendSessionService
|
|
85
|
+
*/
|
|
86
|
+
kickBySid(frontendId: string, sid: number, cb: callBack): void;
|
|
87
|
+
kickBySid(frontendId: string, sid: number, reason: string): void;
|
|
88
|
+
kickBySid(frontendId: string, sid: number, reason: string, cb: callBack): void;
|
|
89
|
+
kickBySid(frontendId: string, sid: number, reason: callBack | string, cb?: callBack) {
|
|
90
|
+
let namespace = 'sys';
|
|
91
|
+
let service = 'sessionRemote';
|
|
92
|
+
let method = 'kickBySid';
|
|
93
|
+
let args: Array<any> = [sid];
|
|
94
|
+
if (typeof reason === 'function') {
|
|
95
|
+
cb = reason;
|
|
96
|
+
} else {
|
|
97
|
+
args.push(reason);
|
|
98
|
+
}
|
|
99
|
+
rpcInvoke(this.app, frontendId, namespace, service, method, args, cb);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Kick sessions by user id.
|
|
104
|
+
*
|
|
105
|
+
* @param {String} frontendId cooperating frontend server id
|
|
106
|
+
* @param {Number|String} uid user id
|
|
107
|
+
* @param {String} reason kick reason
|
|
108
|
+
* @param {Function} cb callback function
|
|
109
|
+
*
|
|
110
|
+
* @memberOf BackendSessionService
|
|
111
|
+
*/
|
|
112
|
+
kickByUid(frontendId: string, uid: string, reason: string, cb: callBack) {
|
|
113
|
+
let namespace = 'sys';
|
|
114
|
+
let service = 'sessionRemote';
|
|
115
|
+
let method = 'kickByUid';
|
|
116
|
+
let args = [uid];
|
|
117
|
+
if (typeof reason === 'function') {
|
|
118
|
+
cb = reason;
|
|
119
|
+
} else {
|
|
120
|
+
args.push(reason);
|
|
121
|
+
}
|
|
122
|
+
rpcInvoke(this.app, frontendId, namespace, service, method, args, cb);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Bind the session with the specified user id. It would finally invoke the
|
|
127
|
+
* the sessionService.bind in the cooperating frontend server.
|
|
128
|
+
*
|
|
129
|
+
* @param {String} frontendId cooperating frontend server id
|
|
130
|
+
* @param {Number} sid session id
|
|
131
|
+
* @param {String} uid user id
|
|
132
|
+
* @param {Function} cb callback function
|
|
133
|
+
*
|
|
134
|
+
* @memberOf BackendSessionService
|
|
135
|
+
* @api private
|
|
136
|
+
*/
|
|
137
|
+
bind(frontendId: string, sid: number, uid: string, cb: callBack) {
|
|
138
|
+
let namespace = 'sys';
|
|
139
|
+
let service = 'sessionRemote';
|
|
140
|
+
let method = 'bind';
|
|
141
|
+
let args = [sid, uid];
|
|
142
|
+
rpcInvoke(this.app, frontendId, namespace, service, method, args, cb);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Unbind the session with the specified user id. It would finally invoke the
|
|
147
|
+
* the sessionService.unbind in the cooperating frontend server.
|
|
148
|
+
*
|
|
149
|
+
* @param {String} frontendId cooperating frontend server id
|
|
150
|
+
* @param {Number} sid session id
|
|
151
|
+
* @param {String} uid user id
|
|
152
|
+
* @param {Function} cb callback function
|
|
153
|
+
*
|
|
154
|
+
* @memberOf BackendSessionService
|
|
155
|
+
* @api private
|
|
156
|
+
*/
|
|
157
|
+
unbind(frontendId: string, sid: number, uid: string, cb: callBack) {
|
|
158
|
+
let namespace = 'sys';
|
|
159
|
+
let service = 'sessionRemote';
|
|
160
|
+
let method = 'unbind';
|
|
161
|
+
let args = [sid, uid];
|
|
162
|
+
rpcInvoke(this.app, frontendId, namespace, service, method, args, cb);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Push the specified customized change to the frontend internal session.
|
|
167
|
+
*
|
|
168
|
+
* @param {String} frontendId cooperating frontend server id
|
|
169
|
+
* @param {Number} sid session id
|
|
170
|
+
* @param {String} key key in session that should be push
|
|
171
|
+
* @param {Object} value value in session, primitive js object
|
|
172
|
+
* @param {Function} cb callback function
|
|
173
|
+
*
|
|
174
|
+
* @memberOf BackendSessionService
|
|
175
|
+
* @api private
|
|
176
|
+
*/
|
|
177
|
+
push(frontendId: string, sid: number, key: string, value: string, cb: callBack) {
|
|
178
|
+
let namespace = 'sys';
|
|
179
|
+
let service = 'sessionRemote';
|
|
180
|
+
let method = 'push';
|
|
181
|
+
let args = [sid, key, value];
|
|
182
|
+
rpcInvoke(this.app, frontendId, namespace, service, method, args, cb);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Push all the customized changes to the frontend internal session.
|
|
187
|
+
*
|
|
188
|
+
* @param {String} frontendId cooperating frontend server id
|
|
189
|
+
* @param {Number} sid session id
|
|
190
|
+
* @param {Object} settings key/values in session that should be push
|
|
191
|
+
* @param {Function} cb callback function
|
|
192
|
+
*
|
|
193
|
+
* @memberOf BackendSessionService
|
|
194
|
+
* @api private
|
|
195
|
+
*/
|
|
196
|
+
pushAll(frontendId: string, sid: number, settings: Object, cb: callBack) {
|
|
197
|
+
let namespace = 'sys';
|
|
198
|
+
let service = 'sessionRemote';
|
|
199
|
+
let method = 'pushAll';
|
|
200
|
+
let args = [sid, settings];
|
|
201
|
+
rpcInvoke(this.app, frontendId, namespace, service, method, args, cb);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
aget = utils.promisify(this.get.bind(this));
|
|
205
|
+
agetByUid = utils.promisify(this.getByUid.bind(this));
|
|
206
|
+
akickBySid = utils.promisify(this.kickBySid.bind(this));
|
|
207
|
+
akickByUid = utils.promisify(this.kickByUid.bind(this));
|
|
208
|
+
abind = utils.promisify(this.bind.bind(this));
|
|
209
|
+
aunbind = utils.promisify(this.unbind.bind(this));
|
|
210
|
+
apush = utils.promisify(this.push.bind(this));
|
|
211
|
+
apushAll = utils.promisify(this.pushAll.bind(this));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let rpcInvoke = function (app: Application, sid: FRONTENDID, namespace: string, service: string, method: string, args: any, cb: Function) {
|
|
215
|
+
app.rpcInvoke(sid, { namespace: namespace, service: service, method: method, args: args }, cb);
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* BackendSession is the proxy for the frontend internal session passed to handlers and
|
|
221
|
+
* it helps to keep the key/value pairs for the server locally.
|
|
222
|
+
* Internal session locates in frontend server and should not be accessed directly.
|
|
223
|
+
*
|
|
224
|
+
* The mainly operation on backend session should be read and any changes happen in backend
|
|
225
|
+
* session is local and would be discarded in next request. You have to push the
|
|
226
|
+
* changes to the frontend manually if necessary. Any push would overwrite the last push
|
|
227
|
+
* of the same key silently and the changes would be saw in next request.
|
|
228
|
+
* And you have to make sure the transaction outside if you would push the session
|
|
229
|
+
* concurrently in different processes.
|
|
230
|
+
*
|
|
231
|
+
* See the api below for more details.
|
|
232
|
+
*
|
|
233
|
+
* @class
|
|
234
|
+
* @constructor
|
|
235
|
+
*/
|
|
236
|
+
export class BackendSession implements ISession {
|
|
237
|
+
id: number;
|
|
238
|
+
uid: string;
|
|
239
|
+
frontendId: string;
|
|
240
|
+
settings: { [key: string]: any };
|
|
241
|
+
__sessionService__: BackendSessionService;
|
|
242
|
+
|
|
243
|
+
constructor(opts: any, service: BackendSessionService) {
|
|
244
|
+
for (let f in opts) {
|
|
245
|
+
(this as any)[f] = opts[f];
|
|
246
|
+
}
|
|
247
|
+
this.__sessionService__ = service;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Bind current session with the user id. It would push the uid to frontend
|
|
252
|
+
* server and bind uid to the frontend internal session.
|
|
253
|
+
*
|
|
254
|
+
* @param {Number|String} uid user id
|
|
255
|
+
* @param {Function} cb callback function
|
|
256
|
+
*
|
|
257
|
+
* @memberOf BackendSession
|
|
258
|
+
*/
|
|
259
|
+
bind(uid: string, cb: callBack) {
|
|
260
|
+
let self = this;
|
|
261
|
+
this.__sessionService__.bind(this.frontendId, this.id, uid, function (err) {
|
|
262
|
+
if (!err) {
|
|
263
|
+
self.uid = uid;
|
|
264
|
+
}
|
|
265
|
+
utils.invokeCallback(cb, err);
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Unbind current session with the user id. It would push the uid to frontend
|
|
271
|
+
* server and unbind uid from the frontend internal session.
|
|
272
|
+
*
|
|
273
|
+
* @param {Number|String} uid user id
|
|
274
|
+
* @param {Function} cb callback function
|
|
275
|
+
*
|
|
276
|
+
* @memberOf BackendSession
|
|
277
|
+
*/
|
|
278
|
+
unbind(uid: string, cb: callBack) {
|
|
279
|
+
let self = this;
|
|
280
|
+
this.__sessionService__.unbind(this.frontendId, this.id, uid, function (err) {
|
|
281
|
+
if (!err) {
|
|
282
|
+
self.uid = null;
|
|
283
|
+
}
|
|
284
|
+
utils.invokeCallback(cb, err);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Set the key/value into backend session.
|
|
290
|
+
*
|
|
291
|
+
* @param {String} key key
|
|
292
|
+
* @param {Object} value value
|
|
293
|
+
*/
|
|
294
|
+
set(key: string, value: any) {
|
|
295
|
+
this.settings[key] = value;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Get the value from backend session by key.
|
|
300
|
+
*
|
|
301
|
+
* @param {String} key key
|
|
302
|
+
* @return {Object} value
|
|
303
|
+
*/
|
|
304
|
+
get(key: string) {
|
|
305
|
+
return this.settings[key];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Push the key/value in backend session to the front internal session.
|
|
310
|
+
*
|
|
311
|
+
* @param {String} key key
|
|
312
|
+
* @param {Function} cb callback function
|
|
313
|
+
*/
|
|
314
|
+
push(key: string, cb: callBack) {
|
|
315
|
+
this.__sessionService__.push(this.frontendId, this.id, key, this.get(key), cb);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Push all the key/values in backend session to the frontend internal session.
|
|
320
|
+
*
|
|
321
|
+
* @param {Function} cb callback function
|
|
322
|
+
*/
|
|
323
|
+
pushAll(cb: callBack) {
|
|
324
|
+
this.__sessionService__.pushAll(this.frontendId, this.id, this.settings, cb);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
abind(uid: string) {
|
|
328
|
+
return new Promise((resolve, reject) => this.bind(uid, (err, ret) => err ? reject(err) : resolve(ret as any)));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
aunbind(uid: string) {
|
|
332
|
+
return new Promise((resolve, reject) => this.unbind(uid, (err, ret) => err ? reject(err) : resolve(ret as any)));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
apush(key: string) {
|
|
336
|
+
return new Promise((resolve, reject) => this.push(key, (err, ret) => err ? reject(err) : resolve(ret as any)));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
apushAll() {
|
|
340
|
+
return new Promise((resolve, reject) => this.pushAll((err, ret) => err ? reject(err) : resolve(ret as any)));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// abind = utils.promisify(this.bind.bind(this));
|
|
344
|
+
// aunbind = utils.promisify(this.unbind.bind(this));
|
|
345
|
+
// apush = utils.promisify(this.push.bind(this));
|
|
346
|
+
// apushAll = utils.promisify(this.pushAll.bind(this));
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Export the key/values for serialization.
|
|
350
|
+
*
|
|
351
|
+
* @api private
|
|
352
|
+
*/
|
|
353
|
+
export() {
|
|
354
|
+
let res: any = {};
|
|
355
|
+
EXPORTED_FIELDS.forEach((field) => {
|
|
356
|
+
res[field] = (this as any)[field];
|
|
357
|
+
});
|
|
358
|
+
return res;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
let BackendSessionCB = function (service: BackendSessionService, cb: Function, err: Error, sinfo: ServerInfo) {
|
|
363
|
+
if (err) {
|
|
364
|
+
utils.invokeCallback(cb, err);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (!sinfo) {
|
|
369
|
+
utils.invokeCallback(cb);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
let sessions = [];
|
|
373
|
+
if (Array.isArray(sinfo)) {
|
|
374
|
+
// #getByUid
|
|
375
|
+
for (let i = 0, k = sinfo.length; i < k; i++) {
|
|
376
|
+
sessions.push(service.create(sinfo[i]));
|
|
377
|
+
}
|
|
378
|
+
utils.invokeCallback(cb, null, sessions);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
// #get
|
|
382
|
+
let session = service.create(sinfo);
|
|
383
|
+
utils.invokeCallback(cb, null, session);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
type callBack = (err: Error | null, result?: void) => void
|