NetworkMgr.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. const HttpClient = require('HttpClient');
  2. const SocketClient = require('SocketClient');
  3. let NetworkMgr = {
  4. http: undefined,
  5. socket: undefined,
  6. _connections: [],
  7. _httpLoginConn: undefined,
  8. _httpLogConn: undefined,
  9. _httpGameConn: undefined,
  10. /**
  11. * 初始化网络管理器
  12. *
  13. * @author Wetion
  14. * @date 2019-03-22
  15. */
  16. init () {
  17. if (CC_EDITOR) {
  18. return;
  19. }
  20. },
  21. /**
  22. * 初始化连接
  23. *
  24. * @author Pyden
  25. * @date 2020-03-02
  26. */
  27. initConn () {
  28. let connectServer = JMC.CONNECTIONS[0];
  29. this.setConnections(connectServer.conns, connectServer.name);
  30. // 创建HTTP客户端
  31. this.createHttpClient(JMC.HTTP_PROTO_FILES);
  32. // 创建SOCKET客户端
  33. this.createSocketClient(JMC.SOCKET_PROTO_FILES);
  34. },
  35. /**
  36. * 增加连接
  37. *
  38. * @author Wetion
  39. * @date 2019-03-28
  40. * @param {Object} newConn
  41. */
  42. addConnection (newConn) {
  43. if (!newConn) {
  44. return;
  45. }
  46. let conn = this.getConnectionByName(newConn.name);
  47. if (conn) {
  48. this._updateConnection(conn, newConn); // 更新该连接
  49. } else {
  50. this._connections.push(newConn); // 增加该连接
  51. }
  52. // 重置连接
  53. this.setConnections(this._connections);
  54. },
  55. /**
  56. * 设置连接
  57. *
  58. * @author Wetion
  59. * @date 2019-03-28
  60. * @param {Array} conns 服务器连接池
  61. * @param {String} serverName 服务器名称
  62. */
  63. setConnections (conns, serverName) {
  64. if (!conns || conns.length === 0) {
  65. return;
  66. }
  67. this._clearConnections();
  68. this._connections = conns;
  69. conns.forEach(conn => {
  70. switch (conn.name) {
  71. case 'HTTP_LOGIN':
  72. this._httpLoginConn = conn;
  73. break;
  74. default:
  75. break;
  76. }
  77. });
  78. },
  79. /**
  80. * 根据名称获取连接对象
  81. *
  82. * @author Wetion
  83. * @date 2019-03-28
  84. * @param {String} name
  85. * @returns {Object} 找到的连接
  86. */
  87. getConnectionByName (name) {
  88. this._connections.forEach(conn => {
  89. if (conn.name === name) {
  90. return conn;
  91. }
  92. });
  93. },
  94. _updateConnection (conn, newConn) {
  95. if (!conn || !newConn) {
  96. return;
  97. }
  98. conn.name = newConn.name;
  99. conn.host = newConn.host;
  100. conn.port = newConn.port;
  101. conn.path = newConn.path;
  102. conn.method = newConn.method;
  103. conn.secretCode = newConn.secretCode;
  104. return conn;
  105. },
  106. _clearConnections () {
  107. this._httpLoginConn = undefined;
  108. this._httpLogConn = undefined;
  109. this._httpGameConn = undefined;
  110. this._socketGameConn = undefined;
  111. },
  112. /**
  113. * 创建一个 SocketClient 对象
  114. *
  115. * @author Wetion
  116. * @date 2019-03-22
  117. * @param {Array} protoFiles 协议文件列表
  118. * @returns {SocketClient}
  119. */
  120. createSocketClient (protoFiles) {
  121. if (!this.socket) {
  122. this.socket = new SocketClient();
  123. this.socket.connectTimeout = 20000;
  124. this.socket.reconnectMaxTimes = 0;
  125. this.socket.loadProtoFiles(protoFiles);
  126. }
  127. return this.socket;
  128. },
  129. /**
  130. * 连接 SOCKET
  131. *
  132. * @author Wetion
  133. * @date 2019-03-22
  134. * @param {String} host
  135. * @param {Number} port
  136. */
  137. connectSocket (host, port) {
  138. if (!this.socket) {
  139. G.LogUtils.warn('Warn:请先创建一个 SocketClient');
  140. return;
  141. }
  142. if (!host && !port) {
  143. G.LogUtils.error('Bug:连接IP、PORT不能为空');
  144. return;
  145. }
  146. this.socket.connect(host, port);
  147. },
  148. isConnecting () {
  149. return this.socket.isReadyDone();
  150. },
  151. /**
  152. * 重新连接SOCKET
  153. *
  154. * @author Wetion
  155. * @date 2019-03-27
  156. */
  157. reconnectSocket () {
  158. if (!this.socket) {
  159. G.LogUtils.warn('Warn:请先创建一个 SocketClient');
  160. return;
  161. }
  162. this.socket.reconnect();
  163. },
  164. /**
  165. * 关闭 SOCKET 连接
  166. *
  167. * @author Wetion
  168. * @date 2019-03-26
  169. */
  170. closeSocket () {
  171. if (!this.socket) {
  172. return;
  173. }
  174. this.socket.close();
  175. },
  176. /**
  177. * SOCKET客户端请求服务器信息
  178. *
  179. * @author Wetion
  180. * @date 2019-03-22
  181. * @param {String} protoName 协议名称
  182. * @param {Object} info 协议内容
  183. * @param {Function} responseHandler 响应回调
  184. * @returns {Boolean} 是否成功请求
  185. */
  186. sendSocketRequest (protoName, info, responseHandler) {
  187. if (!this.socket) {
  188. G.LogUtils.warn('Warn:请先创建一个 SocketClient');
  189. return;
  190. }
  191. return this.socket.c2sRequest(protoName, info, responseHandler);
  192. },
  193. /**
  194. * 请求单机场信息
  195. *
  196. * @author Wetion
  197. * @date 2019-03-22
  198. * @param {String} protoName 协议名称
  199. * @param {Object} info 协议内容
  200. * @param {Function} responseHandler 响应回调
  201. * @returns {Boolean} 是否成功请求
  202. */
  203. sendSingleRequest (protoName, info, responseHandler) {
  204. if (!MJ.lib_single) {
  205. G.LogUtils.error('包内无单机模块');
  206. return false;
  207. }
  208. let responseInfo = MJ.lib_single.c2b_request(protoName, info, '-');
  209. if (responseHandler) {
  210. setTimeout(() => {
  211. responseHandler({responseInfo: responseInfo, requestInfo: info});
  212. }, 1);
  213. }
  214. return true;
  215. },
  216. /**
  217. * 创建一个 HttpClient 对象
  218. *
  219. * @author Wetion
  220. * @date 2019-03-22
  221. * @param {Array} protoFiles 协议文件列表
  222. * @returns {HttpClient}
  223. */
  224. createHttpClient (protoFiles) {
  225. if (!this.http) {
  226. this.http = new HttpClient();
  227. this.http.method = 'POST';
  228. this.http.secretCode = '0bdb9a5863c8c9063a42f38a9d5166fe';
  229. this.http.loadProtoFiles(protoFiles);
  230. }
  231. return this.http;
  232. },
  233. /**
  234. * HTTP客户端请求服务器信息
  235. *
  236. * @author Wetion
  237. * @date 2019-03-22
  238. * @param {String} protoName
  239. * @param {Object} info
  240. * @param {Function} responseHandler
  241. * @param {Object} connInfo
  242. */
  243. sendHttpRequest (protoName, info, responseHandler, conn) {
  244. if (!this.http) {
  245. G.LogUtils.warn('Warn:请先创建一个 HttpClient');
  246. return;
  247. }
  248. this.http.setURLArgs(conn.host, conn.port, conn.path);
  249. this.http.c2sRequest(protoName, info, responseHandler);
  250. },
  251. /**
  252. * HTTP客户端请求 登陆服务器 信息
  253. *
  254. * @author Wetion
  255. * @date 2019-03-22
  256. * @param {String} protoName
  257. * @param {Object} info
  258. * @param {Function} responseHandler
  259. */
  260. sendHttpRequestToLoginServer (protoName, info, responseHandler) {
  261. this.sendHttpRequest(protoName, info, responseHandler, this._httpLoginConn);
  262. },
  263. /**
  264. * HTTP客户端请求 游戏服务器 信息
  265. *
  266. * @author Wetion
  267. * @date 2019-03-22
  268. * @param {String} protoName
  269. * @param {Object} info
  270. * @param {Function} responseHandler
  271. */
  272. sendHttpRequestToGameServer (protoName, info, responseHandler) {
  273. this.sendHttpRequest(protoName, info, responseHandler, this._httpGameConn);
  274. },
  275. // 获取在线参数的地址
  276. getParamsUrl () {
  277. let defaultServerName = JMC.CONNECTIONS[0].name;
  278. if (defaultServerName == 'FORMAL_SERVER') {
  279. return JMC.PARAM_URL;
  280. } else {
  281. return JMC.PARAM_URL_DEBUG;
  282. }
  283. }
  284. };
  285. module.exports = NetworkMgr;