@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,6 @@
1
+ {
2
+ "name": "melo-client",
3
+ "description": "melo-client",
4
+ "local": [ "boot" ],
5
+ "paths": [ "local"]
6
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "boot",
3
+ "description": "Main app boot component",
4
+ "dependencies": {
5
+ "component/emitter":"master",
6
+ "node-melo/melo-protocol": "master",
7
+ "melonode/melo-protobuf": "*",
8
+ "pinusnode/pinus-jsclient-websocket": "master"
9
+ },
10
+ "scripts": ["index.js"]
11
+ }
@@ -0,0 +1,11 @@
1
+ var Emitter = require('emitter');
2
+ window.EventEmitter = Emitter;
3
+
4
+ var protocol = require('@bigtyphoon/melo-protocol');
5
+ window.Protocol = protocol;
6
+
7
+ var protobuf = require('@bigtyphoon/melo-protobuf');
8
+ window.protobuf = protobuf;
9
+
10
+ var melo = require('pinus-jsclient-websocket');
11
+ window.melo = melo;
@@ -0,0 +1,456 @@
1
+ (function() {
2
+ var isArray = Array.isArray;
3
+
4
+ var root = this;
5
+
6
+ function EventEmitter() {
7
+ }
8
+
9
+
10
+ if (typeof module !== 'undefined' && module.exports) {
11
+ module.exports.EventEmitter = EventEmitter;
12
+ }
13
+ else {
14
+ root = window;
15
+ root.EventEmitter = EventEmitter;
16
+ }
17
+
18
+ // By default EventEmitters will print a warning if more than
19
+ // 10 listeners are added to it. This is a useful default which
20
+ // helps finding memory leaks.
21
+ //
22
+ // Obviously not all Emitters should be limited to 10. This function allows
23
+ // that to be increased. Set to zero for unlimited.
24
+ var defaultMaxListeners = 10;
25
+ EventEmitter.prototype.setMaxListeners = function(n) {
26
+ if (!this._events) this._events = {};
27
+ this._maxListeners = n;
28
+ };
29
+
30
+
31
+ EventEmitter.prototype.emit = function() {
32
+ var type = arguments[0];
33
+ // If there is no 'error' event listener then throw.
34
+ if (type === 'error') {
35
+ if (!this._events || !this._events.error ||
36
+ (isArray(this._events.error) && !this._events.error.length))
37
+ {
38
+ if (this.domain) {
39
+ var er = arguments[1];
40
+ er.domain_emitter = this;
41
+ er.domain = this.domain;
42
+ er.domain_thrown = false;
43
+ this.domain.emit('error', er);
44
+ return false;
45
+ }
46
+
47
+ if (arguments[1] instanceof Error) {
48
+ throw arguments[1]; // Unhandled 'error' event
49
+ } else {
50
+ throw new Error("Uncaught, unspecified 'error' event.");
51
+ }
52
+ return false;
53
+ }
54
+ }
55
+
56
+ if (!this._events) return false;
57
+ var handler = this._events[type];
58
+ if (!handler) return false;
59
+
60
+ if (typeof handler == 'function') {
61
+ if (this.domain) {
62
+ this.domain.enter();
63
+ }
64
+ switch (arguments.length) {
65
+ // fast cases
66
+ case 1:
67
+ handler.call(this);
68
+ break;
69
+ case 2:
70
+ handler.call(this, arguments[1]);
71
+ break;
72
+ case 3:
73
+ handler.call(this, arguments[1], arguments[2]);
74
+ break;
75
+ // slower
76
+ default:
77
+ var l = arguments.length;
78
+ var args = new Array(l - 1);
79
+ for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
80
+ handler.apply(this, args);
81
+ }
82
+ if (this.domain) {
83
+ this.domain.exit();
84
+ }
85
+ return true;
86
+
87
+ } else if (isArray(handler)) {
88
+ if (this.domain) {
89
+ this.domain.enter();
90
+ }
91
+ var l = arguments.length;
92
+ var args = new Array(l - 1);
93
+ for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
94
+
95
+ var listeners = handler.slice();
96
+ for (var i = 0, l = listeners.length; i < l; i++) {
97
+ listeners[i].apply(this, args);
98
+ }
99
+ if (this.domain) {
100
+ this.domain.exit();
101
+ }
102
+ return true;
103
+
104
+ } else {
105
+ return false;
106
+ }
107
+ };
108
+
109
+ EventEmitter.prototype.addListener = function(type, listener) {
110
+ if ('function' !== typeof listener) {
111
+ throw new Error('addListener only takes instances of Function');
112
+ }
113
+
114
+ if (!this._events) this._events = {};
115
+
116
+ // To avoid recursion in the case that type == "newListeners"! Before
117
+ // adding it to the listeners, first emit "newListeners".
118
+ this.emit('newListener', type, typeof listener.listener === 'function' ?
119
+ listener.listener : listener);
120
+
121
+ if (!this._events[type]) {
122
+ // Optimize the case of one listener. Don't need the extra array object.
123
+ this._events[type] = listener;
124
+ } else if (isArray(this._events[type])) {
125
+
126
+ // If we've already got an array, just append.
127
+ this._events[type].push(listener);
128
+
129
+ } else {
130
+ // Adding the second element, need to change to array.
131
+ this._events[type] = [this._events[type], listener];
132
+
133
+ }
134
+
135
+ // Check for listener leak
136
+ if (isArray(this._events[type]) && !this._events[type].warned) {
137
+ var m;
138
+ if (this._maxListeners !== undefined) {
139
+ m = this._maxListeners;
140
+ } else {
141
+ m = defaultMaxListeners;
142
+ }
143
+
144
+ if (m && m > 0 && this._events[type].length > m) {
145
+ this._events[type].warned = true;
146
+ console.error('(node) warning: possible EventEmitter memory ' +
147
+ 'leak detected. %d listeners added. ' +
148
+ 'Use emitter.setMaxListeners() to increase limit.',
149
+ this._events[type].length);
150
+ console.trace();
151
+ }
152
+ }
153
+
154
+ return this;
155
+ };
156
+
157
+ EventEmitter.prototype.on = EventEmitter.prototype.addListener;
158
+
159
+ EventEmitter.prototype.once = function(type, listener) {
160
+ if ('function' !== typeof listener) {
161
+ throw new Error('.once only takes instances of Function');
162
+ }
163
+
164
+ var self = this;
165
+ function g() {
166
+ self.removeListener(type, g);
167
+ listener.apply(this, arguments);
168
+ };
169
+
170
+ g.listener = listener;
171
+ self.on(type, g);
172
+
173
+ return this;
174
+ };
175
+
176
+ EventEmitter.prototype.removeListener = function(type, listener) {
177
+ if ('function' !== typeof listener) {
178
+ throw new Error('removeListener only takes instances of Function');
179
+ }
180
+
181
+ // does not use listeners(), so no side effect of creating _events[type]
182
+ if (!this._events || !this._events[type]) return this;
183
+
184
+ var list = this._events[type];
185
+
186
+ if (isArray(list)) {
187
+ var position = -1;
188
+ for (var i = 0, length = list.length; i < length; i++) {
189
+ if (list[i] === listener ||
190
+ (list[i].listener && list[i].listener === listener))
191
+ {
192
+ position = i;
193
+ break;
194
+ }
195
+ }
196
+
197
+ if (position < 0) return this;
198
+ list.splice(position, 1);
199
+ } else if (list === listener ||
200
+ (list.listener && list.listener === listener))
201
+ {
202
+ delete this._events[type];
203
+ }
204
+
205
+ return this;
206
+ };
207
+
208
+ EventEmitter.prototype.removeAllListeners = function(type) {
209
+ if (arguments.length === 0) {
210
+ this._events = {};
211
+ return this;
212
+ }
213
+
214
+ var events = this._events && this._events[type];
215
+ if (!events) return this;
216
+
217
+ if (isArray(events)) {
218
+ events.splice(0);
219
+ } else {
220
+ this._events[type] = null;
221
+ }
222
+
223
+ return this;
224
+ };
225
+
226
+ EventEmitter.prototype.listeners = function(type) {
227
+ if (!this._events) this._events = {};
228
+ if (!this._events[type]) this._events[type] = [];
229
+ if (!isArray(this._events[type])) {
230
+ this._events[type] = [this._events[type]];
231
+ }
232
+ return this._events[type];
233
+ }
234
+ })();
235
+
236
+ (function (exports, global) {
237
+
238
+ var Protocol = exports;
239
+
240
+ var HEADER = 5;
241
+
242
+ var Message = function(id,route,body){
243
+ this.id = id;
244
+ this.route = route;
245
+ this.body = body;
246
+ };
247
+
248
+ /**
249
+ *
250
+ *pomele client encode
251
+ * id message id;
252
+ * route message route
253
+ * msg message body
254
+ * socketio current support string
255
+ *
256
+ */
257
+ Protocol.encode = function(id,route,msg){
258
+ var msgStr = JSON.stringify(msg);
259
+ if (route.length>255) { throw new Error('route maxlength is overflow'); }
260
+ var byteArray = new Uint16Array(HEADER + route.length + msgStr.length);
261
+ var index = 0;
262
+ byteArray[index++] = (id>>24) & 0xFF;
263
+ byteArray[index++] = (id>>16) & 0xFF;
264
+ byteArray[index++] = (id>>8) & 0xFF;
265
+ byteArray[index++] = id & 0xFF;
266
+ byteArray[index++] = route.length & 0xFF;
267
+ for(var i = 0;i<route.length;i++){
268
+ byteArray[index++] = route.charCodeAt(i);
269
+ }
270
+ for (var i = 0; i < msgStr.length; i++) {
271
+ byteArray[index++] = msgStr.charCodeAt(i);
272
+ }
273
+ return bt2Str(byteArray,0,byteArray.length);
274
+ };
275
+
276
+
277
+
278
+
279
+ /**
280
+ *
281
+ *client decode
282
+ *msg String data
283
+ *return Message Object
284
+ */
285
+ Protocol.decode = function(msg){
286
+ var idx, len = msg.length, arr = new Array( len );
287
+ for ( idx = 0 ; idx < len ; ++idx ) {
288
+ arr[idx] = msg.charCodeAt(idx);
289
+ }
290
+ var index = 0;
291
+ var buf = new Uint16Array(arr);
292
+ var id = ((buf[index++] <<24) | (buf[index++]) << 16 | (buf[index++]) << 8 | buf[index++]) >>>0;
293
+ var routeLen = buf[HEADER-1];
294
+ var route = bt2Str(buf,HEADER, routeLen+HEADER);
295
+ var body = bt2Str(buf,routeLen+HEADER,buf.length);
296
+ return new Message(id,route,body);
297
+ };
298
+
299
+ var bt2Str = function(byteArray,start,end) {
300
+ var result = "";
301
+ for(var i = start; i < byteArray.length && i<end; i++) {
302
+ result = result + String.fromCharCode(byteArray[i]);
303
+ };
304
+ return result;
305
+ }
306
+
307
+ })('object' === typeof module ? module.exports : (this.Protocol = {}), this);
308
+
309
+ (function() {
310
+ if (typeof Object.create !== 'function') {
311
+ Object.create = function (o) {
312
+ function F() {}
313
+ F.prototype = o;
314
+ return new F();
315
+ };
316
+ }
317
+
318
+ var root = window;
319
+ var melo = Object.create(EventEmitter.prototype); // object extend from object
320
+ root.melo = melo;
321
+ var socket = null;
322
+ var id = 1;
323
+ var callbacks = {};
324
+
325
+ melo.init = function(params, cb){
326
+ melo.params = params;
327
+ params.debug = true;
328
+ var host = params.host;
329
+ var port = params.port;
330
+
331
+ var url = 'ws://' + host;
332
+ if(port) {
333
+ url += ':' + port;
334
+ }
335
+
336
+ socket = io(url, {'force new connection': true, reconnect: false});
337
+
338
+ socket.on('connect', function(){
339
+ console.log('[meloclient.init] websocket connected!');
340
+ if (cb) {
341
+ cb(socket);
342
+ }
343
+ });
344
+
345
+ socket.on('reconnect', function() {
346
+ console.log('reconnect');
347
+ });
348
+
349
+ socket.on('message', function(data){
350
+ if(typeof data === 'string') {
351
+ data = JSON.parse(data);
352
+ }
353
+ if(data instanceof Array) {
354
+ processMessageBatch(melo, data);
355
+ } else {
356
+ processMessage(melo, data);
357
+ }
358
+ });
359
+
360
+ socket.on('error', function(err) {
361
+ console.log(err);
362
+ });
363
+
364
+ socket.on('disconnect', function(reason) {
365
+ melo.emit('disconnect', reason);
366
+ });
367
+ };
368
+
369
+ melo.disconnect = function() {
370
+ if(socket) {
371
+ socket.disconnect();
372
+ socket = null;
373
+ }
374
+ };
375
+
376
+ melo.request = function(route) {
377
+ if(!route) {
378
+ return;
379
+ }
380
+ var msg = {};
381
+ var cb;
382
+ arguments = Array.prototype.slice.apply(arguments);
383
+ if(arguments.length === 2){
384
+ if(typeof arguments[1] === 'function'){
385
+ cb = arguments[1];
386
+ }else if(typeof arguments[1] === 'object'){
387
+ msg = arguments[1];
388
+ }
389
+ }else if(arguments.length === 3){
390
+ msg = arguments[1];
391
+ cb = arguments[2];
392
+ }
393
+ msg = filter(msg,route);
394
+ id++;
395
+ callbacks[id] = cb;
396
+ var sg = Protocol.encode(id,route,msg);
397
+ socket.send(sg);
398
+ };
399
+
400
+ melo.notify = function(route,msg) {
401
+ this.request(route, msg);
402
+ };
403
+
404
+ var processMessage = function(melo, msg) {
405
+ var route;
406
+ if(msg.id) {
407
+ //if have a id then find the callback function with the request
408
+ var cb = callbacks[msg.id];
409
+
410
+ delete callbacks[msg.id];
411
+ if(typeof cb !== 'function') {
412
+ console.log('[meloclient.processMessage] cb is not a function for request ' + msg.id);
413
+ return;
414
+ }
415
+
416
+ cb(msg.body);
417
+ return;
418
+ }
419
+
420
+ // server push message or old format message
421
+ processCall(msg);
422
+
423
+ //if no id then it should be a server push message
424
+ function processCall(msg) {
425
+ var route = msg.route;
426
+ if(!!route) {
427
+ if (!!msg.body) {
428
+ var body = msg.body.body;
429
+ if (!body) {body = msg.body;}
430
+ melo.emit(route, body);
431
+ } else {
432
+ melo.emit(route,msg);
433
+ }
434
+ } else {
435
+ melo.emit(msg.body.route,msg.body);
436
+ }
437
+ }
438
+ };
439
+
440
+ var processMessageBatch = function(melo, msgs) {
441
+ for(var i=0, l=msgs.length; i<l; i++) {
442
+ processMessage(melo, msgs[i]);
443
+ }
444
+ };
445
+
446
+ function filter(msg,route){
447
+ if(route.indexOf('area.') === 0){
448
+ msg.areaId = melo.areaId;
449
+ }
450
+
451
+ msg.timestamp = Date.now();
452
+ return msg;
453
+ }
454
+
455
+
456
+ })();