@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.
Files changed (296) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +55 -0
  3. package/bin/commadtest.ts +10 -0
  4. package/bin/commands/add.ts +43 -0
  5. package/bin/commands/init.ts +292 -0
  6. package/bin/commands/kill.ts +21 -0
  7. package/bin/commands/list.ts +60 -0
  8. package/bin/commands/masterha.ts +40 -0
  9. package/bin/commands/restart.ts +48 -0
  10. package/bin/commands/start.ts +65 -0
  11. package/bin/commands/stop.ts +26 -0
  12. package/bin/melo.ts +30 -0
  13. package/bin/utils/constants.ts +27 -0
  14. package/bin/utils/utils.ts +130 -0
  15. package/dist/bin/commadtest.js +9 -0
  16. package/dist/bin/commands/add.js +40 -0
  17. package/dist/bin/commands/init.js +279 -0
  18. package/dist/bin/commands/kill.js +21 -0
  19. package/dist/bin/commands/list.js +65 -0
  20. package/dist/bin/commands/masterha.js +36 -0
  21. package/dist/bin/commands/restart.js +45 -0
  22. package/dist/bin/commands/start.js +58 -0
  23. package/dist/bin/commands/stop.js +20 -0
  24. package/dist/bin/melo.js +26 -0
  25. package/dist/bin/utils/constants.js +28 -0
  26. package/dist/bin/utils/utils.js +134 -0
  27. package/dist/lib/application.js +888 -0
  28. package/dist/lib/common/manager/appManager.js +112 -0
  29. package/dist/lib/common/manager/taskManager.js +39 -0
  30. package/dist/lib/common/remote/backend/msgRemote.js +63 -0
  31. package/dist/lib/common/remote/frontend/channelRemote.js +78 -0
  32. package/dist/lib/common/remote/frontend/sessionRemote.js +76 -0
  33. package/dist/lib/common/service/backendSessionService.js +337 -0
  34. package/dist/lib/common/service/channelService.js +514 -0
  35. package/dist/lib/common/service/connectionService.js +95 -0
  36. package/dist/lib/common/service/filterService.js +112 -0
  37. package/dist/lib/common/service/handlerService.js +187 -0
  38. package/dist/lib/common/service/sessionService.js +610 -0
  39. package/dist/lib/components/backendSession.js +14 -0
  40. package/dist/lib/components/channel.js +13 -0
  41. package/dist/lib/components/connection.js +12 -0
  42. package/dist/lib/components/connector.js +437 -0
  43. package/dist/lib/components/dictionary.js +93 -0
  44. package/dist/lib/components/master.js +39 -0
  45. package/dist/lib/components/monitor.js +25 -0
  46. package/dist/lib/components/protobuf.js +156 -0
  47. package/dist/lib/components/proxy.js +236 -0
  48. package/dist/lib/components/pushScheduler.js +62 -0
  49. package/dist/lib/components/remote.js +127 -0
  50. package/dist/lib/components/server.js +63 -0
  51. package/dist/lib/components/session.js +20 -0
  52. package/dist/lib/connectors/commands/handshake.js +119 -0
  53. package/dist/lib/connectors/commands/heartbeat.js +67 -0
  54. package/dist/lib/connectors/commands/kick.js +15 -0
  55. package/dist/lib/connectors/common/coder.js +90 -0
  56. package/dist/lib/connectors/common/handler.js +57 -0
  57. package/dist/lib/connectors/hybrid/IHybridSocket.js +3 -0
  58. package/dist/lib/connectors/hybrid/switcher.js +100 -0
  59. package/dist/lib/connectors/hybrid/tcpprocessor.js +40 -0
  60. package/dist/lib/connectors/hybrid/tcpsocket.js +171 -0
  61. package/dist/lib/connectors/hybrid/wsprocessor.js +49 -0
  62. package/dist/lib/connectors/hybridconnector.js +89 -0
  63. package/dist/lib/connectors/hybridsocket.js +139 -0
  64. package/dist/lib/connectors/mqtt/generate.js +113 -0
  65. package/dist/lib/connectors/mqtt/mqttadaptor.js +81 -0
  66. package/dist/lib/connectors/mqtt/protocol.js +48 -0
  67. package/dist/lib/connectors/mqttconnector.js +107 -0
  68. package/dist/lib/connectors/mqttsocket.js +59 -0
  69. package/dist/lib/connectors/sioconnector.js +135 -0
  70. package/dist/lib/connectors/siosocket.js +69 -0
  71. package/dist/lib/connectors/udpconnector.js +76 -0
  72. package/dist/lib/connectors/udpsocket.js +93 -0
  73. package/dist/lib/filters/handler/serial.js +44 -0
  74. package/dist/lib/filters/handler/time.js +32 -0
  75. package/dist/lib/filters/handler/timeout.js +45 -0
  76. package/dist/lib/filters/handler/toobusy.js +36 -0
  77. package/dist/lib/filters/rpc/rpcLog.js +43 -0
  78. package/dist/lib/filters/rpc/toobusy.js +41 -0
  79. package/dist/lib/index.js +81 -0
  80. package/dist/lib/interfaces/IComponent.js +3 -0
  81. package/dist/lib/interfaces/IConnector.js +3 -0
  82. package/dist/lib/interfaces/IHandlerFilter.js +3 -0
  83. package/dist/lib/interfaces/ILifeCycle.js +3 -0
  84. package/dist/lib/interfaces/IPlugin.js +3 -0
  85. package/dist/lib/interfaces/IPushScheduler.js +3 -0
  86. package/dist/lib/interfaces/ISocket.js +3 -0
  87. package/dist/lib/interfaces/IStore.js +3 -0
  88. package/dist/lib/interfaces/define.js +3 -0
  89. package/dist/lib/master/master.js +129 -0
  90. package/dist/lib/master/starter.js +236 -0
  91. package/dist/lib/master/watchdog.js +120 -0
  92. package/dist/lib/melo.js +125 -0
  93. package/dist/lib/modules/console.js +436 -0
  94. package/dist/lib/modules/masterwatcher.js +98 -0
  95. package/dist/lib/modules/monitorwatcher.js +124 -0
  96. package/dist/lib/modules/onlineUser.js +69 -0
  97. package/dist/lib/modules/restartNotifyModule.js +107 -0
  98. package/dist/lib/modules/watchServer.js +737 -0
  99. package/dist/lib/monitor/monitor.js +80 -0
  100. package/dist/lib/pushSchedulers/buffer.js +96 -0
  101. package/dist/lib/pushSchedulers/direct.js +58 -0
  102. package/dist/lib/pushSchedulers/multi.js +80 -0
  103. package/dist/lib/server/server.js +500 -0
  104. package/dist/lib/util/appUtil.js +306 -0
  105. package/dist/lib/util/constants.js +117 -0
  106. package/dist/lib/util/countDownLatch.js +51 -0
  107. package/dist/lib/util/events.js +20 -0
  108. package/dist/lib/util/handlerHelper.js +8 -0
  109. package/dist/lib/util/log.js +14 -0
  110. package/dist/lib/util/moduleUtil.js +101 -0
  111. package/dist/lib/util/pathUtil.js +134 -0
  112. package/dist/lib/util/remoterHelper.js +8 -0
  113. package/dist/lib/util/utils.js +358 -0
  114. package/dist/test/application.js +522 -0
  115. package/dist/test/config/log4js.json +28 -0
  116. package/dist/test/config/master.json +9 -0
  117. package/dist/test/config/servers.json +6 -0
  118. package/dist/test/filters/handler/serial.js +41 -0
  119. package/dist/test/filters/handler/time.js +41 -0
  120. package/dist/test/filters/handler/timeout.js +41 -0
  121. package/dist/test/filters/handler/toobusy.js +57 -0
  122. package/dist/test/filters/rpc/rpcLog.js +22 -0
  123. package/dist/test/filters/rpc/toobusy.js +39 -0
  124. package/dist/test/manager/mockChannelManager.js +77 -0
  125. package/dist/test/manager/taskManager.js +68 -0
  126. package/dist/test/mock-base/app/servers/other-file +0 -0
  127. package/dist/test/mock-plugin/components/mockPlugin.js +10 -0
  128. package/dist/test/mock-plugin/events/mockEvent.js +12 -0
  129. package/dist/test/modules/console.js +242 -0
  130. package/dist/test/pomelo.js +19 -0
  131. package/dist/test/remote/channelRemote.js +159 -0
  132. package/dist/test/service/channel.js +134 -0
  133. package/dist/test/service/channelService.js +216 -0
  134. package/dist/test/service/connectionService.js +114 -0
  135. package/dist/test/service/filterService.js +144 -0
  136. package/dist/test/service/handlerService.js +65 -0
  137. package/dist/test/service/sessionService.js +387 -0
  138. package/dist/test/util/countDownLatch.js +70 -0
  139. package/dist/test/util/pathUtil.js +108 -0
  140. package/dist/test/util/utils.js +140 -0
  141. package/lib/application.ts +1240 -0
  142. package/lib/common/manager/appManager.ts +118 -0
  143. package/lib/common/manager/taskManager.ts +50 -0
  144. package/lib/common/remote/backend/msgRemote.ts +134 -0
  145. package/lib/common/remote/frontend/channelRemote.ts +91 -0
  146. package/lib/common/remote/frontend/sessionRemote.ts +91 -0
  147. package/lib/common/service/backendSessionService.ts +388 -0
  148. package/lib/common/service/channelService.ts +609 -0
  149. package/lib/common/service/connectionService.ts +112 -0
  150. package/lib/common/service/filterService.ts +118 -0
  151. package/lib/common/service/handlerService.ts +224 -0
  152. package/lib/common/service/sessionService.ts +731 -0
  153. package/lib/components/backendSession.ts +14 -0
  154. package/lib/components/channel.ts +11 -0
  155. package/lib/components/connection.ts +13 -0
  156. package/lib/components/connector.ts +533 -0
  157. package/lib/components/dictionary.ts +121 -0
  158. package/lib/components/master.ts +41 -0
  159. package/lib/components/monitor.ts +30 -0
  160. package/lib/components/protobuf.ts +208 -0
  161. package/lib/components/proxy.ts +282 -0
  162. package/lib/components/pushScheduler.ts +70 -0
  163. package/lib/components/remote.ts +166 -0
  164. package/lib/components/server.ts +71 -0
  165. package/lib/components/session.ts +22 -0
  166. package/lib/connectors/commands/handshake.ts +155 -0
  167. package/lib/connectors/commands/heartbeat.ts +83 -0
  168. package/lib/connectors/commands/kick.ts +11 -0
  169. package/lib/connectors/common/coder.ts +93 -0
  170. package/lib/connectors/common/handler.ts +62 -0
  171. package/lib/connectors/hybrid/IHybridSocket.ts +9 -0
  172. package/lib/connectors/hybrid/switcher.ts +142 -0
  173. package/lib/connectors/hybrid/tcpprocessor.ts +43 -0
  174. package/lib/connectors/hybrid/tcpsocket.ts +223 -0
  175. package/lib/connectors/hybrid/wsprocessor.ts +57 -0
  176. package/lib/connectors/hybridconnector.ts +134 -0
  177. package/lib/connectors/hybridsocket.ts +168 -0
  178. package/lib/connectors/mqtt/generate.ts +103 -0
  179. package/lib/connectors/mqtt/mqttadaptor.ts +114 -0
  180. package/lib/connectors/mqtt/protocol.ts +49 -0
  181. package/lib/connectors/mqttconnector.ts +134 -0
  182. package/lib/connectors/mqttsocket.ts +79 -0
  183. package/lib/connectors/sioconnector.ts +161 -0
  184. package/lib/connectors/siosocket.ts +85 -0
  185. package/lib/connectors/udpconnector.ts +113 -0
  186. package/lib/connectors/udpsocket.ts +110 -0
  187. package/lib/filters/handler/serial.ts +46 -0
  188. package/lib/filters/handler/time.ts +35 -0
  189. package/lib/filters/handler/timeout.ts +50 -0
  190. package/lib/filters/handler/toobusy.ts +37 -0
  191. package/lib/filters/rpc/rpcLog.ts +42 -0
  192. package/lib/filters/rpc/toobusy.ts +41 -0
  193. package/lib/index.ts +74 -0
  194. package/lib/interfaces/IComponent.ts +47 -0
  195. package/lib/interfaces/IConnector.ts +20 -0
  196. package/lib/interfaces/IHandlerFilter.ts +15 -0
  197. package/lib/interfaces/ILifeCycle.ts +16 -0
  198. package/lib/interfaces/IPlugin.ts +65 -0
  199. package/lib/interfaces/IPushScheduler.ts +52 -0
  200. package/lib/interfaces/ISocket.ts +26 -0
  201. package/lib/interfaces/IStore.ts +10 -0
  202. package/lib/interfaces/define.ts +15 -0
  203. package/lib/master/master.ts +148 -0
  204. package/lib/master/starter.ts +234 -0
  205. package/lib/master/watchdog.ts +135 -0
  206. package/lib/melo.ts +152 -0
  207. package/lib/modules/console.ts +465 -0
  208. package/lib/modules/masterwatcher.ts +120 -0
  209. package/lib/modules/monitorwatcher.ts +151 -0
  210. package/lib/modules/onlineUser.ts +78 -0
  211. package/lib/modules/restartNotifyModule.ts +128 -0
  212. package/lib/modules/watchServer.ts +766 -0
  213. package/lib/monitor/monitor.ts +99 -0
  214. package/lib/pushSchedulers/buffer.ts +117 -0
  215. package/lib/pushSchedulers/direct.ts +65 -0
  216. package/lib/pushSchedulers/multi.ts +94 -0
  217. package/lib/server/server.ts +554 -0
  218. package/lib/util/appUtil.ts +313 -0
  219. package/lib/util/constants.ts +154 -0
  220. package/lib/util/countDownLatch.ts +72 -0
  221. package/lib/util/events.ts +15 -0
  222. package/lib/util/handlerHelper.ts +5 -0
  223. package/lib/util/log.ts +11 -0
  224. package/lib/util/moduleUtil.ts +110 -0
  225. package/lib/util/pathUtil.ts +132 -0
  226. package/lib/util/remoterHelper.ts +68 -0
  227. package/lib/util/utils.ts +365 -0
  228. package/package.json +93 -0
  229. package/template/game-server/.vscode/launch.json +27 -0
  230. package/template/game-server/app/servers/connector/handler/entryHandler.ts +50 -0
  231. package/template/game-server/app/servers/connector/remote/authRemoter.ts +36 -0
  232. package/template/game-server/app.ts +30 -0
  233. package/template/game-server/app.ts.mqtt +32 -0
  234. package/template/game-server/app.ts.sio +36 -0
  235. package/template/game-server/app.ts.sio.wss +34 -0
  236. package/template/game-server/app.ts.udp +31 -0
  237. package/template/game-server/app.ts.wss +40 -0
  238. package/template/game-server/config/adminServer.json +5 -0
  239. package/template/game-server/config/adminUser.json +22 -0
  240. package/template/game-server/config/clientProtos.json +1 -0
  241. package/template/game-server/config/dictionary.json +1 -0
  242. package/template/game-server/config/log4js.json +150 -0
  243. package/template/game-server/config/master.json +8 -0
  244. package/template/game-server/config/serverProtos.json +1 -0
  245. package/template/game-server/config/servers.json +12 -0
  246. package/template/game-server/copy.js +5 -0
  247. package/template/game-server/package.json +28 -0
  248. package/template/game-server/preload.ts +40 -0
  249. package/template/game-server/tsconfig.json +36 -0
  250. package/template/npm-install.bat +4 -0
  251. package/template/npm-install.sh +5 -0
  252. package/template/readme.md +31 -0
  253. package/template/shared/server.crt +15 -0
  254. package/template/shared/server.key +15 -0
  255. package/template/web-server/app.js +25 -0
  256. package/template/web-server/app.js.https +36 -0
  257. package/template/web-server/bin/component.bat +1 -0
  258. package/template/web-server/bin/component.sh +1 -0
  259. package/template/web-server/package.json +10 -0
  260. package/template/web-server/public/css/base.css +76 -0
  261. package/template/web-server/public/image/logo.png +0 -0
  262. package/template/web-server/public/image/sp.png +0 -0
  263. package/template/web-server/public/index.html +57 -0
  264. package/template/web-server/public/index.html.sio +58 -0
  265. package/template/web-server/public/js/lib/build/build.js +1730 -0
  266. package/template/web-server/public/js/lib/build/build.js.wss +1721 -0
  267. package/template/web-server/public/js/lib/component.json +6 -0
  268. package/template/web-server/public/js/lib/local/boot/component.json +11 -0
  269. package/template/web-server/public/js/lib/local/boot/index.js +11 -0
  270. package/template/web-server/public/js/lib/meloclient.js +456 -0
  271. package/template/web-server/public/js/lib/meloclient.js.wss +456 -0
  272. package/template/web-server/public/js/lib/socket.io.js +3 -0
  273. package/test/application.ts +607 -0
  274. package/test/filters/handler/serial.ts +47 -0
  275. package/test/filters/handler/time.ts +47 -0
  276. package/test/filters/handler/timeout.ts +46 -0
  277. package/test/filters/handler/toobusy.ts +59 -0
  278. package/test/filters/rpc/rpcLog.ts +20 -0
  279. package/test/filters/rpc/toobusy.ts +40 -0
  280. package/test/manager/mockChannelManager.ts +92 -0
  281. package/test/manager/taskManager.ts +78 -0
  282. package/test/mock-base/app/servers/other-file +0 -0
  283. package/test/mock-plugin/components/mockPlugin.ts +8 -0
  284. package/test/mock-plugin/events/mockEvent.ts +12 -0
  285. package/test/modules/console.ts +264 -0
  286. package/test/pomelo.ts +18 -0
  287. package/test/remote/channelRemote.ts +176 -0
  288. package/test/service/channel.ts +161 -0
  289. package/test/service/channelService.ts +243 -0
  290. package/test/service/connectionService.ts +142 -0
  291. package/test/service/filterService.ts +165 -0
  292. package/test/service/handlerService.ts +77 -0
  293. package/test/service/sessionService.ts +464 -0
  294. package/test/util/countDownLatch.ts +81 -0
  295. package/test/util/pathUtil.ts +122 -0
  296. package/test/util/utils.ts +165 -0
