pomeloclient.js 11 KB

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