NetworkMgr.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. const HttpClient = require('HttpClient');
  2. const SocketClient = require('SocketClient');
  3. let NetworkMgr = {
  4. http: undefined,
  5. socket: undefined,
  6. _localStorage: undefined,
  7. _connections: [],
  8. _httpLoginConn: undefined,
  9. _httpLogConn: undefined,
  10. _httpGameConn: undefined,
  11. /**
  12. * 初始化网络管理器
  13. *
  14. * @author Wetion
  15. * @date 2019-03-22
  16. */
  17. init () {
  18. if (CC_EDITOR) {
  19. return;
  20. }
  21. this._initLocalStorage();
  22. if (!G.OPEN_DEBUG && G.RELEASE_SERVER_NAME) {
  23. // 特定渠道包判断
  24. // 在关闭Debug 并且 RELEASE_SERVER_NAME 存在的情况下
  25. // 将默认的服务器名改成 RELEASE_SERVER_NAME
  26. this.setStorageValue('defaultServerName', G.RELEASE_SERVER_NAME);
  27. }
  28. },
  29. /**
  30. * 初始化连接
  31. *
  32. * @author Pyden
  33. * @date 2020-03-02
  34. */
  35. initConn () {
  36. let connectServer;
  37. let defaultServerName = this.getStorageValue('defaultServerName');
  38. if (defaultServerName) {
  39. JMC.CONNECTIONS.forEach(server => {
  40. if (server.name === defaultServerName) {
  41. connectServer = server;
  42. }
  43. });
  44. }
  45. if (!connectServer) {
  46. connectServer = JMC.CONNECTIONS[0];
  47. }
  48. this.setConnections(connectServer.conns, connectServer.name);
  49. // 创建HTTP客户端
  50. this.createHttpClient(JMC.HTTP_PROTO_FILES);
  51. // 创建SOCKET客户端
  52. this.createSocketClient(JMC.SOCKET_PROTO_FILES);
  53. },
  54. _initLocalStorage () {
  55. this._localStorage = G.StorageMgr.getStorage('networkStorage');
  56. },
  57. /**
  58. * 增加连接
  59. *
  60. * @author Wetion
  61. * @date 2019-03-28
  62. * @param {Object} newConn
  63. */
  64. addConnection (newConn) {
  65. if (!newConn) {
  66. return;
  67. }
  68. let conn = this.getConnectionByName(newConn.name);
  69. if (conn) {
  70. this._updateConnection(conn, newConn); // 更新该连接
  71. } else {
  72. this._connections.push(newConn); // 增加该连接
  73. }
  74. // 重置连接
  75. this.setConnections(this._connections);
  76. },
  77. /**
  78. * 设置连接
  79. *
  80. * @author Wetion
  81. * @date 2019-03-28
  82. * @param {Array} conns 服务器连接池
  83. * @param {String} serverName 服务器名称
  84. */
  85. setConnections (conns, serverName) {
  86. if (!conns || conns.length === 0) {
  87. return;
  88. }
  89. this._clearConnections();
  90. this._connections = conns;
  91. conns.forEach(conn => {
  92. switch (conn.name) {
  93. case 'HTTP_LOGIN':
  94. this._httpLoginConn = conn;
  95. break;
  96. case 'HTTP_LOG':
  97. this._httpLogConn = conn;
  98. break;
  99. case 'HTTP_GAME':
  100. this._httpGameConn = conn;
  101. break;
  102. default:
  103. break;
  104. }
  105. });
  106. if (serverName) {
  107. this.setStorageValue('defaultServerName', serverName);
  108. }
  109. },
  110. /**
  111. * 根据名称获取连接对象
  112. *
  113. * @author Wetion
  114. * @date 2019-03-28
  115. * @param {String} name
  116. * @returns {Object} 找到的连接
  117. */
  118. getConnectionByName (name) {
  119. this._connections.forEach(conn => {
  120. if (conn.name === name) {
  121. return conn;
  122. }
  123. });
  124. },
  125. _updateConnection (conn, newConn) {
  126. if (!conn || !newConn) {
  127. return;
  128. }
  129. conn.name = newConn.name;
  130. conn.host = newConn.host;
  131. conn.port = newConn.port;
  132. conn.path = newConn.path;
  133. conn.method = newConn.method;
  134. conn.secretCode = newConn.secretCode;
  135. return conn;
  136. },
  137. _clearConnections () {
  138. this._httpLoginConn = undefined;
  139. this._httpLogConn = undefined;
  140. this._httpGameConn = undefined;
  141. this._socketGameConn = undefined;
  142. },
  143. /**
  144. * 获取管理器的本地存储值
  145. *
  146. * @author Wetion
  147. * @date 2019-03-27
  148. * @param {String} key
  149. * @returns {object}
  150. */
  151. getStorageValue (key) {
  152. if (!this._localStorage) {
  153. G.LogUtils.error('Bug:用户本地存储不存在');
  154. return;
  155. }
  156. return this._localStorage.getValue(key);
  157. },
  158. /**
  159. * 设置管理器本地存储值
  160. *
  161. * @author Wetion
  162. * @date 2019-03-27
  163. * @param {String} key
  164. * @param {object} value
  165. */
  166. setStorageValue (key, value) {
  167. if (!this._localStorage) {
  168. G.LogUtils.error('Bug:用户本地存储不存在');
  169. return;
  170. }
  171. this._localStorage.setValue(key, value);
  172. },
  173. /**
  174. * 批量设置管理器本地存储值
  175. *
  176. * @author Wetion
  177. * @date 2019-03-27
  178. * @param {Object} values
  179. */
  180. setStorageValues (values) {
  181. if (!this._localStorage) {
  182. G.LogUtils.error('Bug:用户本地存储不存在');
  183. return;
  184. }
  185. this._localStorage.setValues(values);
  186. },
  187. /**
  188. * 创建一个 SocketClient 对象
  189. *
  190. * @author Wetion
  191. * @date 2019-03-22
  192. * @param {Array} protoFiles 协议文件列表
  193. * @returns {SocketClient}
  194. */
  195. createSocketClient (protoFiles) {
  196. if (!this.socket) {
  197. this.socket = new SocketClient();
  198. this.socket.connectTimeout = 20000;
  199. this.socket.reconnectMaxTimes = 0;
  200. this.socket.loadProtoFiles(protoFiles);
  201. }
  202. return this.socket;
  203. },
  204. /**
  205. * 连接 SOCKET
  206. *
  207. * @author Wetion
  208. * @date 2019-03-22
  209. * @param {String} host
  210. * @param {Number} port
  211. */
  212. connectSocket (host, port) {
  213. if (!this.socket) {
  214. G.LogUtils.warn('Warn:请先创建一个 SocketClient');
  215. return;
  216. }
  217. if (!host && !port) {
  218. G.LogUtils.error('Bug:连接IP、PORT不能为空');
  219. return;
  220. }
  221. this.socket.connect(host, port);
  222. },
  223. isConnecting () {
  224. return this.socket.isReadyDone();
  225. },
  226. /**
  227. * 重新连接SOCKET
  228. *
  229. * @author Wetion
  230. * @date 2019-03-27
  231. */
  232. reconnectSocket () {
  233. if (!this.socket) {
  234. G.LogUtils.warn('Warn:请先创建一个 SocketClient');
  235. return;
  236. }
  237. this.socket.reconnect();
  238. },
  239. /**
  240. * 关闭 SOCKET 连接
  241. *
  242. * @author Wetion
  243. * @date 2019-03-26
  244. */
  245. closeSocket () {
  246. if (!this.socket) {
  247. return;
  248. }
  249. this.socket.close();
  250. },
  251. /**
  252. * SOCKET客户端请求服务器信息
  253. *
  254. * @author Wetion
  255. * @date 2019-03-22
  256. * @param {String} protoName 协议名称
  257. * @param {Object} info 协议内容
  258. * @param {Function} responseHandler 响应回调
  259. * @returns {Boolean} 是否成功请求
  260. */
  261. sendSocketRequest (protoName, info, responseHandler) {
  262. if (!this.socket) {
  263. G.LogUtils.warn('Warn:请先创建一个 SocketClient');
  264. return;
  265. }
  266. return this.socket.c2sRequest(protoName, info, responseHandler);
  267. },
  268. /**
  269. * 请求单机场信息
  270. *
  271. * @author Wetion
  272. * @date 2019-03-22
  273. * @param {String} protoName 协议名称
  274. * @param {Object} info 协议内容
  275. * @param {Function} responseHandler 响应回调
  276. * @returns {Boolean} 是否成功请求
  277. */
  278. sendSingleRequest (protoName, info, responseHandler) {
  279. if (!MJ.lib_single) {
  280. G.LogUtils.error('包内无单机模块');
  281. return false;
  282. }
  283. let responseInfo = MJ.lib_single.c2b_request(protoName, info, '-');
  284. if (responseHandler) {
  285. setTimeout(() => {
  286. responseHandler({responseInfo: responseInfo, requestInfo: info});
  287. }, 1);
  288. }
  289. return true;
  290. },
  291. /**
  292. * 创建一个 HttpClient 对象
  293. *
  294. * @author Wetion
  295. * @date 2019-03-22
  296. * @param {Array} protoFiles 协议文件列表
  297. * @returns {HttpClient}
  298. */
  299. createHttpClient (protoFiles) {
  300. if (!this.http) {
  301. this.http = new HttpClient();
  302. this.http.method = 'POST';
  303. this.http.secretCode = '0bdb9a5863c8c9063a42f38a9d5166fe';
  304. this.http.loadProtoFiles(protoFiles);
  305. }
  306. return this.http;
  307. },
  308. /**
  309. * HTTP客户端请求服务器信息
  310. *
  311. * @author Wetion
  312. * @date 2019-03-22
  313. * @param {String} protoName
  314. * @param {Object} info
  315. * @param {Function} responseHandler
  316. * @param {Object} connInfo
  317. */
  318. sendHttpRequest (protoName, info, responseHandler, conn) {
  319. if (!this.http) {
  320. G.LogUtils.warn('Warn:请先创建一个 HttpClient');
  321. return;
  322. }
  323. this.http.setURLArgs(conn.host, conn.port, conn.path);
  324. this.http.c2sRequest(protoName, info, responseHandler);
  325. },
  326. /**
  327. * HTTP客户端请求 登陆服务器 信息
  328. *
  329. * @author Wetion
  330. * @date 2019-03-22
  331. * @param {String} protoName
  332. * @param {Object} info
  333. * @param {Function} responseHandler
  334. */
  335. sendHttpRequestToLoginServer (protoName, info, responseHandler) {
  336. this.sendHttpRequest(protoName, info, responseHandler, this._httpLoginConn);
  337. },
  338. /**
  339. * HTTP客户端请求 游戏服务器 信息
  340. *
  341. * @author Wetion
  342. * @date 2019-03-22
  343. * @param {String} protoName
  344. * @param {Object} info
  345. * @param {Function} responseHandler
  346. */
  347. sendHttpRequestToGameServer (protoName, info, responseHandler) {
  348. this.sendHttpRequest(protoName, info, responseHandler, this._httpGameConn);
  349. },
  350. /**
  351. * HTTP客户端请求 日志服务器 信息
  352. *
  353. * @author Wetion
  354. * @date 2019-03-22
  355. * @param {String} protoName
  356. * @param {Object} info
  357. * @param {Function} responseHandler
  358. */
  359. sendHttpRequestToLogServer (protoName, info, responseHandler) {
  360. this.sendHttpRequest(protoName, info, responseHandler, this._httpLogConn);
  361. },
  362. // 获取用户下载头像的地址
  363. getHeadDownloadUrlForUser () {
  364. let defaultServerName = this.getStorageValue('defaultServerName');
  365. if (defaultServerName == 'FORMAL_SERVER') {
  366. return JMC.HEAD_DOWNLOAD_URL;
  367. } else {
  368. return JMC.HEAD_DOWNLOAD_URL_DEBUG;
  369. }
  370. },
  371. // 获取机器人下载头像的地址
  372. getHeadDownloadUrlForRobot () {
  373. return JMC.RES_ROOT_URL;
  374. },
  375. // 获取在线参数的地址
  376. getParamsUrl () {
  377. let defaultServerName = this.getStorageValue('defaultServerName') || JMC.CONNECTIONS[0].name;
  378. if (defaultServerName == 'FORMAL_SERVER') {
  379. return JMC.PARAM_URL;
  380. } else {
  381. return JMC.PARAM_URL_DEBUG;
  382. }
  383. }
  384. };
  385. module.exports = NetworkMgr;