pomelo-client.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Copyright 2015 rain1017.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. module.exports = function(){
  17. var WebSocket = require('ws');
  18. var Protocol = require('pomelo-protocol');
  19. var Package = Protocol.Package;
  20. var Message = Protocol.Message;
  21. var EventEmitter = require('events').EventEmitter;
  22. var protocol = require('pomelo-protocol');
  23. var protobuf = require('pomelo-protobuf');
  24. var cwd = process.cwd();
  25. var util = require('util');
  26. if (typeof Object.create !== 'function') {
  27. Object.create = function(o) {
  28. function F() {
  29. }
  30. F.prototype = o;
  31. return new F();
  32. };
  33. }
  34. var JS_WS_CLIENT_TYPE = 'js-websocket';
  35. var JS_WS_CLIENT_VERSION = '0.0.1';
  36. var RES_OK = 200;
  37. var RES_OLD_CLIENT = 501;
  38. var pomelo = Object.create(EventEmitter.prototype); // object extend from object
  39. var socket = null;
  40. var reqId = 0;
  41. var callbacks = {};
  42. var handlers = {};
  43. var routeMap = {};
  44. var heartbeatInterval = 5000;
  45. var heartbeatTimeout = heartbeatInterval * 2;
  46. var nextHeartbeatTimeout = 0;
  47. var gapThreshold = 100; // heartbeat gap threshold
  48. var heartbeatId = null;
  49. var heartbeatTimeoutId = null;
  50. var handshakeCallback = null;
  51. var handshakeBuffer = {
  52. 'sys' : {
  53. type : JS_WS_CLIENT_TYPE,
  54. version : JS_WS_CLIENT_VERSION
  55. },
  56. 'user' : {}
  57. };
  58. var initCallback = null;
  59. pomelo.init = function(params, cb) {
  60. pomelo.params = params;
  61. params.debug = true;
  62. initCallback = cb;
  63. var host = params.host;
  64. var port = params.port;
  65. var url = 'ws://' + host;
  66. if (port) {
  67. url += ':' + port;
  68. }
  69. if (!params.type) {
  70. console.log('init websocket');
  71. handshakeBuffer.user = params.user;
  72. handshakeCallback = params.handshakeCallback;
  73. this.initWebSocket(url, cb);
  74. }
  75. };
  76. pomelo.initWebSocket = function(url, cb) {
  77. console.log(url);
  78. var onopen = function(event) {
  79. console.log('[pomeloclient.init] websocket connected!');
  80. var obj = Package.encode(Package.TYPE_HANDSHAKE, Protocol
  81. .strencode(JSON.stringify(handshakeBuffer)));
  82. send(obj);
  83. };
  84. var onmessage = function(event) {
  85. processPackage(Package.decode(event.data), cb);
  86. // new package arrived, update the heartbeat timeout
  87. if (heartbeatTimeout) {
  88. nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
  89. }
  90. };
  91. var onerror = function(event) {
  92. pomelo.emit('error', event);
  93. //console.log('socket error %j ', event);
  94. };
  95. var onclose = function(event) {
  96. pomelo.emit('close', event);
  97. //console.log('socket close %j ', event);
  98. };
  99. socket = new WebSocket(url);
  100. socket.binaryType = 'arraybuffer';
  101. socket.onopen = onopen;
  102. socket.onmessage = onmessage;
  103. socket.onerror = onerror;
  104. socket.onclose = onclose;
  105. };
  106. pomelo.disconnect = function() {
  107. if (socket) {
  108. if (socket.disconnect)
  109. socket.disconnect();
  110. if (socket.close)
  111. socket.close();
  112. console.log('disconnect');
  113. socket = null;
  114. }
  115. if (heartbeatId) {
  116. clearTimeout(heartbeatId);
  117. heartbeatId = null;
  118. }
  119. if (heartbeatTimeoutId) {
  120. clearTimeout(heartbeatTimeoutId);
  121. heartbeatTimeoutId = null;
  122. }
  123. };
  124. pomelo.request = function(route, msg, cb) {
  125. msg = msg || {};
  126. route = route || msg.route;
  127. if (!route) {
  128. console.log('fail to send request without route.');
  129. return;
  130. }
  131. reqId++;
  132. sendMessage(reqId, route, msg);
  133. callbacks[reqId] = cb;
  134. routeMap[reqId] = route;
  135. };
  136. pomelo.notify = function(route, msg) {
  137. msg = msg || {};
  138. sendMessage(0, route, msg);
  139. };
  140. var sendMessage = function(reqId, route, msg) {
  141. var type = reqId ? Message.TYPE_REQUEST : Message.TYPE_NOTIFY;
  142. // compress message by protobuf
  143. var protos = !!pomelo.data.protos ? pomelo.data.protos.client : {};
  144. if (!!protos[route]) {
  145. msg = protobuf.encode(route, msg);
  146. } else {
  147. msg = Protocol.strencode(JSON.stringify(msg));
  148. }
  149. var compressRoute = 0;
  150. if (pomelo.dict && pomelo.dict[route]) {
  151. route = pomelo.dict[route];
  152. compressRoute = 1;
  153. }
  154. msg = Message.encode(reqId, type, compressRoute, route, msg);
  155. var packet = Package.encode(Package.TYPE_DATA, msg);
  156. send(packet);
  157. };
  158. var _host = '';
  159. var _port = '';
  160. var _token = '';
  161. /*
  162. * var send = function(packet){ if (!!socket) { socket.send(packet.buffer ||
  163. * packet,{binary: true, mask: true}); } else { setTimeout(function() {
  164. * entry(_host, _port, _token, function() {console.log('Socket is null.
  165. * ReEntry!')}); }, 3000); } };
  166. */
  167. var send = function(packet) {
  168. if (!!socket) {
  169. try{
  170. socket.send(packet, {
  171. binary : true,
  172. mask : true
  173. });
  174. }
  175. catch(err){
  176. pomelo.emit('error', err);
  177. }
  178. }
  179. };
  180. var handler = {};
  181. var heartbeat = function(data) {
  182. var obj = Package.encode(Package.TYPE_HEARTBEAT);
  183. if (heartbeatTimeoutId) {
  184. clearTimeout(heartbeatTimeoutId);
  185. heartbeatTimeoutId = null;
  186. }
  187. if (heartbeatId) {
  188. // already in a heartbeat interval
  189. return;
  190. }
  191. heartbeatId = setTimeout(function() {
  192. heartbeatId = null;
  193. send(obj);
  194. nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
  195. heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, heartbeatTimeout);
  196. }, heartbeatInterval);
  197. };
  198. var heartbeatTimeoutCb = function() {
  199. var gap = nextHeartbeatTimeout - Date.now();
  200. if (gap > gapThreshold) {
  201. heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, gap);
  202. } else {
  203. console.error('server heartbeat timeout');
  204. pomelo.emit('heartbeat timeout');
  205. pomelo.disconnect();
  206. }
  207. };
  208. var handshake = function(data) {
  209. data = JSON.parse(Protocol.strdecode(data));
  210. if (data.code === RES_OLD_CLIENT) {
  211. pomelo.emit('error', 'client version not fullfill');
  212. return;
  213. }
  214. if (data.code !== RES_OK) {
  215. pomelo.emit('error', 'handshake fail');
  216. return;
  217. }
  218. handshakeInit(data);
  219. var obj = Package.encode(Package.TYPE_HANDSHAKE_ACK);
  220. send(obj);
  221. if (initCallback) {
  222. initCallback(socket);
  223. initCallback = null;
  224. }
  225. };
  226. var onData = function(data) {
  227. // probuff decode
  228. var msg = Message.decode(data);
  229. if (msg.id > 0) {
  230. msg.route = routeMap[msg.id];
  231. delete routeMap[msg.id];
  232. if (!msg.route) {
  233. return;
  234. }
  235. }
  236. msg.body = deCompose(msg);
  237. processMessage(pomelo, msg);
  238. };
  239. var onKick = function(data) {
  240. pomelo.emit('onKick');
  241. };
  242. handlers[Package.TYPE_HANDSHAKE] = handshake;
  243. handlers[Package.TYPE_HEARTBEAT] = heartbeat;
  244. handlers[Package.TYPE_DATA] = onData;
  245. handlers[Package.TYPE_KICK] = onKick;
  246. var processPackage = function(msg) {
  247. handlers[msg.type](msg.body);
  248. };
  249. var processMessage = function(pomelo, msg) {
  250. if (!msg || !msg.id) {
  251. // server push message
  252. // console.error('processMessage error!!!');
  253. pomelo.emit(msg.route, msg.body);
  254. return;
  255. }
  256. // if have a id then find the callback function with the request
  257. var cb = callbacks[msg.id];
  258. delete callbacks[msg.id];
  259. if (typeof cb !== 'function') {
  260. return;
  261. }
  262. cb(msg.body);
  263. return;
  264. };
  265. var processMessageBatch = function(pomelo, msgs) {
  266. for ( var i = 0, l = msgs.length; i < l; i++) {
  267. processMessage(pomelo, msgs[i]);
  268. }
  269. };
  270. var deCompose = function(msg) {
  271. var protos = !!pomelo.data.protos ? pomelo.data.protos.server : {};
  272. var abbrs = pomelo.data.abbrs;
  273. var route = msg.route;
  274. try {
  275. // Decompose route from dict
  276. if (msg.compressRoute) {
  277. if (!abbrs[route]) {
  278. console.error('illegal msg!');
  279. return {};
  280. }
  281. route = msg.route = abbrs[route];
  282. }
  283. if (!!protos[route]) {
  284. return protobuf.decode(route, msg.body);
  285. } else {
  286. return JSON.parse(Protocol.strdecode(msg.body));
  287. }
  288. } catch (ex) {
  289. console.error('route, body = ' + route + ', ' + msg.body);
  290. }
  291. return msg;
  292. };
  293. var handshakeInit = function(data) {
  294. if (data.sys && data.sys.heartbeat) {
  295. heartbeatInterval = data.sys.heartbeat * 1000; // heartbeat interval
  296. heartbeatTimeout = heartbeatInterval * 2; // max heartbeat timeout
  297. } else {
  298. heartbeatInterval = 0;
  299. heartbeatTimeout = 0;
  300. }
  301. initData(data);
  302. if (typeof handshakeCallback === 'function') {
  303. handshakeCallback(data.user);
  304. }
  305. };
  306. // Initilize data used in pomelo client
  307. var initData = function(data) {
  308. if (!data || !data.sys) {
  309. return;
  310. }
  311. pomelo.data = pomelo.data || {};
  312. var dict = data.sys.dict;
  313. var protos = data.sys.protos;
  314. // Init compress dict
  315. if (!!dict) {
  316. pomelo.data.dict = dict;
  317. pomelo.data.abbrs = {};
  318. for ( var route in dict) {
  319. pomelo.data.abbrs[dict[route]] = route;
  320. }
  321. }
  322. // Init protobuf protos
  323. if (!!protos) {
  324. pomelo.data.protos = {
  325. server : protos.server || {},
  326. client : protos.client || {}
  327. };
  328. if (!!protobuf) {
  329. protobuf.init({
  330. encoderProtos : protos.client,
  331. decoderProtos : protos.server
  332. });
  333. }
  334. }
  335. };
  336. return pomelo;
  337. };