@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
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012-2014 Netease, Inc. and other pomelo contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+
2
+
3
+ [![Build Status](https://travis-ci.org/node-melo/melo.svg?branch=master)](https://travis-ci.org/node-melo/melo)
4
+
5
+ 示例工程请参见:https://github.com/node-melo/melo-example
6
+
7
+ 手动安装:
8
+ npm install melo -g
9
+
10
+ mkdir testProject
11
+ cd testProject
12
+ 初始化项目
13
+ melo init
14
+
15
+
16
+ ## Melo -- a fast, scalable game server framework for node.js
17
+
18
+ Melo is a fast, scalable game server framework for [node.js](http://nodejs.org).
19
+ It provides the basic development framework and many related components, including libraries and tools.
20
+ Melo is also suitable for real-time web applications; its distributed architecture makes melo scale better than other real-time web frameworks.
21
+
22
+ ## Features
23
+
24
+ ### Complete support of game server and realtime application server architecture
25
+
26
+ * Multiple-player game: mobile, social, web, MMO rpg(middle size)
27
+ * Realtime application: chat, message push, etc.
28
+
29
+ ### Fast, scalable
30
+
31
+ * Distributed (multi-process) architecture, can be easily scale up
32
+ * Flexible server extension
33
+ * Full performance optimization and test
34
+
35
+ ### Easy
36
+
37
+ * Simple API: request, response, broadcast, etc.
38
+ * Lightweight: high development efficiency based on node.js
39
+ * Convention over configuration: almost zero config
40
+
41
+ ### Powerful
42
+
43
+ * Many clients support, including javascript, flash, android, iOS, cocos2d-x, C
44
+ * Many libraries and tools, including command line tool, admin tool, performance test tool, AI, path finding etc.
45
+ * Good reference materials: full docs, many examples and [an open-source MMO RPG demo](https://github.com/NetEase/melo/wiki/Introduction-to--Lord-of-Melo)
46
+
47
+ ### Extensible
48
+
49
+ * Support plugin architecture, easy to add new features through plugins. We also provide many plugins like online status, master high availability.
50
+ * Custom features, users can define their own network protocol, custom components very easy.
51
+
52
+ ## Why should I use melo?
53
+ Fast, scalable, real-time game server development is not an easy job, and a good container or framework can reduce its complexity.
54
+ Unfortunately, unlike web, finding a game server framework solution is difficult, especially an open source solution. Melo fills this gap, providing a full solution for building game server frameworks.
55
+
@@ -0,0 +1,10 @@
1
+
2
+ import * as program from 'commander';
3
+ import stop from './commands/stop'
4
+
5
+ program.version("1.2.3");
6
+
7
+ stop(program)
8
+
9
+ process.argv.push("stop","-P","3006","server1","server2")
10
+ program.parse(process.argv)
@@ -0,0 +1,43 @@
1
+
2
+
3
+ import { Command } from 'commander';
4
+ import { connectToMaster } from '../utils/utils';
5
+ import { ConsoleModule as co } from '../../lib/modules/console';
6
+ import { DEFAULT_USERNAME, DEFAULT_PWD, DEFAULT_MASTER_HOST, DEFAULT_MASTER_PORT, ADD_SERVER_INFO } from '../utils/constants';
7
+
8
+
9
+ export default function (program: Command) {
10
+ program.command('add')
11
+ .description('add a new server')
12
+ .option('-u, --username <username>', 'administration user name', DEFAULT_USERNAME)
13
+ .option('-p, --password <password>', 'administration password', DEFAULT_PWD)
14
+ .option('-h, --host <master-host>', 'master server host', DEFAULT_MASTER_HOST)
15
+ .option('-P, --port <master-port>', 'master server port', (value)=>parseInt(value), DEFAULT_MASTER_PORT)
16
+ .action(function () {
17
+ let args = [].slice.call(arguments, 0);
18
+ let opts = args[args.length - 1];
19
+ opts.args = args.slice(0, -1);
20
+ add(opts);
21
+ });
22
+ }
23
+
24
+
25
+ /**
26
+ * Add server to application.
27
+ *
28
+ * @param {Object} opts options for `add` operation
29
+ */
30
+ function add(opts: any) {
31
+ let id = 'melo_add_' + Date.now();
32
+ connectToMaster(id, opts, function (client) {
33
+ client.request(co.moduleId, { signal: 'add', args: opts.args }, function (err: Error) {
34
+ if (err) {
35
+ console.error(err);
36
+ }
37
+ else {
38
+ console.info(ADD_SERVER_INFO);
39
+ }
40
+ process.exit(0);
41
+ });
42
+ });
43
+ }
@@ -0,0 +1,292 @@
1
+ import { CUR_DIR, INIT_PROJ_NOTICE, TIME_INIT, FILEREAD_ERROR } from '../utils/constants';
2
+ import { confirm, abort, version, prompt } from '../utils/utils';
3
+
4
+ import * as fs from 'fs';
5
+ import * as os from 'os';
6
+ import * as path from 'path';
7
+ import * as util from 'util';
8
+ import * as mkdirp from 'mkdirp';
9
+ import { Command } from 'commander';
10
+
11
+ export default function (program: Command) {
12
+ program.command('init [path]')
13
+ .description('create a new application')
14
+ .action(function (path) {
15
+ init(path || CUR_DIR);
16
+ });
17
+ }
18
+
19
+ /**
20
+ * Get user's choice on connector selecting
21
+ *
22
+ * @param {Function} cb
23
+ */
24
+ function connectorType(cb: Function) {
25
+ prompt('Please select underly connector, 1 for websocket(native socket), 2 for socket.io, 3 for wss, 4 for socket.io(wss), 5 for udp, 6 for mqtt: [1]', function (msg: string) {
26
+ console.log('selected' , msg);
27
+ switch (msg.trim()) {
28
+ case '':
29
+ cb(1);
30
+ break;
31
+ case '1':
32
+ case '2':
33
+ case '3':
34
+ case '4':
35
+ case '5':
36
+ case '6':
37
+ cb(msg.trim());
38
+ break;
39
+ default:
40
+ console.log(('Invalid choice! Please input 1 - 5.' as any).red + '\n');
41
+ connectorType(cb);
42
+ break;
43
+ }
44
+ });
45
+ }
46
+
47
+ /**
48
+ * Check if the given directory `path` is empty.
49
+ *
50
+ * @param {String} path
51
+ * @param {Function} fn
52
+ */
53
+ export function emptyDirectory(path: string) {
54
+ if(!fs.existsSync(path))
55
+ return true;
56
+ let files = fs.readdirSync(path);
57
+ return (!files || !files.length);
58
+ }
59
+
60
+ /**
61
+ * Init application at the given directory `path`.
62
+ *
63
+ * @param {String} path
64
+ */
65
+ function init(path: string) {
66
+ console.log(INIT_PROJ_NOTICE);
67
+ connectorType(function (type: string) {
68
+ let empty = emptyDirectory(path);
69
+ if (empty) {
70
+ process.stdin.destroy();
71
+ console.log('start createApplication');
72
+ createApplicationAt(path, type);
73
+ } else {
74
+ confirm('Destination is not empty, continue? (y/n) [no] ', function (force: boolean) {
75
+ process.stdin.destroy();
76
+ if (force) {
77
+ createApplicationAt(path, type);
78
+ } else {
79
+ abort(('Fail to init a project' as any).red);
80
+ }
81
+ });
82
+ }
83
+ });
84
+ }
85
+
86
+
87
+ /**
88
+ * Mkdir -p.
89
+ *
90
+ * @param {String} path
91
+ * @param {Function} fn
92
+ */
93
+ function mkdir(path: string) {
94
+ let err = mkdirp.sync(path, 0o755);
95
+
96
+ console.log((' create : ' as any).green + path);
97
+
98
+ }
99
+ /**
100
+ * Copy template files to project.
101
+ *
102
+ * @param {String} origin
103
+ * @param {String} target
104
+ */
105
+ function copy(origin: string, target: string) {
106
+ if (!fs.existsSync(origin)) {
107
+ abort(origin + 'does not exist.');
108
+ }
109
+ if (!fs.existsSync(target)) {
110
+ mkdir(target);
111
+ console.log((' create : ' as any).green + target);
112
+ }
113
+ let datalist = fs.readdirSync(origin);
114
+
115
+ for (let i = 0; i < datalist.length; i++) {
116
+ let oCurrent = path.resolve(origin, datalist[i]);
117
+ let tCurrent = path.resolve(target, datalist[i]);
118
+ if (fs.statSync(oCurrent).isFile()) {
119
+ console.log((' create : ' as any).green + tCurrent + ' from : ' + oCurrent);
120
+ fs.writeFileSync(tCurrent, fs.readFileSync(oCurrent));
121
+ } else if (fs.statSync(oCurrent).isDirectory()) {
122
+ copy(oCurrent, tCurrent);
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Create directory and files at the given directory `path`.
129
+ *
130
+ * @param {String} ph
131
+ */
132
+ function createApplicationAt(ph: string, type: string) {
133
+ let name = path.basename(path.resolve(CUR_DIR, ph));
134
+ copy(path.join(__dirname, '../../../template/'), ph);
135
+ mkdir(path.join(ph, 'game-server/dist/logs'));
136
+ mkdir(path.join(ph, 'shared'));
137
+ // rmdir -r
138
+ let rmdir = function (dir: string) {
139
+ let list = fs.readdirSync(dir);
140
+ for (let i = 0; i < list.length; i++) {
141
+ let filename = path.join(dir, list[i]);
142
+ let stat = fs.statSync(filename);
143
+ if (filename === '.' || filename === '..') {
144
+ } else if (stat.isDirectory()) {
145
+ rmdir(filename);
146
+ } else {
147
+ fs.unlinkSync(filename);
148
+ }
149
+ }
150
+ fs.rmdirSync(dir);
151
+ };
152
+ let unlinkFiles: string[];
153
+ switch (type) {
154
+ case '1':
155
+ // use websocket
156
+ unlinkFiles = ['game-server/app.ts.sio',
157
+ 'game-server/app.ts.wss',
158
+ 'game-server/app.ts.mqtt',
159
+ 'game-server/app.ts.sio.wss',
160
+ 'game-server/app.ts.udp',
161
+ 'web-server/app.js.https',
162
+ 'web-server/public/index.html.sio',
163
+ 'web-server/public/js/lib/meloclient.js',
164
+ 'web-server/public/js/lib/meloclient.js.wss',
165
+ 'web-server/public/js/lib/build/build.js.wss',
166
+ 'web-server/public/js/lib/socket.io.js'];
167
+ for (let i = 0; i < unlinkFiles.length; ++i) {
168
+ let f = path.resolve(ph, unlinkFiles[i]);
169
+ console.log('delete : ' + f);
170
+ fs.unlinkSync(f);
171
+ }
172
+ break;
173
+ case '2':
174
+ // use socket.io
175
+ unlinkFiles = ['game-server/app.ts',
176
+ 'game-server/app.ts.wss',
177
+ 'game-server/app.ts.udp',
178
+ 'game-server/app.ts.mqtt',
179
+ 'game-server/app.ts.sio.wss',
180
+ 'web-server/app.js.https',
181
+ 'web-server/public/index.html',
182
+ 'web-server/public/js/lib/component.json',
183
+ 'web-server/public/js/lib/meloclient.js.wss'];
184
+ for (let i = 0; i < unlinkFiles.length; ++i) {
185
+ fs.unlinkSync(path.resolve(ph, unlinkFiles[i]));
186
+ }
187
+
188
+ fs.renameSync(path.resolve(ph, 'game-server/app.ts.sio'), path.resolve(ph, 'game-server/app.ts'));
189
+ fs.renameSync(path.resolve(ph, 'web-server/public/index.html.sio'), path.resolve(ph, 'web-server/public/index.html'));
190
+
191
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/build'));
192
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/local'));
193
+ break;
194
+ case '3':
195
+ // use websocket wss
196
+ unlinkFiles = ['game-server/app.ts.sio',
197
+ 'game-server/app.ts',
198
+ 'game-server/app.ts.udp',
199
+ 'game-server/app.ts.sio.wss',
200
+ 'game-server/app.ts.mqtt',
201
+ 'web-server/app.js',
202
+ 'web-server/public/index.html.sio',
203
+ 'web-server/public/js/lib/meloclient.js',
204
+ 'web-server/public/js/lib/meloclient.js.wss',
205
+ 'web-server/public/js/lib/build/build.js',
206
+ 'web-server/public/js/lib/socket.io.js'];
207
+ for (let i = 0; i < unlinkFiles.length; ++i) {
208
+ fs.unlinkSync(path.resolve(ph, unlinkFiles[i]));
209
+ }
210
+
211
+ fs.renameSync(path.resolve(ph, 'game-server/app.ts.wss'), path.resolve(ph, 'game-server/app.ts'));
212
+ fs.renameSync(path.resolve(ph, 'web-server/app.js.https'), path.resolve(ph, 'web-server/app.js'));
213
+ fs.renameSync(path.resolve(ph, 'web-server/public/js/lib/build/build.js.wss'), path.resolve(ph, 'web-server/public/js/lib/build/build.js'));
214
+ break;
215
+ case '4':
216
+ // use socket.io wss
217
+ unlinkFiles = ['game-server/app.ts.sio',
218
+ 'game-server/app.ts',
219
+ 'game-server/app.ts.udp',
220
+ 'game-server/app.ts.wss',
221
+ 'game-server/app.ts.mqtt',
222
+ 'web-server/app.js',
223
+ 'web-server/public/index.html',
224
+ 'web-server/public/js/lib/meloclient.js'];
225
+ for (let i = 0; i < unlinkFiles.length; ++i) {
226
+ fs.unlinkSync(path.resolve(ph, unlinkFiles[i]));
227
+ }
228
+
229
+ fs.renameSync(path.resolve(ph, 'game-server/app.ts.sio.wss'), path.resolve(ph, 'game-server/app.ts'));
230
+ fs.renameSync(path.resolve(ph, 'web-server/app.js.https'), path.resolve(ph, 'web-server/app.js'));
231
+ fs.renameSync(path.resolve(ph, 'web-server/public/index.html.sio'), path.resolve(ph, 'web-server/public/index.html'));
232
+ fs.renameSync(path.resolve(ph, 'web-server/public/js/lib/meloclient.js.wss'), path.resolve(ph, 'web-server/public/js/lib/meloclient.js'));
233
+
234
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/build'));
235
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/local'));
236
+ fs.unlinkSync(path.resolve(ph, 'web-server/public/js/lib/component.json'));
237
+ break;
238
+ case '5':
239
+ // use socket.io wss
240
+ unlinkFiles = ['game-server/app.ts.sio',
241
+ 'game-server/app.ts',
242
+ 'game-server/app.ts.wss',
243
+ 'game-server/app.ts.mqtt',
244
+ 'game-server/app.ts.sio.wss',
245
+ 'web-server/app.js.https',
246
+ 'web-server/public/index.html',
247
+ 'web-server/public/js/lib/component.json',
248
+ 'web-server/public/js/lib/meloclient.js.wss'];
249
+ for (let i = 0; i < unlinkFiles.length; ++i) {
250
+ fs.unlinkSync(path.resolve(ph, unlinkFiles[i]));
251
+ }
252
+
253
+ fs.renameSync(path.resolve(ph, 'game-server/app.ts.udp'), path.resolve(ph, 'game-server/app.ts'));
254
+ fs.renameSync(path.resolve(ph, 'web-server/public/index.html.sio'), path.resolve(ph, 'web-server/public/index.html'));
255
+
256
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/build'));
257
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/local'));
258
+ break;
259
+ case '6':
260
+ // use socket.io
261
+ unlinkFiles = ['game-server/app.ts',
262
+ 'game-server/app.ts.wss',
263
+ 'game-server/app.ts.udp',
264
+ 'game-server/app.ts.sio',
265
+ 'game-server/app.ts.sio.wss',
266
+ 'web-server/app.js.https',
267
+ 'web-server/public/index.html',
268
+ 'web-server/public/js/lib/component.json',
269
+ 'web-server/public/js/lib/meloclient.js.wss'];
270
+ for (let i = 0; i < unlinkFiles.length; ++i) {
271
+ fs.unlinkSync(path.resolve(ph, unlinkFiles[i]));
272
+ }
273
+
274
+ fs.renameSync(path.resolve(ph, 'game-server/app.ts.mqtt'), path.resolve(ph, 'game-server/app.ts'));
275
+ fs.renameSync(path.resolve(ph, 'web-server/public/index.html.sio'), path.resolve(ph, 'web-server/public/index.html'));
276
+
277
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/build'));
278
+ rmdir(path.resolve(ph, 'web-server/public/js/lib/local'));
279
+ break;
280
+ }
281
+ let replaceFiles = ['game-server/app.ts',
282
+ 'game-server/package.json',
283
+ 'web-server/package.json'];
284
+ for (let j = 0; j < replaceFiles.length; j++) {
285
+ let str = fs.readFileSync(path.resolve(ph, replaceFiles[j])).toString();
286
+ fs.writeFileSync(path.resolve(ph, replaceFiles[j]), str.replace(/__melo_app_name__/g, name));
287
+ }
288
+ let f = path.resolve(ph, 'game-server/package.json');
289
+ let content = fs.readFileSync(f).toString();
290
+ fs.writeFileSync(f, content.replace(/__melo_app_version__/g, version));
291
+ }
292
+
@@ -0,0 +1,21 @@
1
+
2
+ import { connectToMaster, terminal } from '../utils/utils';
3
+ import { ConsoleModule as co } from '../../lib/modules/console';
4
+ import { DEFAULT_USERNAME, DEFAULT_PWD, DEFAULT_MASTER_HOST, DEFAULT_MASTER_PORT, ADD_SERVER_INFO } from '../utils/constants';
5
+ import { Command } from 'commander';
6
+
7
+ export default function (program: Command) {
8
+ program.command('kill')
9
+ .description('kill the application')
10
+ .option('-u, --username <username>', 'administration user name', DEFAULT_USERNAME)
11
+ .option('-p, --password <password>', 'administration password', DEFAULT_PWD)
12
+ .option('-h, --host <master-host>', 'master server host', DEFAULT_MASTER_HOST)
13
+ .option('-P, --port <master-port>', 'master server port', (value)=>parseInt(value), DEFAULT_MASTER_PORT)
14
+ .option('-f, --force', 'using this option would kill all the node processes')
15
+ .action(function () {
16
+ let args = [].slice.call(arguments, 0);
17
+ let opts = args[args.length - 1];
18
+ opts.serverIds = args.slice(0, -1);
19
+ terminal('kill', opts);
20
+ });
21
+ }
@@ -0,0 +1,60 @@
1
+ import { DEFAULT_USERNAME, DEFAULT_PWD, DEFAULT_MASTER_HOST, DEFAULT_MASTER_PORT } from '../utils/constants';
2
+ import { connectToMaster } from '../utils/utils';
3
+ import {AdminClient} from '@bigtyphoon/melo-admin';
4
+ import * as colors from 'colors';
5
+ // @ts-ignore
6
+ import * as pc from 'pretty-columns';
7
+ import { ConsoleModule as co } from '../../lib/modules/console';
8
+ import { Command } from 'commander';
9
+
10
+ export default function (program: Command) {
11
+ program.command('list')
12
+ .description('list the servers')
13
+ .option('-u, --username <username>', 'administration user name', DEFAULT_USERNAME)
14
+ .option('-p, --password <password>', 'administration password', DEFAULT_PWD)
15
+ .option('-h, --host <master-host>', 'master server host', DEFAULT_MASTER_HOST)
16
+ .option('-P, --port <master-port>', 'master server port', (value)=>parseInt(value), DEFAULT_MASTER_PORT)
17
+ .action(function (opts) {
18
+ list(opts);
19
+ });
20
+ }
21
+ /**
22
+ * List melo processes.
23
+ *
24
+ * @param {Object} opts options for `list` operation
25
+ */
26
+ function list(opts: any) {
27
+ let id = 'melo_list_' + Date.now();
28
+ connectToMaster(id, opts, function (client: AdminClient) {
29
+ client.request(co.moduleId, { signal: 'list' }, function (err: Error, data: any) {
30
+ if (err) {
31
+ console.error(err);
32
+ }
33
+ let servers: any[] = [];
34
+ for (let key in data.msg) {
35
+ servers.push(data.msg[key]);
36
+ }
37
+ let comparer = function (a: any, b: any) {
38
+ if (a.serverType < b.serverType) {
39
+ return -1;
40
+ } else if (a.serverType > b.serverType) {
41
+ return 1;
42
+ } else if (a.serverId < b.serverId) {
43
+ return -1;
44
+ } else if (a.serverId > b.serverId) {
45
+ return 1;
46
+ } else {
47
+ return 0;
48
+ }
49
+ };
50
+ servers.sort(comparer);
51
+ let rows: string[][] = [];
52
+ rows.push([ colors.red('serverId'), colors.blue('serverType'), colors.green('pid'), colors.cyan('rss(M)'), colors.magenta('heapTotal(M)'), colors.white('heapUsed(M)'), colors.yellow('uptime(m)')]);
53
+ servers.forEach(function (server) {
54
+ rows.push([server.serverId, server.serverType, server.pid, server.rss, server.heapTotal, server.heapUsed, server.uptime]);
55
+ });
56
+ pc.output(rows);
57
+ process.exit(0);
58
+ });
59
+ });
60
+ }
@@ -0,0 +1,40 @@
1
+
2
+
3
+
4
+ import * as fs from 'fs';
5
+ import * as os from 'os';
6
+ import * as path from 'path';
7
+ import * as constants from '../../lib/util/constants';
8
+ import { connectToMaster, abort, runServer } from '../utils/utils';
9
+ import { ConsoleModule as co } from '../../lib/modules/console';
10
+ import { DEFAULT_USERNAME, DEFAULT_PWD, DEFAULT_MASTER_HOST, DEFAULT_MASTER_PORT, ADD_SERVER_INFO, DEFAULT_GAME_SERVER_DIR, MASTER_HA_NOT_FOUND } from '../utils/constants';
11
+ import { Command } from 'commander';
12
+
13
+ export default function (program: Command) {
14
+ program.command('masterha')
15
+ .description('start all the slaves of the master')
16
+ .option('-d, --directory <directory>', 'the code directory', DEFAULT_GAME_SERVER_DIR)
17
+ .action(function (opts) {
18
+ startMasterha(opts);
19
+ });
20
+ }
21
+
22
+ /**
23
+ * Start master slaves.
24
+ *
25
+ * @param {String} option for `startMasterha` operation
26
+ */
27
+ function startMasterha(opts: any) {
28
+ let configFile = path.join(opts.directory, constants.FILEPATH.MASTER_HA);
29
+ if (!fs.existsSync(configFile)) {
30
+ abort(MASTER_HA_NOT_FOUND);
31
+ }
32
+ let masterha = require(configFile).masterha;
33
+ for (let i = 0; i < masterha.length; i++) {
34
+ let server = masterha[i];
35
+ server.mode = constants.RESERVED.STAND_ALONE;
36
+ server.masterha = 'true';
37
+ server.home = opts.directory;
38
+ runServer(server);
39
+ }
40
+ }
@@ -0,0 +1,48 @@
1
+
2
+
3
+
4
+
5
+ import * as program from 'commander';
6
+ import { connectToMaster } from '../utils/utils';
7
+ import { ConsoleModule as co } from '../../lib/modules/console';
8
+ import { DEFAULT_USERNAME, DEFAULT_PWD, DEFAULT_MASTER_HOST, DEFAULT_MASTER_PORT, ADD_SERVER_INFO, RESTART_SERVER_INFO } from '../utils/constants';
9
+ import { Command } from 'commander';
10
+
11
+ export default function (program: Command) {
12
+ program.command('restart')
13
+ .description('restart the servers, for multiple servers, use `melo restart server-id-1 server-id-2`')
14
+ .option('-u, --username <username>', 'administration user name', DEFAULT_USERNAME)
15
+ .option('-p, --password <password>', 'administration password', DEFAULT_PWD)
16
+ .option('-h, --host <master-host>', 'master server host', DEFAULT_MASTER_HOST)
17
+ .option('-P, --port <master-port>', 'master server port', (value)=>parseInt(value), DEFAULT_MASTER_PORT)
18
+ .option('-t, --type <server-type>,', 'start server type')
19
+ .option('-i, --id <server-id>', 'start server id')
20
+ .action(function (opts) {
21
+ restart(opts);
22
+ });
23
+ }
24
+
25
+
26
+ function restart(opts: any) {
27
+ let id = 'melo_restart_' + Date.now();
28
+ let serverIds: string[] = [];
29
+ let type: string = null;
30
+ if (!!opts.id) {
31
+ serverIds.push(opts.id);
32
+ }
33
+ if (!!opts.type) {
34
+ type = opts.type;
35
+ }
36
+ connectToMaster(id, opts, function (client) {
37
+ client.request(co.moduleId, { signal: 'restart', ids: serverIds, type: type }, function (err: Error, fails: string[]) {
38
+ if (!!err) {
39
+ console.error(err);
40
+ } else if (!!fails.length) {
41
+ console.info('restart fails server ids: %j', fails);
42
+ } else {
43
+ console.info(RESTART_SERVER_INFO);
44
+ }
45
+ process.exit(0);
46
+ });
47
+ });
48
+ }
@@ -0,0 +1,65 @@
1
+
2
+
3
+
4
+ import * as fs from 'fs';
5
+ import * as os from 'os';
6
+ import * as path from 'path';
7
+ import * as util from 'util';
8
+ import * as constants from '../../lib/util/constants';
9
+ import { connectToMaster, abort } from '../utils/utils';
10
+ import { ConsoleModule as co } from '../../lib/modules/console';
11
+ import { DEFAULT_USERNAME, DEFAULT_PWD, DEFAULT_MASTER_HOST, DEFAULT_MASTER_PORT, ADD_SERVER_INFO, CLOSEAPP_INFO, KILL_CMD_WIN, KILL_CMD_LUX, DEFAULT_ENV, DEFAULT_GAME_SERVER_DIR, SCRIPT_NOT_FOUND, DAEMON_INFO } from '../utils/constants';
12
+ import { exec, spawn } from 'child_process';
13
+ import { Command } from 'commander';
14
+
15
+
16
+
17
+ export default function (program: Command) {
18
+ program.command('start')
19
+ .description('start the application')
20
+ .option('-e, --env <env>', 'the used environment', DEFAULT_ENV)
21
+ .option('-D, --daemon', 'enable the daemon start')
22
+ .option('-d, --directory, <directory>', 'the code directory', DEFAULT_GAME_SERVER_DIR)
23
+ .option('-t, --type <server-type>,', 'start server type')
24
+ .option('-i, --id <server-id>', 'start server id')
25
+ .action(function (opts) {
26
+ start(opts);
27
+ });
28
+ }
29
+ /**
30
+ * Start application.
31
+ *
32
+ * @param {Object} opts options for `start` operation
33
+ */
34
+ function start(opts: any) {
35
+ let absScript = path.resolve(opts.directory, 'app.js');
36
+ if (!fs.existsSync(absScript)) {
37
+ abort(SCRIPT_NOT_FOUND);
38
+ }
39
+
40
+ let logDir = path.resolve(opts.directory, 'logs');
41
+ if (!fs.existsSync(logDir)) {
42
+ fs.mkdirSync(logDir);
43
+ }
44
+
45
+ let ls;
46
+ let type = opts.type || constants.RESERVED.ALL;
47
+ let params = [absScript, 'env=' + opts.env, 'type=' + type];
48
+ if (!!opts.id) {
49
+ params.push('startId=' + opts.id);
50
+ }
51
+ if (opts.daemon) {
52
+ ls = spawn(process.execPath, params, { detached: true, stdio: 'ignore' });
53
+ ls.unref();
54
+ console.log(DAEMON_INFO);
55
+ process.exit(0);
56
+ } else {
57
+ ls = spawn(process.execPath, params);
58
+ ls.stdout.on('data', function (data) {
59
+ console.log(data.toString());
60
+ });
61
+ ls.stderr.on('data', function (data) {
62
+ console.log(data.toString());
63
+ });
64
+ }
65
+ }