123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 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;
|