import DataModelBase from '../framework/DataModelBase'; import Event from './Event'; import Common from '../common/Common'; import CCipher from '../common/CCipher'; import Packet from '../net/Packet'; import ConstMsgId from '../net/ConstMsgId'; import MsgTransfer from '../net/MsgTransfer'; import Define from '../common/Define'; //var protobuf = require("protobufjs"); //var PBTransfer = require("PBTransfer"); //var ByteBuffer = protobuf.ByteBuffer; export default class Connect extends DataModelBase { constructor(url,isShortConnect) { super('Connect'); this._url = url; Common.Print('Connect url:' + url); this._isShortConnect = isShortConnect; this._state = Common.SocketState.Closed; this._webSocket = null; this._protoLoaded = false; this._protoMsgClass = {}; this._messageIndex = 0; this._bNeedClose = false; // 客户端需要被主动关闭 this._reConnectInterval = 3000; this._bReconnect = false; this._closeTimeOut = null; this._errorTimeOut = null; this._timeOut = null; this._sendDataBuf = new Array(); this._heatInterval = 15000;//15 second this._pingTimes = 0; this._socketType = Define.SOCKET_TYPE.PLAZA } // var protoFiles = [ // ["resources/proto/authmsg.proto", "./authmsg.proto"], // ["resources/proto/commsg.proto", "./commsg.proto"], // ["resources/proto/gamemsg.proto", "./gamemsg.proto"] // ]; //加载proto文件,在游戏初始时进行,推荐在资源加载时调用 //需要等待proto全部加载完成之后,才能使用socket loadProto (protoFiles,callback) { let con = this con._protoMsgClass = {}; cc.loader.loadResDir("proto", function(err, array) { for (var i=0, dep, data; i 0 || wSubCmdID != Packet.SUB_KN_DETECT_SOCKET) // { // var pack = null // if(dataviewLength > Packet.TCPInfoSize) // { // var packBuf = evt.data.slice(Packet.TCPHeadSize,dataviewLength) // } // this.emit(Event.SOCKET_MSG.DATA, wMainCmdID,wSubCmdID,packBuf) // } } //错误监听 _onError(evt) { //GH.log("GameSocket _onError", evt); console.log(Common.GetDateString() + 'GameSocket _onError'+ JSON.stringify(evt)) let self = this; this._state = Common.SocketState.Error; if(this._timeOut!=null) { clearTimeout(this._timeOut); this._timeOut = null; } if(this._bNeedClose != true) { this._errorTimeOut = setTimeout(function() { self.open(true); }, self._reConnectInterval); } this.emit(Event.SOCKET_MSG.ERROR); } //关闭监听 _onClose(evt) { console.log(Common.GetDateString() + 'GameSocket _onClose'+ JSON.stringify(evt)) var state = this.getConnectState() console.log(Common.GetDateString() + 'GameSocket _onClose state :'+ state) if( Common.SocketState.OK == state ) { return } this._state = Common.SocketState.Closed; var bClientClosed = this._bClientClosed; let self = this; if(this._timeOut!=null) { clearTimeout(this._timeOut); this._timeOut = null; } if(this._bNeedClose != true) { this._closeTimeOut = setTimeout(function() { self.open(true); }, self._reConnectInterval); } this.emit(Event.SOCKET_MSG.CLOSE,bClientClosed); } _sendData(dataBuf) { if(!dataBuf || !(dataBuf instanceof ArrayBuffer)) return -1; if (!this._webSocket) return -1; if(this.getConnectState() != Common.SocketState.OK) { return this._state; } if (this._webSocket.readyState === WebSocket.OPEN) { console.log('_sendData') this._webSocket.send(dataBuf); return 0; } else { this._state = this.getConnectState(); //this._webSocket.close(); return this._state; } } checkHeartBeat() { var self = this; // if(this._bClientClosed = true) // { // return // } self._timeOut = setTimeout(function () { if(self._bNeedClose != true) { if (self.pingTimes >= 3) //6次没有收到ping消息,认为断了线 { Common.Print('pingTimes >= 3'); self.pingTimes = 0; self.open(true);//认为已经断开连接//然后重连 return; } //记录心跳次数 self.pingTimes++; self.sendPing(); } self.checkHeartBeat(); }, this._heatInterval); } sendPing() { console.log(Common.GetDateString() + 'sendPing'); // var msg = MsgTransfer.encodemsg(ConstMsgId.MDM_KN_COMMAND ,ConstMsgId.SUB_KN_DETECT_SOCKET,92 ) var msg = "{type:ping}" // console.log('msg is: ',sre); this.sendMsg(msg); } }