@@ -0,0 +1,176 @@
1
+ import * as should from 'should';
2
+ // import { describe, it } from "mocha-typescript"
3
+ import { UID, SID } from '../../lib/util/constants';
4
+ import { ScheduleOptions } from '../../lib/interfaces/IPushScheduler';
5
+
6
+ let melo = require('../../lib/index').melo;
7
+ let remote = require('../../lib/common/remote/frontend/channelRemote').default;
8
+ let SessionService = require('../../lib/common/service/sessionService').SessionService;
9
+ let ChannelService = require('../../lib/common/service/channelService').ChannelService;
10
+ let countDownLatch = require('../../lib/util/countDownLatch').CountDownLatch;
11
+ let MockChannelManager = require('../manager/mockChannelManager').MockManager;
12
+
13
+
14
+ let mockBase = process.cwd() + '/test';
15
+
16
+ let WAIT_TIME = 200;
17
+
18
+ describe('channel remote test', function () {
19
+ describe('#pushMessage', function () {
20
+ it('should push message the the specified clients', function (done: Mocha.Done) {
21
+ this.timeout(5555)
22
+ let sids = [1, 2, 3, 4, 5, 6];
23
+ let uids = [11, 12, 13];
24
+ let frontendId = 'frontend-server-id';
25
+ let mockRoute = 'mock-route-string';
26
+ let mockMsg = { msg: 'some test msg' };
27
+ let invokeCount = 0;
28
+ let invokeUids: Array<number> = [];
29
+
30
+ let sessionService = new SessionService();
31
+ sessionService.sendMessageByUid = function (uid: number, msg: string) {
32
+ mockMsg.should.eql(msg);
33
+ invokeCount++;
34
+ invokeUids.push(uid);
35
+ };
36
+
37
+ let session;
38
+ for (let i = 0, l = sids.length, j = 0; i < l; i++) {
39
+ session = sessionService.create(sids[i], frontendId);
40
+ if (i % 2) {
41
+ sessionService.bind(session.id, uids[j]);
42
+ j++;
43
+ }
44
+ }
45
+
46
+ let app = melo.createApp({ base: mockBase });
47
+ app.components.__connector__ = {
48
+ send: function (reqId: number, route: string, msg: any, recvs: Array<SID>, opts: ScheduleOptions, cb: (err?: Error, resp?: any) => void) {
49
+ app.components.__pushScheduler__.schedule(reqId, route, msg, recvs, opts, cb);
50
+ }
51
+ };
52
+ app.components.__connector__.connector = {};
53
+ app.components.__pushScheduler__ = {
54
+ schedule: function (reqId: number, route: string, msg: any, recvs: Array<SID>, opts: ScheduleOptions, cb: (err?: Error, resp?: any) => void) {
55
+ mockMsg.should.eql(msg);
56
+ invokeCount += recvs.length;
57
+ let sess;
58
+ for (let i = 0; i < recvs.length; i++) {
59
+ sess = sessionService.get(recvs[i]);
60
+ if (sess) {
61
+ invokeUids.push(sess.uid);
62
+ }
63
+ }
64
+ cb();
65
+ }
66
+ };
67
+ app.set('sessionService', sessionService);
68
+ let channelRemote = remote(app);
69
+ channelRemote.pushMessage(mockRoute, mockMsg, uids, { isPush: true }).then(() => {
70
+ invokeCount.should.equal(uids.length);
71
+ invokeUids.length.should.equal(uids.length);
72
+ for (let i = 0, l = uids.length; i < l; i++) {
73
+ invokeUids.should.containEql(uids[i]);
74
+ }
75
+ done();
76
+ });
77
+
78
+ });
79
+ });
80
+
81
+ describe('#broadcast', function () {
82
+ it('should broadcast to all the client connected', function (done: Mocha.Done) {
83
+ let sids = [1, 2, 3, 4, 5];
84
+ let uids = [11, 12, 13, 14, 15];
85
+ let frontendId = 'frontend-server-id';
86
+ let mockRoute = 'mock-route-string';
87
+ let mockMsg = { msg: 'some test msg' };
88
+ let invokeCount = 0;
89
+
90
+ let sessionService = new SessionService();
91
+ let channelService = new ChannelService();
92
+
93
+ let session;
94
+ for (let i = 0, l = sids.length; i < l; i++) {
95
+ session = sessionService.create(sids[i], frontendId);
96
+ if (i % 2) {
97
+ session.bind(uids[i]);
98
+ }
99
+ }
100
+
101
+ let app = melo.createApp({ base: mockBase });
102
+ app.components.__connector__ = {
103
+ send: function (reqId: number, route: string, msg: any, recvs: Array<SID>, opts: ScheduleOptions, cb: (err?: Error, resp?: any) => void) {
104
+ app.components.__pushScheduler__.schedule(reqId, route, msg, recvs, opts, cb);
105
+ }
106
+ };
107
+ app.components.__connector__.connector = {};
108
+ app.components.__pushScheduler__ = {
109
+ schedule: function (reqId: number, route: string, msg: any, recvs: Array<SID>, opts: ScheduleOptions, cb: (err?: Error, resp?: any) => void) {
110
+ invokeCount++;
111
+ mockMsg.should.eql(msg);
112
+ should.exist(opts);
113
+ should.equal(opts.type, 'broadcast');
114
+ cb();
115
+ }
116
+ };
117
+ app.set('sessionService', sessionService);
118
+ app.set('channelService', channelService);
119
+ let channelRemote = remote(app);
120
+ channelRemote.broadcast(mockRoute, mockMsg, { type: 'broadcast' }).then(() => {
121
+ invokeCount.should.equal(1);
122
+ done();
123
+ });
124
+ });
125
+
126
+ it('should broadcast to all the binded client connected', function (done: Mocha.Done) {
127
+ let sids = [1, 2, 3, 4, 5, 6];
128
+ let uids = [11, 12, 13];
129
+ let frontendId = 'frontend-server-id';
130
+ let mockRoute = 'mock-route-string';
131
+ let mockMsg = { msg: 'some test msg' };
132
+ let invokeCount = 0;
133
+ let invokeUids = [];
134
+
135
+ let sessionService = new SessionService();
136
+ let channelService = new ChannelService();
137
+
138
+ let session;
139
+ for (let i = 0, l = sids.length, j = 0; i < l; i++) {
140
+ session = sessionService.create(sids[i], frontendId);
141
+ if (i % 2) {
142
+ session.bind(uids[j]);
143
+ j++;
144
+ }
145
+ }
146
+
147
+ let app = melo.createApp({ base: mockBase });
148
+ app.components.__connector__ = {
149
+ send: function (reqId: number, route: string, msg: any, recvs: Array<SID>, opts: ScheduleOptions, cb: (err?: Error, resp?: any) => void) {
150
+ app.components.__pushScheduler__.schedule(reqId, route, msg, recvs, opts, cb);
151
+ }
152
+ };
153
+ app.components.__connector__.connector = {};
154
+ app.components.__pushScheduler__ = {
155
+ schedule: function (reqId: number, route: string, msg: any, recvs: Array<SID>, opts: ScheduleOptions, cb: (err?: Error, resp?: any) => void) {
156
+ invokeCount++;
157
+ mockMsg.should.eql(msg);
158
+ should.exist(opts);
159
+ should.equal(opts.type, 'broadcast');
160
+ true.should.equal(opts.userOptions.binded);
161
+ cb();
162
+ }
163
+ };
164
+ app.set('sessionService', sessionService);
165
+ app.set('channelService', channelService);
166
+ let channelRemote = remote(app);
167
+ channelRemote.broadcast(mockRoute, mockMsg, {
168
+ type: 'broadcast',
169
+ userOptions: { binded: true }
170
+ }).then(() => {
171
+ invokeCount.should.equal(1);
172
+ done();
173
+ });
174
+ });
175
+ });
176
+ });
@@ -0,0 +1,161 @@
1
+ import * as should from 'should';
2
+ // import { describe, it } from "mocha-typescript"
3
+ let melo = require('../../lib/index').melo;
4
+ let ChannelService = require('../../lib/common/service/channelService').ChannelService;
5
+
6
+ let mockBase = process.cwd() + '/test';
7
+ let channelName = 'test_channel';
8
+ let mockApp = { serverId: 'test-server-1' };
9
+
10
+ describe('channel test', function () {
11
+ describe('#add', function () {
12
+ it('should add a member into channel and could fetch it later', function () {
13
+ let channelService = new ChannelService(mockApp);
14
+ let channel = channelService.createChannel(channelName);
15
+ should.exist(channel);
16
+
17
+ let uid = 'uid1', sid = 'sid1';
18
+ channel.add(uid, sid).should.be.true;
19
+
20
+ let member = channel.getMember(uid);
21
+ should.exist(member);
22
+ uid.should.equal(member.uid);
23
+ sid.should.equal(member.sid);
24
+ });
25
+
26
+ it('should fail if the sid not specified', function () {
27
+ let channelService = new ChannelService(mockApp);
28
+ let channel = channelService.createChannel(channelName);
29
+ should.exist(channel);
30
+
31
+ let uid = 'uid1';
32
+ channel.add(uid, null).should.be.false;
33
+ });
34
+
35
+ it('should fail after the channel has been destroied', function () {
36
+ let channelService = new ChannelService(mockApp);
37
+ let channel = channelService.createChannel(channelName);
38
+ should.exist(channel);
39
+
40
+ channel.destroy();
41
+
42
+ let uid = 'uid1', sid = 'sid1';
43
+ channel.add(uid, sid).should.be.false;
44
+ });
45
+ });
46
+
47
+ describe('#leave', function () {
48
+ it('should remove the member from channel when leave', function () {
49
+ let channelService = new ChannelService(mockApp);
50
+ let channel = channelService.createChannel(channelName);
51
+ should.exist(channel);
52
+
53
+ let uid = 'uid1', sid = 'sid1';
54
+ channel.add(uid, sid).should.be.true;
55
+
56
+ let member = channel.getMember(uid);
57
+ should.exist(member);
58
+
59
+ channel.leave(uid, sid);
60
+ member = channel.getMember(uid);
61
+ should.not.exist(member);
62
+ });
63
+
64
+ it('should fail if uid or sid not specified', function () {
65
+ let channelService = new ChannelService(mockApp);
66
+ let channel = channelService.createChannel(channelName);
67
+ should.exist(channel);
68
+
69
+ let uid = 'uid1', sid = 'sid1';
70
+ channel.add(uid, sid).should.be.true;
71
+
72
+ channel.leave(uid, null).should.be.false;
73
+ channel.leave(null, sid).should.be.false;
74
+ });
75
+ });
76
+
77
+ describe('#getMembers', function () {
78
+ it('should return all the members of channel', function () {
79
+ let uinfos = [
80
+ { uid: 'uid1', sid: 'sid1' },
81
+ { uid: 'uid2', sid: 'sid2' },
82
+ { uid: 'uid3', sid: 'sid3' }
83
+ ];
84
+
85
+ let channelService = new ChannelService(mockApp);
86
+ let channel = channelService.createChannel(channelName);
87
+
88
+ let i, l, item;
89
+ for (i = 0, l = uinfos.length; i < l; i++) {
90
+ item = uinfos[i];
91
+ channel.add(item.uid, item.sid);
92
+ }
93
+
94
+ let members = channel.getMembers();
95
+ should.exist(members);
96
+ members.length.should.equal(uinfos.length);
97
+ for (i = 0, l = uinfos.length; i < l; i++) {
98
+ item = uinfos[i];
99
+ members.should.containEql(item.uid);
100
+ }
101
+ });
102
+ });
103
+
104
+ describe('#pushMessage', function () {
105
+ it('should push message to the right frontend server by sid', function (done: Mocha.Done) {
106
+ let sid1 = 'sid1', sid2 = 'sid2';
107
+ let uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
108
+ let mockUids = [{ sid: sid1, uid: uid1 }, { sid: sid2, uid: uid2 }, { sid: sid2, uid: uid3 }];
109
+ let mockMsg = { key: 'some remote message' };
110
+ let uidMap: { [key: string]: any } = {};
111
+ for (let i in mockUids) {
112
+ uidMap[mockUids[i].uid] = mockUids[i];
113
+ }
114
+
115
+ let invokeCount = 0;
116
+
117
+ let mockRpcInvoke = function (sid: string, rmsg: { [key: string]: any }, cb: Function) {
118
+ invokeCount++;
119
+ let args = rmsg.args;
120
+ let route = args[0];
121
+ let msg = args[1];
122
+ let uids = args[2];
123
+ mockMsg.should.eql(msg);
124
+
125
+ for (let j = 0, l = uids.length; j < l; j++) {
126
+ let uid = uids[j];
127
+ let r2 = uidMap[uid];
128
+ r2.sid.should.equal(sid);
129
+ }
130
+
131
+ cb();
132
+ };
133
+
134
+ let app = melo.createApp({ base: mockBase });
135
+ app.rpcInvoke = mockRpcInvoke;
136
+ let channelService = new ChannelService(app);
137
+
138
+ let channel = channelService.createChannel(channelName);
139
+ for (let i = 0, l = mockUids.length; i < l; i++) {
140
+ channel.add(mockUids[i].uid, mockUids[i].sid);
141
+ }
142
+
143
+ channel.pushMessage(mockMsg, function () {
144
+ invokeCount.should.equal(2);
145
+ done();
146
+ });
147
+ });
148
+ it('should fail if channel has destroied', function () {
149
+ let channelService = new ChannelService(mockApp);
150
+ let channel = channelService.createChannel(channelName);
151
+ should.exist(channel);
152
+
153
+ channel.destroy();
154
+
155
+ channel.pushMessage({}, function (err: Error) {
156
+ should.exist(err);
157
+ err.message.should.equal('channel is not running now');
158
+ });
159
+ });
160
+ });
161
+ });
@@ -0,0 +1,243 @@
1
+ import * as should from 'should';
2
+ // import { describe, it } from "mocha-typescript"
3
+ let melo = require('../../lib/index').melo;
4
+ let ChannelService = require('../../lib/common/service/channelService').ChannelService;
5
+
6
+ let channelName = 'test_channel';
7
+ let mockBase = process.cwd() + '/test';
8
+ let mockApp = { serverId: 'test-server-1' };
9
+
10
+ describe('channel manager test', function () {
11
+ describe('#createChannel', function () {
12
+ it('should create and return a channel with the specified name', function () {
13
+ let channelService = new ChannelService(mockApp);
14
+ let channel = channelService.createChannel(channelName);
15
+ should.exist(channel);
16
+ channelName.should.equal(channel.name);
17
+ });
18
+
19
+ it('should return the same channel if the name has already existed', function () {
20
+ let channelService = new ChannelService(mockApp);
21
+ let channel = channelService.createChannel(channelName);
22
+ should.exist(channel);
23
+ channelName.should.equal(channel.name);
24
+ let channel2 = channelService.createChannel(channelName);
25
+ channel.should.equal(channel2);
26
+ });
27
+ });
28
+
29
+ describe('#destroyChannel', function () {
30
+ it('should delete the channel instance', function () {
31
+ let channelService = new ChannelService(mockApp);
32
+ let channel = channelService.createChannel(channelName);
33
+ should.exist(channel);
34
+ channelName.should.equal(channel.name);
35
+ channelService.destroyChannel(channelName);
36
+ let channel2 = channelService.createChannel(channelName);
37
+ channel.should.not.equal(channel2);
38
+ });
39
+ });
40
+
41
+ describe('#getChannel', function () {
42
+ it('should return the channel with the specified name if it exists', function () {
43
+ let channelService = new ChannelService(mockApp);
44
+ channelService.createChannel(channelName);
45
+ let channel = channelService.getChannel(channelName);
46
+ should.exist(channel);
47
+ channelName.should.equal(channel.name);
48
+ });
49
+
50
+ it('should return undefined if the channel dose not exist', function () {
51
+ let channelService = new ChannelService(mockApp);
52
+ let channel = channelService.getChannel(channelName);
53
+ should.not.exist(channel);
54
+ });
55
+
56
+ it('should create and return a new channel if create parameter is set', function () {
57
+ let channelService = new ChannelService(mockApp);
58
+ let channel = channelService.getChannel(channelName, true);
59
+ should.exist(channel);
60
+ channelName.should.equal(channel.name);
61
+ });
62
+ });
63
+
64
+ describe('#pushMessageByUids', function () {
65
+ it('should push message to the right frontend server', function (done: Mocha.Done) {
66
+ let sid1 = 'sid1', sid2 = 'sid2';
67
+ let uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
68
+ let orgRoute = 'test.route.string';
69
+ let mockUids = [
70
+ { sid: sid1, uid: uid1 },
71
+ { sid: sid2, uid: uid2 },
72
+ { sid: sid2, uid: uid3 }
73
+ ];
74
+ let mockMsg = { key: 'some remote message' };
75
+ let uidMap: { [key: string]: any } = {};
76
+ for (let i in mockUids) {
77
+ uidMap[mockUids[i].uid] = mockUids[i];
78
+ }
79
+
80
+ let invokeCount = 0;
81
+
82
+ let mockRpcInvoke = function (sid: string, rmsg: { [key: string]: any }, cb: Function) {
83
+ invokeCount++;
84
+ let args = rmsg.args;
85
+ let route = args[0];
86
+ let msg = args[1];
87
+ let uids = args[2];
88
+ mockMsg.should.eql(msg);
89
+
90
+ for (let j = 0, l = uids.length; j < l; j++) {
91
+ let uid = uids[j];
92
+ let r2 = uidMap[uid];
93
+ r2.sid.should.equal(sid);
94
+ }
95
+
96
+ cb();
97
+ };
98
+
99
+ let app = melo.createApp({ base: mockBase });
100
+ app.rpcInvoke = mockRpcInvoke;
101
+ let channelService = new ChannelService(app);
102
+
103
+ channelService.pushMessageByUids(orgRoute, mockMsg, mockUids, function () {
104
+ invokeCount.should.equal(2);
105
+ done();
106
+ });
107
+ });
108
+
109
+ it('should return an err if uids is empty', function (done: Mocha.Done) {
110
+ let mockMsg = { key: 'some remote message' };
111
+ let app = melo.createApp({ base: mockBase });
112
+ let channelService = new ChannelService(app);
113
+
114
+ channelService.pushMessageByUids(mockMsg, null, function (err: Error) {
115
+ should.exist(err);
116
+ err.message.should.equal('uids should not be empty');
117
+ done();
118
+ });
119
+ });
120
+
121
+ it('should return err if all message fail to push', function (done: Mocha.Done) {
122
+ let sid1 = 'sid1', sid2 = 'sid2';
123
+ let uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
124
+ let mockUids = [
125
+ { sid: sid1, uid: uid1 },
126
+ { sid: sid2, uid: uid2 },
127
+ { sid: sid2, uid: uid3 }
128
+ ];
129
+ let mockMsg = { key: 'some remote message' };
130
+ let uidMap: { [key: string]: any } = {};
131
+ for (let i in mockUids) {
132
+ uidMap[mockUids[i].uid] = mockUids[i];
133
+ }
134
+
135
+ let invokeCount = 0;
136
+
137
+ let mockRpcInvoke = function (sid: string, rmsg: { [key: string]: any }, cb: (parameter: Error) => void) {
138
+ invokeCount++;
139
+ cb(new Error('[TestMockError] mock rpc error'));
140
+ };
141
+
142
+ let app = melo.createApp({ base: mockBase });
143
+ app.rpcInvoke = mockRpcInvoke;
144
+ let channelService = new ChannelService(app);
145
+
146
+ channelService.pushMessageByUids(mockMsg, mockUids, function (err: Error) {
147
+ invokeCount.should.equal(2);
148
+ should.exist(err);
149
+ err.message.should.equal('all uids push message fail');
150
+ done();
151
+ });
152
+ });
153
+
154
+ it('should return fail uid list if fail to push messge to some of the uids', function (done: Mocha.Done) {
155
+ let sid1 = 'sid1', sid2 = 'sid2';
156
+ let uid1 = 'uid1', uid2 = 'uid2', uid3 = 'uid3';
157
+ let mockUids = [{ sid: sid1, uid: uid1 }, { sid: sid2, uid: uid2 }, { sid: sid2, uid: uid3 }];
158
+ let mockMsg = { key: 'some remote message' };
159
+ let uidMap: { [key: string]: any } = {};
160
+ for (let i in mockUids) {
161
+ uidMap[mockUids[i].uid] = mockUids[i];
162
+ }
163
+
164
+ let invokeCount = 0;
165
+
166
+ let mockRpcInvoke = function (sid: string, rmsg: { [key: string]: any }, cb: Function) {
167
+ invokeCount++;
168
+ if (rmsg.args[2].indexOf(uid1) >= 0) {
169
+ cb(null, [uid1]);
170
+ } else if (rmsg.args[2].indexOf(uid3) >= 0) {
171
+ cb(null, [uid3]);
172
+ } else {
173
+ cb();
174
+ }
175
+ };
176
+
177
+ let app = melo.createApp({ base: mockBase });
178
+ app.rpcInvoke = mockRpcInvoke;
179
+ let channelService = new ChannelService(app);
180
+
181
+ channelService.pushMessageByUids(mockMsg, mockUids, function (err: Error, fails: Array<any>) {
182
+ invokeCount.should.equal(2);
183
+ should.not.exist(err);
184
+ should.exist(fails);
185
+ fails.length.should.equal(2);
186
+ fails.should.containEql(uid1);
187
+ fails.should.containEql(uid3);
188
+ done();
189
+ });
190
+ });
191
+ });
192
+
193
+ describe('#broadcast', function () {
194
+ it('should push message to all specified frontend servers', function (done: Mocha.Done) {
195
+ let mockServers = [
196
+ { id: 'connector-1', serverType: 'connector', other: 'xxx1' },
197
+ { id: 'connector-2', serverType: 'connector', other: 'xxx2' },
198
+ { id: 'area-1', serverType: 'area', other: 'yyy1' },
199
+ { id: 'gate-1', serverType: 'gate', other: 'zzz1' },
200
+ { id: 'gate-2', serverType: 'gate', other: 'xxx1' },
201
+ { id: 'gate-3', serverType: 'gate', other: 'yyy1' }
202
+ ];
203
+ let connectorIds = ['connector-1', 'connector-2'];
204
+ let mockSType = 'connector';
205
+ let mockRoute = 'test.route.string';
206
+ let mockBinded = true;
207
+ let opts = { binded: mockBinded };
208
+ let mockMsg = { key: 'some remote message' };
209
+
210
+ let invokeCount = 0;
211
+ let sids: Array<number> = [];
212
+
213
+ let mockRpcInvoke = function (sid: number, rmsg: { [key: string]: any }, cb: Function) {
214
+ invokeCount++;
215
+ let args = rmsg.args;
216
+ let route = args[0];
217
+ let msg = args[1];
218
+ let opts = args[2];
219
+ mockMsg.should.eql(msg);
220
+ mockRoute.should.equal(route);
221
+ should.exist(opts);
222
+ mockBinded.should.equal(opts.userOptions.binded);
223
+ sids.push(sid);
224
+ cb();
225
+ };
226
+
227
+ let app = melo.createApp({ base: mockBase });
228
+ app.rpcInvoke = mockRpcInvoke;
229
+ app.addServers(mockServers);
230
+ let channelService = new ChannelService(app);
231
+
232
+ channelService.broadcast(mockSType, mockRoute, mockMsg,
233
+ opts, function () {
234
+ invokeCount.should.equal(2);
235
+ sids.length.should.equal(connectorIds.length);
236
+ for (let i = 0, l = connectorIds.length; i < l; i++) {
237
+ sids.should.containEql(connectorIds[i]);
238
+ }
239
+ done();
240
+ });
241
+ });
242
+ });
243
+ });