const HttpClient = require('HttpClient'); const SocketClient = require('SocketClient'); let NetworkMgr = { http: undefined, socket: undefined, _connections: [], _httpLoginConn: undefined, _httpLogConn: undefined, _httpGameConn: undefined, /** * 初始化网络管理器 * * @author Wetion * @date 2019-03-22 */ init () { if (CC_EDITOR) { return; } }, /** * 初始化连接 * * @author Pyden * @date 2020-03-02 */ initConn () { let connectServer = JMC.CONNECTIONS[0]; this.setConnections(connectServer.conns, connectServer.name); // 创建HTTP客户端 this.createHttpClient(JMC.HTTP_PROTO_FILES); // 创建SOCKET客户端 this.createSocketClient(JMC.SOCKET_PROTO_FILES); }, /** * 增加连接 * * @author Wetion * @date 2019-03-28 * @param {Object} newConn */ addConnection (newConn) { if (!newConn) { return; } let conn = this.getConnectionByName(newConn.name); if (conn) { this._updateConnection(conn, newConn); // 更新该连接 } else { this._connections.push(newConn); // 增加该连接 } // 重置连接 this.setConnections(this._connections); }, /** * 设置连接 * * @author Wetion * @date 2019-03-28 * @param {Array} conns 服务器连接池 * @param {String} serverName 服务器名称 */ setConnections (conns, serverName) { if (!conns || conns.length === 0) { return; } this._clearConnections(); this._connections = conns; conns.forEach(conn => { switch (conn.name) { case 'HTTP_LOGIN': this._httpLoginConn = conn; break; default: break; } }); }, /** * 根据名称获取连接对象 * * @author Wetion * @date 2019-03-28 * @param {String} name * @returns {Object} 找到的连接 */ getConnectionByName (name) { this._connections.forEach(conn => { if (conn.name === name) { return conn; } }); }, _updateConnection (conn, newConn) { if (!conn || !newConn) { return; } conn.name = newConn.name; conn.host = newConn.host; conn.port = newConn.port; conn.path = newConn.path; conn.method = newConn.method; conn.secretCode = newConn.secretCode; return conn; }, _clearConnections () { this._httpLoginConn = undefined; this._httpLogConn = undefined; this._httpGameConn = undefined; this._socketGameConn = undefined; }, /** * 创建一个 SocketClient 对象 * * @author Wetion * @date 2019-03-22 * @param {Array} protoFiles 协议文件列表 * @returns {SocketClient} */ createSocketClient (protoFiles) { if (!this.socket) { this.socket = new SocketClient(); this.socket.connectTimeout = 20000; this.socket.reconnectMaxTimes = 0; this.socket.loadProtoFiles(protoFiles); } return this.socket; }, /** * 连接 SOCKET * * @author Wetion * @date 2019-03-22 * @param {String} host * @param {Number} port */ connectSocket (host, port) { if (!this.socket) { G.LogUtils.warn('Warn:请先创建一个 SocketClient'); return; } if (!host && !port) { G.LogUtils.error('Bug:连接IP、PORT不能为空'); return; } this.socket.connect(host, port); }, isConnecting () { return this.socket.isReadyDone(); }, /** * 重新连接SOCKET * * @author Wetion * @date 2019-03-27 */ reconnectSocket () { if (!this.socket) { G.LogUtils.warn('Warn:请先创建一个 SocketClient'); return; } this.socket.reconnect(); }, /** * 关闭 SOCKET 连接 * * @author Wetion * @date 2019-03-26 */ closeSocket () { if (!this.socket) { return; } this.socket.close(); }, /** * SOCKET客户端请求服务器信息 * * @author Wetion * @date 2019-03-22 * @param {String} protoName 协议名称 * @param {Object} info 协议内容 * @param {Function} responseHandler 响应回调 * @returns {Boolean} 是否成功请求 */ sendSocketRequest (protoName, info, responseHandler) { if (!this.socket) { G.LogUtils.warn('Warn:请先创建一个 SocketClient'); return; } return this.socket.c2sRequest(protoName, info, responseHandler); }, /** * 请求单机场信息 * * @author Wetion * @date 2019-03-22 * @param {String} protoName 协议名称 * @param {Object} info 协议内容 * @param {Function} responseHandler 响应回调 * @returns {Boolean} 是否成功请求 */ sendSingleRequest (protoName, info, responseHandler) { if (!MJ.lib_single) { G.LogUtils.error('包内无单机模块'); return false; } let responseInfo = MJ.lib_single.c2b_request(protoName, info, '-'); if (responseHandler) { setTimeout(() => { responseHandler({responseInfo: responseInfo, requestInfo: info}); }, 1); } return true; }, /** * 创建一个 HttpClient 对象 * * @author Wetion * @date 2019-03-22 * @param {Array} protoFiles 协议文件列表 * @returns {HttpClient} */ createHttpClient (protoFiles) { if (!this.http) { this.http = new HttpClient(); this.http.method = 'POST'; this.http.secretCode = '0bdb9a5863c8c9063a42f38a9d5166fe'; this.http.loadProtoFiles(protoFiles); } return this.http; }, /** * HTTP客户端请求服务器信息 * * @author Wetion * @date 2019-03-22 * @param {String} protoName * @param {Object} info * @param {Function} responseHandler * @param {Object} connInfo */ sendHttpRequest (protoName, info, responseHandler, conn) { if (!this.http) { G.LogUtils.warn('Warn:请先创建一个 HttpClient'); return; } this.http.setURLArgs(conn.host, conn.port, conn.path); this.http.c2sRequest(protoName, info, responseHandler); }, /** * HTTP客户端请求 登陆服务器 信息 * * @author Wetion * @date 2019-03-22 * @param {String} protoName * @param {Object} info * @param {Function} responseHandler */ sendHttpRequestToLoginServer (protoName, info, responseHandler) { this.sendHttpRequest(protoName, info, responseHandler, this._httpLoginConn); }, /** * HTTP客户端请求 游戏服务器 信息 * * @author Wetion * @date 2019-03-22 * @param {String} protoName * @param {Object} info * @param {Function} responseHandler */ sendHttpRequestToGameServer (protoName, info, responseHandler) { this.sendHttpRequest(protoName, info, responseHandler, this._httpGameConn); }, // 获取在线参数的地址 getParamsUrl () { let defaultServerName = JMC.CONNECTIONS[0].name; if (defaultServerName == 'FORMAL_SERVER') { return JMC.PARAM_URL; } else { return JMC.PARAM_URL_DEBUG; } } }; module.exports = NetworkMgr;