build.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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, userstate 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 = 'ws://' + 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");