build.js.wss 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /**
  2. * hasOwnProperty.
  3. */
  4. var has = Object.prototype.hasOwnProperty;
  5. /**
  6. * Require the given path.
  7. *
  8. * @param {String} path
  9. * @return {Object} exports
  10. * @api public
  11. */
  12. function require(path, parent, orig) {
  13. var resolved = require.resolve(path);
  14. // lookup failed
  15. if (null == resolved) {
  16. orig = orig || path;
  17. parent = parent || 'root';
  18. var err = new Error('Failed to require "' + orig + '" from "' + parent + '"');
  19. err.path = orig;
  20. err.parent = parent;
  21. err.require = true;
  22. throw err;
  23. }
  24. var module = require.modules[resolved];
  25. // perform real require()
  26. // by invoking the module's
  27. // registered function
  28. if (!module.exports) {
  29. module.exports = {};
  30. module.client = module.component = true;
  31. module.call(this, module.exports, require.relative(resolved), module);
  32. }
  33. return module.exports;
  34. }
  35. /**
  36. * Registered modules.
  37. */
  38. require.modules = {};
  39. /**
  40. * Registered aliases.
  41. */
  42. require.aliases = {};
  43. /**
  44. * Resolve `path`.
  45. *
  46. * Lookup:
  47. *
  48. * - PATH/index.js
  49. * - PATH.js
  50. * - PATH
  51. *
  52. * @param {String} path
  53. * @return {String} path or null
  54. * @api private
  55. */
  56. require.resolve = function(path) {
  57. if (path.charAt(0) === '/') path = path.slice(1);
  58. var index = path + '/index.js';
  59. var paths = [
  60. path,
  61. path + '.js',
  62. path + '.json',
  63. path + '/index.js',
  64. path + '/index.json'
  65. ];
  66. for (var i = 0; i < paths.length; i++) {
  67. var path = paths[i];
  68. if (has.call(require.modules, path)) return path;
  69. }
  70. if (has.call(require.aliases, index)) {
  71. return require.aliases[index];
  72. }
  73. };
  74. /**
  75. * Normalize `path` relative to the current path.
  76. *
  77. * @param {String} curr
  78. * @param {String} path
  79. * @return {String}
  80. * @api private
  81. */
  82. require.normalize = function(curr, path) {
  83. var segs = [];
  84. if ('.' != path.charAt(0)) return path;
  85. curr = curr.split('/');
  86. path = path.split('/');
  87. for (var i = 0; i < path.length; ++i) {
  88. if ('..' == path[i]) {
  89. curr.pop();
  90. } else if ('.' != path[i] && '' != path[i]) {
  91. segs.push(path[i]);
  92. }
  93. }
  94. return curr.concat(segs).join('/');
  95. };
  96. /**
  97. * Register module at `path` with callback `definition`.
  98. *
  99. * @param {String} path
  100. * @param {Function} definition
  101. * @api private
  102. */
  103. require.register = function(path, definition) {
  104. require.modules[path] = definition;
  105. };
  106. /**
  107. * Alias a module definition.
  108. *
  109. * @param {String} from
  110. * @param {String} to
  111. * @api private
  112. */
  113. require.alias = function(from, to) {
  114. if (!has.call(require.modules, from)) {
  115. throw new Error('Failed to alias "' + from + '", it does not exist');
  116. }
  117. require.aliases[to] = from;
  118. };
  119. /**
  120. * Return a require function relative to the `parent` path.
  121. *
  122. * @param {String} parent
  123. * @return {Function}
  124. * @api private
  125. */
  126. require.relative = function(parent) {
  127. var p = require.normalize(parent, '..');
  128. /**
  129. * lastIndexOf helper.
  130. */
  131. function lastIndexOf(arr, obj) {
  132. var i = arr.length;
  133. while (i--) {
  134. if (arr[i] === obj) return i;
  135. }
  136. return -1;
  137. }
  138. /**
  139. * The relative require() itself.
  140. */
  141. function localRequire(path) {
  142. var resolved = localRequire.resolve(path);
  143. return require(resolved, parent, path);
  144. }
  145. /**
  146. * Resolve relative to the parent.
  147. */
  148. localRequire.resolve = function(path) {
  149. var c = path.charAt(0);
  150. if ('/' == c) return path.slice(1);
  151. if ('.' == c) return require.normalize(p, path);
  152. // resolve deps by returning
  153. // the dep in the nearest "deps"
  154. // directory
  155. var segs = parent.split('/');
  156. var i = lastIndexOf(segs, 'deps') + 1;
  157. if (!i) i = 0;
  158. path = segs.slice(0, i + 1).join('/') + '/deps/' + path;
  159. return path;
  160. };
  161. /**
  162. * Check if module is defined at `path`.
  163. */
  164. localRequire.exists = function(path) {
  165. return has.call(require.modules, localRequire.resolve(path));
  166. };
  167. return localRequire;
  168. };
  169. require.register("component-indexof/index.js", function(exports, require, module){
  170. var indexOf = [].indexOf;
  171. module.exports = function(arr, obj){
  172. if (indexOf) return arr.indexOf(obj);
  173. for (var i = 0; i < arr.length; ++i) {
  174. if (arr[i] === obj) return i;
  175. }
  176. return -1;
  177. };
  178. });
  179. require.register("component-emitter/index.js", function(exports, require, module){
  180. /**
  181. * Module dependencies.
  182. */
  183. var index = require('indexof');
  184. /**
  185. * Expose `Emitter`.
  186. */
  187. module.exports = Emitter;
  188. /**
  189. * Initialize a new `Emitter`.
  190. *
  191. * @api public
  192. */
  193. function Emitter(obj) {
  194. if (obj) return mixin(obj);
  195. };
  196. /**
  197. * Mixin the emitter properties.
  198. *
  199. * @param {Object} obj
  200. * @return {Object}
  201. * @api private
  202. */
  203. function mixin(obj) {
  204. for (var key in Emitter.prototype) {
  205. obj[key] = Emitter.prototype[key];
  206. }
  207. return obj;
  208. }
  209. /**
  210. * Listen on the given `event` with `fn`.
  211. *
  212. * @param {String} event
  213. * @param {Function} fn
  214. * @return {Emitter}
  215. * @api public
  216. */
  217. Emitter.prototype.on = function(event, fn){
  218. this._callbacks = this._callbacks || {};
  219. (this._callbacks[event] = this._callbacks[event] || [])
  220. .push(fn);
  221. return this;
  222. };
  223. /**
  224. * Adds an `event` listener that will be invoked a single
  225. * time then automatically removed.
  226. *
  227. * @param {String} event
  228. * @param {Function} fn
  229. * @return {Emitter}
  230. * @api public
  231. */
  232. Emitter.prototype.once = function(event, fn){
  233. var self = this;
  234. this._callbacks = this._callbacks || {};
  235. function on() {
  236. self.off(event, on);
  237. fn.apply(this, arguments);
  238. }
  239. fn._off = on;
  240. this.on(event, on);
  241. return this;
  242. };
  243. /**
  244. * Remove the given callback for `event` or all
  245. * registered callbacks.
  246. *
  247. * @param {String} event
  248. * @param {Function} fn
  249. * @return {Emitter}
  250. * @api public
  251. */
  252. Emitter.prototype.off =
  253. Emitter.prototype.removeListener =
  254. Emitter.prototype.removeAllListeners = function(event, fn){
  255. this._callbacks = this._callbacks || {};
  256. // all
  257. if (0 == arguments.length) {
  258. this._callbacks = {};
  259. return this;
  260. }
  261. // specific event
  262. var callbacks = this._callbacks[event];
  263. if (!callbacks) return this;
  264. // remove all handlers
  265. if (1 == arguments.length) {
  266. delete this._callbacks[event];
  267. return this;
  268. }
  269. // remove specific handler
  270. var i = index(callbacks, fn._off || fn);
  271. if (~i) callbacks.splice(i, 1);
  272. return this;
  273. };
  274. /**
  275. * Emit `event` with the given args.
  276. *
  277. * @param {String} event
  278. * @param {Mixed} ...
  279. * @return {Emitter}
  280. */
  281. Emitter.prototype.emit = function(event){
  282. this._callbacks = this._callbacks || {};
  283. var args = [].slice.call(arguments, 1)
  284. , callbacks = this._callbacks[event];
  285. if (callbacks) {
  286. callbacks = callbacks.slice(0);
  287. for (var i = 0, len = callbacks.length; i < len; ++i) {
  288. callbacks[i].apply(this, args);
  289. }
  290. }
  291. return this;
  292. };
  293. /**
  294. * Return array of callbacks for `event`.
  295. *
  296. * @param {String} event
  297. * @return {Array}
  298. * @api public
  299. */
  300. Emitter.prototype.listeners = function(event){
  301. this._callbacks = this._callbacks || {};
  302. return this._callbacks[event] || [];
  303. };
  304. /**
  305. * Check if this emitter has `event` handlers.
  306. *
  307. * @param {String} event
  308. * @return {Boolean}
  309. * @api public
  310. */
  311. Emitter.prototype.hasListeners = function(event){
  312. return !! this.listeners(event).length;
  313. };
  314. });
  315. require.register("NetEase-pomelo-protocol/lib/protocol.js", function(exports, require, module){
  316. (function (exports, ByteArray, global) {
  317. var Protocol = exports;
  318. var PKG_HEAD_BYTES = 4;
  319. var MSG_FLAG_BYTES = 1;
  320. var MSG_ROUTE_CODE_BYTES = 2;
  321. var MSG_ID_MAX_BYTES = 5;
  322. var MSG_ROUTE_LEN_BYTES = 1;
  323. var MSG_ROUTE_CODE_MAX = 0xffff;
  324. var MSG_COMPRESS_ROUTE_MASK = 0x1;
  325. var MSG_TYPE_MASK = 0x7;
  326. var Package = Protocol.Package = {};
  327. var Message = Protocol.Message = {};
  328. Package.TYPE_HANDSHAKE = 1;
  329. Package.TYPE_HANDSHAKE_ACK = 2;
  330. Package.TYPE_HEARTBEAT = 3;
  331. Package.TYPE_DATA = 4;
  332. Package.TYPE_KICK = 5;
  333. Message.TYPE_REQUEST = 0;
  334. Message.TYPE_NOTIFY = 1;
  335. Message.TYPE_RESPONSE = 2;
  336. Message.TYPE_PUSH = 3;
  337. /**
  338. * pomele client encode
  339. * id message id;
  340. * route message route
  341. * msg message body
  342. * socketio current support string
  343. */
  344. Protocol.strencode = function(str) {
  345. var byteArray = new ByteArray(str.length * 3);
  346. var offset = 0;
  347. for(var i = 0; i < str.length; i++){
  348. var charCode = str.charCodeAt(i);
  349. var codes = null;
  350. if(charCode <= 0x7f){
  351. codes = [charCode];
  352. }else if(charCode <= 0x7ff){
  353. codes = [0xc0|(charCode>>6), 0x80|(charCode & 0x3f)];
  354. }else{
  355. codes = [0xe0|(charCode>>12), 0x80|((charCode & 0xfc0)>>6), 0x80|(charCode & 0x3f)];
  356. }
  357. for(var j = 0; j < codes.length; j++){
  358. byteArray[offset] = codes[j];
  359. ++offset;
  360. }
  361. }
  362. var _buffer = new ByteArray(offset);
  363. copyArray(_buffer, 0, byteArray, 0, offset);
  364. return _buffer;
  365. };
  366. /**
  367. * client decode
  368. * msg String data
  369. * return Message Object
  370. */
  371. Protocol.strdecode = function(buffer) {
  372. var bytes = new ByteArray(buffer);
  373. var array = [];
  374. var offset = 0;
  375. var charCode = 0;
  376. var end = bytes.length;
  377. while(offset < end){
  378. if(bytes[offset] < 128){
  379. charCode = bytes[offset];
  380. offset += 1;
  381. }else if(bytes[offset] < 224){
  382. charCode = ((bytes[offset] & 0x3f)<<6) + (bytes[offset+1] & 0x3f);
  383. offset += 2;
  384. }else{
  385. charCode = ((bytes[offset] & 0x0f)<<12) + ((bytes[offset+1] & 0x3f)<<6) + (bytes[offset+2] & 0x3f);
  386. offset += 3;
  387. }
  388. array.push(charCode);
  389. }
  390. return String.fromCharCode.apply(null, array);
  391. };
  392. /**
  393. * Package protocol encode.
  394. *
  395. * Pomelo package format:
  396. * +------+-------------+------------------+
  397. * | type | body length | body |
  398. * +------+-------------+------------------+
  399. *
  400. * Head: 4bytes
  401. * 0: package type,
  402. * 1 - handshake,
  403. * 2 - handshake ack,
  404. * 3 - heartbeat,
  405. * 4 - data
  406. * 5 - kick
  407. * 1 - 3: big-endian body length
  408. * Body: body length bytes
  409. *
  410. * @param {Number} type package type
  411. * @param {ByteArray} body body content in bytes
  412. * @return {ByteArray} new byte array that contains encode result
  413. */
  414. Package.encode = function(type, body){
  415. var length = body ? body.length : 0;
  416. var buffer = new ByteArray(PKG_HEAD_BYTES + length);
  417. var index = 0;
  418. buffer[index++] = type & 0xff;
  419. buffer[index++] = (length >> 16) & 0xff;
  420. buffer[index++] = (length >> 8) & 0xff;
  421. buffer[index++] = length & 0xff;
  422. if(body) {
  423. copyArray(buffer, index, body, 0, length);
  424. }
  425. return buffer;
  426. };
  427. /**
  428. * Package protocol decode.
  429. * See encode for package format.
  430. *
  431. * @param {ByteArray} buffer byte array containing package content
  432. * @return {Object} {type: package type, buffer: body byte array}
  433. */
  434. Package.decode = function(buffer){
  435. var bytes = new ByteArray(buffer);
  436. var type = bytes[0];
  437. var index = 1;
  438. var length = ((bytes[index++]) << 16 | (bytes[index++]) << 8 | bytes[index++]) >>> 0;
  439. var body = length ? new ByteArray(length) : null;
  440. copyArray(body, 0, bytes, PKG_HEAD_BYTES, length);
  441. return {'type': type, 'body': body};
  442. };
  443. /**
  444. * Message protocol encode.
  445. *
  446. * @param {Number} id message id
  447. * @param {Number} type message type
  448. * @param {Number} compressRoute whether compress route
  449. * @param {Number|String} route route code or route string
  450. * @param {Buffer} msg message body bytes
  451. * @return {Buffer} encode result
  452. */
  453. Message.encode = function(id, type, compressRoute, route, msg){
  454. // caculate message max length
  455. var idBytes = msgHasId(type) ? caculateMsgIdBytes(id) : 0;
  456. var msgLen = MSG_FLAG_BYTES + idBytes;
  457. if(msgHasRoute(type)) {
  458. if(compressRoute) {
  459. if(typeof route !== 'number'){
  460. throw new Error('error flag for number route!');
  461. }
  462. msgLen += MSG_ROUTE_CODE_BYTES;
  463. } else {
  464. msgLen += MSG_ROUTE_LEN_BYTES;
  465. if(route) {
  466. route = Protocol.strencode(route);
  467. if(route.length>255) {
  468. throw new Error('route maxlength is overflow');
  469. }
  470. msgLen += route.length;
  471. }
  472. }
  473. }
  474. if(msg) {
  475. msgLen += msg.length;
  476. }
  477. var buffer = new ByteArray(msgLen);
  478. var offset = 0;
  479. // add flag
  480. offset = encodeMsgFlag(type, compressRoute, buffer, offset);
  481. // add message id
  482. if(msgHasId(type)) {
  483. offset = encodeMsgId(id, idBytes, buffer, offset);
  484. }
  485. // add route
  486. if(msgHasRoute(type)) {
  487. offset = encodeMsgRoute(compressRoute, route, buffer, offset);
  488. }
  489. // add body
  490. if(msg) {
  491. offset = encodeMsgBody(msg, buffer, offset);
  492. }
  493. return buffer;
  494. };
  495. /**
  496. * Message protocol decode.
  497. *
  498. * @param {Buffer|Uint8Array} buffer message bytes
  499. * @return {Object} message object
  500. */
  501. Message.decode = function(buffer) {
  502. var bytes = new ByteArray(buffer);
  503. var bytesLen = bytes.length || bytes.byteLength;
  504. var offset = 0;
  505. var id = 0;
  506. var route = null;
  507. // parse flag
  508. var flag = bytes[offset++];
  509. var compressRoute = flag & MSG_COMPRESS_ROUTE_MASK;
  510. var type = (flag >> 1) & MSG_TYPE_MASK;
  511. // parse id
  512. if(msgHasId(type)) {
  513. var byte = bytes[offset++];
  514. id = byte & 0x7f;
  515. while(byte & 0x80) {
  516. id <<= 7;
  517. byte = bytes[offset++];
  518. id |= byte & 0x7f;
  519. }
  520. }
  521. // parse route
  522. if(msgHasRoute(type)) {
  523. if(compressRoute) {
  524. route = (bytes[offset++]) << 8 | bytes[offset++];
  525. } else {
  526. var routeLen = bytes[offset++];
  527. if(routeLen) {
  528. route = new ByteArray(routeLen);
  529. copyArray(route, 0, bytes, offset, routeLen);
  530. route = Protocol.strdecode(route);
  531. } else {
  532. route = '';
  533. }
  534. offset += routeLen;
  535. }
  536. }
  537. // parse body
  538. var bodyLen = bytesLen - offset;
  539. var body = new ByteArray(bodyLen);
  540. copyArray(body, 0, bytes, offset, bodyLen);
  541. return {'id': id, 'type': type, 'compressRoute': compressRoute,
  542. 'route': route, 'body': body};
  543. };
  544. var copyArray = function(dest, doffset, src, soffset, length) {
  545. if('function' === typeof src.copy) {
  546. // Buffer
  547. src.copy(dest, doffset, soffset, soffset + length);
  548. } else {
  549. // Uint8Array
  550. for(var index=0; index<length; index++){
  551. dest[doffset++] = src[soffset++];
  552. }
  553. }
  554. };
  555. var msgHasId = function(type) {
  556. return type === Message.TYPE_REQUEST || type === Message.TYPE_RESPONSE;
  557. };
  558. var msgHasRoute = function(type) {
  559. return type === Message.TYPE_REQUEST || type === Message.TYPE_NOTIFY ||
  560. type === Message.TYPE_PUSH;
  561. };
  562. var caculateMsgIdBytes = function(id) {
  563. var len = 0;
  564. do {
  565. len += 1;
  566. id >>= 7;
  567. } while(id > 0);
  568. return len;
  569. };
  570. var encodeMsgFlag = function(type, compressRoute, buffer, offset) {
  571. if(type !== Message.TYPE_REQUEST && type !== Message.TYPE_NOTIFY &&
  572. type !== Message.TYPE_RESPONSE && type !== Message.TYPE_PUSH) {
  573. throw new Error('unkonw message type: ' + type);
  574. }
  575. buffer[offset] = (type << 1) | (compressRoute ? 1 : 0);
  576. return offset + MSG_FLAG_BYTES;
  577. };
  578. var encodeMsgId = function(id, idBytes, buffer, offset) {
  579. var index = offset + idBytes - 1;
  580. buffer[index--] = id & 0x7f;
  581. while(index >= offset) {
  582. id >>= 7;
  583. buffer[index--] = id & 0x7f | 0x80;
  584. }
  585. return offset + idBytes;
  586. };
  587. var encodeMsgRoute = function(compressRoute, route, buffer, offset) {
  588. if (compressRoute) {
  589. if(route > MSG_ROUTE_CODE_MAX){
  590. throw new Error('route number is overflow');
  591. }
  592. buffer[offset++] = (route >> 8) & 0xff;
  593. buffer[offset++] = route & 0xff;
  594. } else {
  595. if(route) {
  596. buffer[offset++] = route.length & 0xff;
  597. copyArray(buffer, offset, route, 0, route.length);
  598. offset += route.length;
  599. } else {
  600. buffer[offset++] = 0;
  601. }
  602. }
  603. return offset;
  604. };
  605. var encodeMsgBody = function(msg, buffer, offset) {
  606. copyArray(buffer, offset, msg, 0, msg.length);
  607. return offset + msg.length;
  608. };
  609. module.exports = Protocol;
  610. })('object' === typeof module ? module.exports : (this.Protocol = {}),'object' === typeof module ? Buffer : Uint8Array, this);
  611. });
  612. require.register("pomelonode-pomelo-protobuf/lib/client/protobuf.js", function(exports, require, module){
  613. /* ProtocolBuffer client 0.1.0*/
  614. /**
  615. * pomelo-protobuf
  616. * @author <zhang0935@gmail.com>
  617. */
  618. /**
  619. * Protocol buffer root
  620. * In browser, it will be window.protbuf
  621. */
  622. (function (exports, global){
  623. var Protobuf = exports;
  624. Protobuf.init = function(opts){
  625. //On the serverside, use serverProtos to encode messages send to client
  626. Protobuf.encoder.init(opts.encoderProtos);
  627. //On the serverside, user clientProtos to decode messages receive from clients
  628. Protobuf.decoder.init(opts.decoderProtos);
  629. };
  630. Protobuf.encode = function(key, msg){
  631. return Protobuf.encoder.encode(key, msg);
  632. };
  633. Protobuf.decode = function(key, msg){
  634. return Protobuf.decoder.decode(key, msg);
  635. };
  636. // exports to support for components
  637. module.exports = Protobuf;
  638. })('object' === typeof module ? module.exports : (this.protobuf = {}), this);
  639. /**
  640. * constants
  641. */
  642. (function (exports, global){
  643. var constants = exports.constants = {};
  644. constants.TYPES = {
  645. uInt32 : 0,
  646. sInt32 : 0,
  647. int32 : 0,
  648. double : 1,
  649. string : 2,
  650. message : 2,
  651. float : 5
  652. };
  653. })('undefined' !== typeof protobuf ? protobuf : module.exports, this);
  654. /**
  655. * util module
  656. */
  657. (function (exports, global){
  658. var Util = exports.util = {};
  659. Util.isSimpleType = function(type){
  660. return ( type === 'uInt32' ||
  661. type === 'sInt32' ||
  662. type === 'int32' ||
  663. type === 'uInt64' ||
  664. type === 'sInt64' ||
  665. type === 'float' ||
  666. type === 'double' );
  667. };
  668. })('undefined' !== typeof protobuf ? protobuf : module.exports, this);
  669. /**
  670. * codec module
  671. */
  672. (function (exports, global){
  673. var Codec = exports.codec = {};
  674. var buffer = new ArrayBuffer(8);
  675. var float32Array = new Float32Array(buffer);
  676. var float64Array = new Float64Array(buffer);
  677. var uInt8Array = new Uint8Array(buffer);
  678. Codec.encodeUInt32 = function(n){
  679. var n = parseInt(n);
  680. if(isNaN(n) || n < 0){
  681. return null;
  682. }
  683. var result = [];
  684. do{
  685. var tmp = n % 128;
  686. var next = Math.floor(n/128);
  687. if(next !== 0){
  688. tmp = tmp + 128;
  689. }
  690. result.push(tmp);
  691. n = next;
  692. }while(n !== 0);
  693. return result;
  694. };
  695. Codec.encodeSInt32 = function(n){
  696. var n = parseInt(n);
  697. if(isNaN(n)){
  698. return null;
  699. }
  700. n = n<0?(Math.abs(n)*2-1):n*2;
  701. return Codec.encodeUInt32(n);
  702. };
  703. Codec.decodeUInt32 = function(bytes){
  704. var n = 0;
  705. for(var i = 0; i < bytes.length; i++){
  706. var m = parseInt(bytes[i]);
  707. n = n + ((m & 0x7f) * Math.pow(2,(7*i)));
  708. if(m < 128){
  709. return n;
  710. }
  711. }
  712. return n;
  713. };
  714. Codec.decodeSInt32 = function(bytes){
  715. var n = this.decodeUInt32(bytes);
  716. var flag = ((n%2) === 1)?-1:1;
  717. n = ((n%2 + n)/2)*flag;
  718. return n;
  719. };
  720. Codec.encodeFloat = function(float){
  721. float32Array[0] = float;
  722. return uInt8Array;
  723. };
  724. Codec.decodeFloat = function(bytes, offset){
  725. if(!bytes || bytes.length < (offset +4)){
  726. return null;
  727. }
  728. for(var i = 0; i < 4; i++){
  729. uInt8Array[i] = bytes[offset + i];
  730. }
  731. return float32Array[0];
  732. };
  733. Codec.encodeDouble = function(double){
  734. float64Array[0] = double;
  735. return uInt8Array.subarray(0, 8);
  736. };
  737. Codec.decodeDouble = function(bytes, offset){
  738. if(!bytes || bytes.length < (8 + offset)){
  739. return null;
  740. }
  741. for(var i = 0; i < 8; i++){
  742. uInt8Array[i] = bytes[offset + i];
  743. }
  744. return float64Array[0];
  745. };
  746. Codec.encodeStr = function(bytes, offset, str){
  747. for(var i = 0; i < str.length; i++){
  748. var code = str.charCodeAt(i);
  749. var codes = encode2UTF8(code);
  750. for(var j = 0; j < codes.length; j++){
  751. bytes[offset] = codes[j];
  752. offset++;
  753. }
  754. }
  755. return offset;
  756. };
  757. /**
  758. * Decode string from utf8 bytes
  759. */
  760. Codec.decodeStr = function(bytes, offset, length){
  761. var array = [];
  762. var end = offset + length;
  763. while(offset < end){
  764. var code = 0;
  765. if(bytes[offset] < 128){
  766. code = bytes[offset];
  767. offset += 1;
  768. }else if(bytes[offset] < 224){
  769. code = ((bytes[offset] & 0x3f)<<6) + (bytes[offset+1] & 0x3f);
  770. offset += 2;
  771. }else{
  772. code = ((bytes[offset] & 0x0f)<<12) + ((bytes[offset+1] & 0x3f)<<6) + (bytes[offset+2] & 0x3f);
  773. offset += 3;
  774. }
  775. array.push(code);
  776. }
  777. var str = '';
  778. for(var i = 0; i < array.length;){
  779. str += String.fromCharCode.apply(null, array.slice(i, i + 10000));
  780. i += 10000;
  781. }
  782. return str;
  783. };
  784. /**
  785. * Return the byte length of the str use utf8
  786. */
  787. Codec.byteLength = function(str){
  788. if(typeof(str) !== 'string'){
  789. return -1;
  790. }
  791. var length = 0;
  792. for(var i = 0; i < str.length; i++){
  793. var code = str.charCodeAt(i);
  794. length += codeLength(code);
  795. }
  796. return length;
  797. };
  798. /**
  799. * Encode a unicode16 char code to utf8 bytes
  800. */
  801. function encode2UTF8(charCode){
  802. if(charCode <= 0x7f){
  803. return [charCode];
  804. }else if(charCode <= 0x7ff){
  805. return [0xc0|(charCode>>6), 0x80|(charCode & 0x3f)];
  806. }else{
  807. return [0xe0|(charCode>>12), 0x80|((charCode & 0xfc0)>>6), 0x80|(charCode & 0x3f)];
  808. }
  809. }
  810. function codeLength(code){
  811. if(code <= 0x7f){
  812. return 1;
  813. }else if(code <= 0x7ff){
  814. return 2;
  815. }else{
  816. return 3;
  817. }
  818. }
  819. })('undefined' !== typeof protobuf ? protobuf : module.exports, this);
  820. /**
  821. * encoder module
  822. */
  823. (function (exports, global){
  824. var protobuf = exports;
  825. var MsgEncoder = exports.encoder = {};
  826. var codec = protobuf.codec;
  827. var constant = protobuf.constants;
  828. var util = protobuf.util;
  829. MsgEncoder.init = function(protos){
  830. this.protos = protos || {};
  831. };
  832. MsgEncoder.encode = function(route, msg){
  833. //Get protos from protos map use the route as key
  834. var protos = this.protos[route];
  835. //Check msg
  836. if(!checkMsg(msg, protos)){
  837. return null;
  838. }
  839. //Set the length of the buffer 2 times bigger to prevent overflow
  840. var length = codec.byteLength(JSON.stringify(msg));
  841. //Init buffer and offset
  842. var buffer = new ArrayBuffer(length);
  843. var uInt8Array = new Uint8Array(buffer);
  844. var offset = 0;
  845. if(!!protos){
  846. offset = encodeMsg(uInt8Array, offset, protos, msg);
  847. if(offset > 0){
  848. return uInt8Array.subarray(0, offset);
  849. }
  850. }
  851. return null;
  852. };
  853. /**
  854. * Check if the msg follow the defination in the protos
  855. */
  856. function checkMsg(msg, protos){
  857. if(!protos){
  858. return false;
  859. }
  860. for(var name in protos){
  861. var proto = protos[name];
  862. //All required element must exist
  863. switch(proto.option){
  864. case 'required' :
  865. if(typeof(msg[name]) === 'undefined'){
  866. return false;
  867. }
  868. case 'optional' :
  869. if(typeof(msg[name]) !== 'undefined'){
  870. if(!!protos.__messages[proto.type]){
  871. checkMsg(msg[name], protos.__messages[proto.type]);
  872. }
  873. }
  874. break;
  875. case 'repeated' :
  876. //Check nest message in repeated elements
  877. if(!!msg[name] && !!protos.__messages[proto.type]){
  878. for(var i = 0; i < msg[name].length; i++){
  879. if(!checkMsg(msg[name][i], protos.__messages[proto.type])){
  880. return false;
  881. }
  882. }
  883. }
  884. break;
  885. }
  886. }
  887. return true;
  888. }
  889. function encodeMsg(buffer, offset, protos, msg){
  890. for(var name in msg){
  891. if(!!protos[name]){
  892. var proto = protos[name];
  893. switch(proto.option){
  894. case 'required' :
  895. case 'optional' :
  896. offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
  897. offset = encodeProp(msg[name], proto.type, offset, buffer, protos);
  898. break;
  899. case 'repeated' :
  900. if(msg[name].length > 0){
  901. offset = encodeArray(msg[name], proto, offset, buffer, protos);
  902. }
  903. break;
  904. }
  905. }
  906. }
  907. return offset;
  908. }
  909. function encodeProp(value, type, offset, buffer, protos){
  910. switch(type){
  911. case 'uInt32':
  912. offset = writeBytes(buffer, offset, codec.encodeUInt32(value));
  913. break;
  914. case 'int32' :
  915. case 'sInt32':
  916. offset = writeBytes(buffer, offset, codec.encodeSInt32(value));
  917. break;
  918. case 'float':
  919. writeBytes(buffer, offset, codec.encodeFloat(value));
  920. offset += 4;
  921. break;
  922. case 'double':
  923. writeBytes(buffer, offset, codec.encodeDouble(value));
  924. offset += 8;
  925. break;
  926. case 'string':
  927. var length = codec.byteLength(value);
  928. //Encode length
  929. offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
  930. //write string
  931. codec.encodeStr(buffer, offset, value);
  932. offset += length;
  933. break;
  934. default :
  935. if(!!protos.__messages[type]){
  936. //Use a tmp buffer to build an internal msg
  937. var tmpBuffer = new ArrayBuffer(codec.byteLength(JSON.stringify(value)));
  938. var length = 0;
  939. length = encodeMsg(tmpBuffer, length, protos.__messages[type], value);
  940. //Encode length
  941. offset = writeBytes(buffer, offset, codec.encodeUInt32(length));
  942. //contact the object
  943. for(var i = 0; i < length; i++){
  944. buffer[offset] = tmpBuffer[i];
  945. offset++;
  946. }
  947. }
  948. break;
  949. }
  950. return offset;
  951. }
  952. /**
  953. * Encode reapeated properties, simple msg and object are decode differented
  954. */
  955. function encodeArray(array, proto, offset, buffer, protos){
  956. var i = 0;
  957. if(util.isSimpleType(proto.type)){
  958. offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
  959. offset = writeBytes(buffer, offset, codec.encodeUInt32(array.length));
  960. for(i = 0; i < array.length; i++){
  961. offset = encodeProp(array[i], proto.type, offset, buffer);
  962. }
  963. }else{
  964. for(i = 0; i < array.length; i++){
  965. offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
  966. offset = encodeProp(array[i], proto.type, offset, buffer, protos);
  967. }
  968. }
  969. return offset;
  970. }
  971. function writeBytes(buffer, offset, bytes){
  972. for(var i = 0; i < bytes.length; i++, offset++){
  973. buffer[offset] = bytes[i];
  974. }
  975. return offset;
  976. }
  977. function encodeTag(type, tag){
  978. var value = constant.TYPES[type]||2;
  979. return codec.encodeUInt32((tag<<3)|value);
  980. }
  981. })('undefined' !== typeof protobuf ? protobuf : module.exports, this);
  982. /**
  983. * decoder module
  984. */
  985. (function (exports, global){
  986. var protobuf = exports;
  987. var MsgDecoder = exports.decoder = {};
  988. var codec = protobuf.codec;
  989. var util = protobuf.util;
  990. var buffer;
  991. var offset = 0;
  992. MsgDecoder.init = function(protos){
  993. this.protos = protos || {};
  994. };
  995. MsgDecoder.setProtos = function(protos){
  996. if(!!protos){
  997. this.protos = protos;
  998. }
  999. };
  1000. MsgDecoder.decode = function(route, buf){
  1001. var protos = this.protos[route];
  1002. buffer = buf;
  1003. offset = 0;
  1004. if(!!protos){
  1005. return decodeMsg({}, protos, buffer.length);
  1006. }
  1007. return null;
  1008. };
  1009. function decodeMsg(msg, protos, length){
  1010. while(offset<length){
  1011. var head = getHead();
  1012. var type = head.type;
  1013. var tag = head.tag;
  1014. var name = protos.__tags[tag];
  1015. switch(protos[name].option){
  1016. case 'optional' :
  1017. case 'required' :
  1018. msg[name] = decodeProp(protos[name].type, protos);
  1019. break;
  1020. case 'repeated' :
  1021. if(!msg[name]){
  1022. msg[name] = [];
  1023. }
  1024. decodeArray(msg[name], protos[name].type, protos);
  1025. break;
  1026. }
  1027. }
  1028. return msg;
  1029. }
  1030. /**
  1031. * Test if the given msg is finished
  1032. */
  1033. function isFinish(msg, protos){
  1034. return (!protos.__tags[peekHead().tag]);
  1035. }
  1036. /**
  1037. * Get property head from protobuf
  1038. */
  1039. function getHead(){
  1040. var tag = codec.decodeUInt32(getBytes());
  1041. return {
  1042. type : tag&0x7,
  1043. tag : tag>>3
  1044. };
  1045. }
  1046. /**
  1047. * Get tag head without move the offset
  1048. */
  1049. function peekHead(){
  1050. var tag = codec.decodeUInt32(peekBytes());
  1051. return {
  1052. type : tag&0x7,
  1053. tag : tag>>3
  1054. };
  1055. }
  1056. function decodeProp(type, protos){
  1057. switch(type){
  1058. case 'uInt32':
  1059. return codec.decodeUInt32(getBytes());
  1060. case 'int32' :
  1061. case 'sInt32' :
  1062. return codec.decodeSInt32(getBytes());
  1063. case 'float' :
  1064. var float = codec.decodeFloat(buffer, offset);
  1065. offset += 4;
  1066. return float;
  1067. case 'double' :
  1068. var double = codec.decodeDouble(buffer, offset);
  1069. offset += 8;
  1070. return double;
  1071. case 'string' :
  1072. var length = codec.decodeUInt32(getBytes());
  1073. var str = codec.decodeStr(buffer, offset, length);
  1074. offset += length;
  1075. return str;
  1076. default :
  1077. if(!!protos && !!protos.__messages[type]){
  1078. var length = codec.decodeUInt32(getBytes());
  1079. var msg = {};
  1080. decodeMsg(msg, protos.__messages[type], offset+length);
  1081. return msg;
  1082. }
  1083. break;
  1084. }
  1085. }
  1086. function decodeArray(array, type, protos){
  1087. if(util.isSimpleType(type)){
  1088. var length = codec.decodeUInt32(getBytes());
  1089. for(var i = 0; i < length; i++){
  1090. array.push(decodeProp(type));
  1091. }
  1092. }else{
  1093. array.push(decodeProp(type, protos));
  1094. }
  1095. }
  1096. function getBytes(flag){
  1097. var bytes = [];
  1098. var pos = offset;
  1099. flag = flag || false;
  1100. var b;
  1101. do{
  1102. b = buffer[pos];
  1103. bytes.push(b);
  1104. pos++;
  1105. }while(b >= 128);
  1106. if(!flag){
  1107. offset = pos;
  1108. }
  1109. return bytes;
  1110. }
  1111. function peekBytes(){
  1112. return getBytes(true);
  1113. }
  1114. })('undefined' !== typeof protobuf ? protobuf : module.exports, this);
  1115. });
  1116. require.register("pomelonode-pomelo-jsclient-websocket/lib/pomelo-client.js", function(exports, require, module){
  1117. (function() {
  1118. var JS_WS_CLIENT_TYPE = 'js-websocket';
  1119. var JS_WS_CLIENT_VERSION = '0.0.1';
  1120. var Protocol = window.Protocol;
  1121. var Package = Protocol.Package;
  1122. var Message = Protocol.Message;
  1123. var EventEmitter = window.EventEmitter;
  1124. var RES_OK = 200;
  1125. var RES_FAIL = 500;
  1126. var RES_OLD_CLIENT = 501;
  1127. if (typeof Object.create !== 'function') {
  1128. Object.create = function (o) {
  1129. function F() {}
  1130. F.prototype = o;
  1131. return new F();
  1132. };
  1133. }
  1134. var root = window;
  1135. var pomelo = Object.create(EventEmitter.prototype); // object extend from object
  1136. root.pomelo = pomelo;
  1137. var socket = null;
  1138. var reqId = 0;
  1139. var callbacks = {};
  1140. var handlers = {};
  1141. //Map from request id to route
  1142. var routeMap = {};
  1143. var heartbeatInterval = 0;
  1144. var heartbeatTimeout = 0;
  1145. var nextHeartbeatTimeout = 0;
  1146. var gapThreshold = 100; // heartbeat gap threashold
  1147. var heartbeatId = null;
  1148. var heartbeatTimeoutId = null;
  1149. var handshakeCallback = null;
  1150. var handshakeBuffer = {
  1151. 'sys': {
  1152. type: JS_WS_CLIENT_TYPE,
  1153. version: JS_WS_CLIENT_VERSION
  1154. },
  1155. 'user': {
  1156. }
  1157. };
  1158. var initCallback = null;
  1159. pomelo.init = function(params, cb){
  1160. initCallback = cb;
  1161. var host = params.host;
  1162. var port = params.port;
  1163. var url = 'wss://' + host;
  1164. if(port) {
  1165. url += ':' + port;
  1166. }
  1167. handshakeBuffer.user = params.user;
  1168. handshakeCallback = params.handshakeCallback;
  1169. initWebSocket(url, cb);
  1170. };
  1171. var initWebSocket = function(url,cb) {
  1172. console.log('connect to ' + url);
  1173. var onopen = function(event){
  1174. var obj = Package.encode(Package.TYPE_HANDSHAKE, Protocol.strencode(JSON.stringify(handshakeBuffer)));
  1175. send(obj);
  1176. };
  1177. var onmessage = function(event) {
  1178. processPackage(Package.decode(event.data), cb);
  1179. // new package arrived, update the heartbeat timeout
  1180. if(heartbeatTimeout) {
  1181. nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
  1182. }
  1183. };
  1184. var onerror = function(event) {
  1185. pomelo.emit('io-error', event);
  1186. console.error('socket error: ', event);
  1187. };
  1188. var onclose = function(event){
  1189. pomelo.emit('close',event);
  1190. console.error('socket close: ', event);
  1191. };
  1192. socket = new WebSocket(url);
  1193. socket.binaryType = 'arraybuffer';
  1194. socket.onopen = onopen;
  1195. socket.onmessage = onmessage;
  1196. socket.onerror = onerror;
  1197. socket.onclose = onclose;
  1198. };
  1199. pomelo.disconnect = function() {
  1200. if(socket) {
  1201. if(socket.disconnect) socket.disconnect();
  1202. if(socket.close) socket.close();
  1203. console.log('disconnect');
  1204. socket = null;
  1205. }
  1206. if(heartbeatId) {
  1207. clearTimeout(heartbeatId);
  1208. heartbeatId = null;
  1209. }
  1210. if(heartbeatTimeoutId) {
  1211. clearTimeout(heartbeatTimeoutId);
  1212. heartbeatTimeoutId = null;
  1213. }
  1214. };
  1215. pomelo.request = function(route, msg, cb) {
  1216. if(arguments.length === 2 && typeof msg === 'function') {
  1217. cb = msg;
  1218. msg = {};
  1219. } else {
  1220. msg = msg || {};
  1221. }
  1222. route = route || msg.route;
  1223. if(!route) {
  1224. return;
  1225. }
  1226. reqId++;
  1227. sendMessage(reqId, route, msg);
  1228. callbacks[reqId] = cb;
  1229. routeMap[reqId] = route;
  1230. };
  1231. pomelo.notify = function(route, msg) {
  1232. msg = msg || {};
  1233. sendMessage(0, route, msg);
  1234. };
  1235. var sendMessage = function(reqId, route, msg) {
  1236. var type = reqId ? Message.TYPE_REQUEST : Message.TYPE_NOTIFY;
  1237. //compress message by protobuf
  1238. var protos = !!pomelo.data.protos?pomelo.data.protos.client:{};
  1239. if(!!protos[route]){
  1240. msg = protobuf.encode(route, msg);
  1241. }else{
  1242. msg = Protocol.strencode(JSON.stringify(msg));
  1243. }
  1244. var compressRoute = 0;
  1245. if(pomelo.dict && pomelo.dict[route]){
  1246. route = pomelo.dict[route];
  1247. compressRoute = 1;
  1248. }
  1249. msg = Message.encode(reqId, type, compressRoute, route, msg);
  1250. var packet = Package.encode(Package.TYPE_DATA, msg);
  1251. send(packet);
  1252. };
  1253. var send = function(packet){
  1254. socket.send(packet.buffer);
  1255. };
  1256. var handler = {};
  1257. var heartbeat = function(data) {
  1258. if(!heartbeatInterval) {
  1259. // no heartbeat
  1260. return;
  1261. }
  1262. var obj = Package.encode(Package.TYPE_HEARTBEAT);
  1263. if(heartbeatTimeoutId) {
  1264. clearTimeout(heartbeatTimeoutId);
  1265. heartbeatTimeoutId = null;
  1266. }
  1267. if(heartbeatId) {
  1268. // already in a heartbeat interval
  1269. return;
  1270. }
  1271. heartbeatId = setTimeout(function() {
  1272. heartbeatId = null;
  1273. send(obj);
  1274. nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
  1275. heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, heartbeatTimeout);
  1276. }, heartbeatInterval);
  1277. };
  1278. var heartbeatTimeoutCb = function() {
  1279. var gap = nextHeartbeatTimeout - Date.now();
  1280. if(gap > gapThreshold) {
  1281. heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, gap);
  1282. } else {
  1283. console.error('server heartbeat timeout');
  1284. pomelo.emit('heartbeat timeout');
  1285. pomelo.disconnect();
  1286. }
  1287. };
  1288. var handshake = function(data){
  1289. data = JSON.parse(Protocol.strdecode(data));
  1290. if(data.code === RES_OLD_CLIENT) {
  1291. pomelo.emit('error', 'client version not fullfill');
  1292. return;
  1293. }
  1294. if(data.code !== RES_OK) {
  1295. pomelo.emit('error', 'handshake fail');
  1296. return;
  1297. }
  1298. handshakeInit(data);
  1299. var obj = Package.encode(Package.TYPE_HANDSHAKE_ACK);
  1300. send(obj);
  1301. if(initCallback) {
  1302. initCallback(socket);
  1303. initCallback = null;
  1304. }
  1305. };
  1306. var onData = function(data){
  1307. //probuff decode
  1308. var msg = Message.decode(data);
  1309. if(msg.id > 0){
  1310. msg.route = routeMap[msg.id];
  1311. delete routeMap[msg.id];
  1312. if(!msg.route){
  1313. return;
  1314. }
  1315. }
  1316. msg.body = deCompose(msg);
  1317. processMessage(pomelo, msg);
  1318. };
  1319. var onKick = function(data) {
  1320. pomelo.emit('onKick');
  1321. };
  1322. handlers[Package.TYPE_HANDSHAKE] = handshake;
  1323. handlers[Package.TYPE_HEARTBEAT] = heartbeat;
  1324. handlers[Package.TYPE_DATA] = onData;
  1325. handlers[Package.TYPE_KICK] = onKick;
  1326. var processPackage = function(msg) {
  1327. handlers[msg.type](msg.body);
  1328. };
  1329. var processMessage = function(pomelo, msg) {
  1330. if(!msg.id) {
  1331. // server push message
  1332. pomelo.emit(msg.route, msg.body);
  1333. return;
  1334. }
  1335. //if have a id then find the callback function with the request
  1336. var cb = callbacks[msg.id];
  1337. delete callbacks[msg.id];
  1338. if(typeof cb !== 'function') {
  1339. return;
  1340. }
  1341. cb(msg.body);
  1342. return;
  1343. };
  1344. var processMessageBatch = function(pomelo, msgs) {
  1345. for(var i=0, l=msgs.length; i<l; i++) {
  1346. processMessage(pomelo, msgs[i]);
  1347. }
  1348. };
  1349. var deCompose = function(msg){
  1350. var protos = !!pomelo.data.protos?pomelo.data.protos.server:{};
  1351. var abbrs = pomelo.data.abbrs;
  1352. var route = msg.route;
  1353. //Decompose route from dict
  1354. if(msg.compressRoute) {
  1355. if(!abbrs[route]){
  1356. return {};
  1357. }
  1358. route = msg.route = abbrs[route];
  1359. }
  1360. if(!!protos[route]){
  1361. return protobuf.decode(route, msg.body);
  1362. }else{
  1363. return JSON.parse(Protocol.strdecode(msg.body));
  1364. }
  1365. return msg;
  1366. };
  1367. var handshakeInit = function(data){
  1368. if(data.sys && data.sys.heartbeat) {
  1369. heartbeatInterval = data.sys.heartbeat * 1000; // heartbeat interval
  1370. heartbeatTimeout = heartbeatInterval * 2; // max heartbeat timeout
  1371. } else {
  1372. heartbeatInterval = 0;
  1373. heartbeatTimeout = 0;
  1374. }
  1375. initData(data);
  1376. if(typeof handshakeCallback === 'function') {
  1377. handshakeCallback(data.user);
  1378. }
  1379. };
  1380. //Initilize data used in pomelo client
  1381. var initData = function(data){
  1382. if(!data || !data.sys) {
  1383. return;
  1384. }
  1385. pomelo.data = pomelo.data || {};
  1386. var dict = data.sys.dict;
  1387. var protos = data.sys.protos;
  1388. //Init compress dict
  1389. if(dict){
  1390. pomelo.data.dict = dict;
  1391. pomelo.data.abbrs = {};
  1392. for(var route in dict){
  1393. pomelo.data.abbrs[dict[route]] = route;
  1394. }
  1395. }
  1396. //Init protobuf protos
  1397. if(protos){
  1398. pomelo.data.protos = {
  1399. server : protos.server || {},
  1400. client : protos.client || {}
  1401. };
  1402. if(!!protobuf){
  1403. protobuf.init({encoderProtos: protos.client, decoderProtos: protos.server});
  1404. }
  1405. }
  1406. };
  1407. module.exports = pomelo;
  1408. })();
  1409. });
  1410. require.register("boot/index.js", function(exports, require, module){
  1411. var Emitter = require('emitter');
  1412. window.EventEmitter = Emitter;
  1413. var protocol = require('pomelo-protocol');
  1414. window.Protocol = protocol;
  1415. var protobuf = require('pomelo-protobuf');
  1416. window.protobuf = protobuf;
  1417. var pomelo = require('pomelo-jsclient-websocket');
  1418. window.pomelo = pomelo;
  1419. });
  1420. require.alias("boot/index.js", "pomelo-client/deps/boot/index.js");
  1421. require.alias("component-emitter/index.js", "boot/deps/emitter/index.js");
  1422. require.alias("component-indexof/index.js", "component-emitter/deps/indexof/index.js");
  1423. require.alias("NetEase-pomelo-protocol/lib/protocol.js", "boot/deps/pomelo-protocol/lib/protocol.js");
  1424. require.alias("NetEase-pomelo-protocol/lib/protocol.js", "boot/deps/pomelo-protocol/index.js");
  1425. require.alias("NetEase-pomelo-protocol/lib/protocol.js", "NetEase-pomelo-protocol/index.js");
  1426. require.alias("pomelonode-pomelo-protobuf/lib/client/protobuf.js", "boot/deps/pomelo-protobuf/lib/client/protobuf.js");
  1427. require.alias("pomelonode-pomelo-protobuf/lib/client/protobuf.js", "boot/deps/pomelo-protobuf/index.js");
  1428. require.alias("pomelonode-pomelo-protobuf/lib/client/protobuf.js", "pomelonode-pomelo-protobuf/index.js");
  1429. require.alias("pomelonode-pomelo-jsclient-websocket/lib/pomelo-client.js", "boot/deps/pomelo-jsclient-websocket/lib/pomelo-client.js");
  1430. require.alias("pomelonode-pomelo-jsclient-websocket/lib/pomelo-client.js", "boot/deps/pomelo-jsclient-websocket/index.js");
  1431. require.alias("pomelonode-pomelo-jsclient-websocket/lib/pomelo-client.js", "pomelonode-pomelo-jsclient-websocket/index.js");