var enet = ( function ( exports ) { 'use strict' ; var DefaultNetEventHandler = class ( function ( ) { function DefaultNetEventHandler ( ) { } DefaultNetEventHandler . prototype . onStartConnenct = function ( connectOpt ) { console . log ( "start connect:" + connectOpt . url + ",opt:" , connectOpt ) ; } ; DefaultNetEventHandler . prototype . onConnectEnd = function ( connectOpt , handshakeRes ) { console . log ( "connect ok:" + connectOpt . url + ",opt:" , connectOpt ) ; console . log ( "handshakeRes:" , handshakeRes ) ; } ; DefaultNetEventHandler . prototype . onError = function ( event , connectOpt ) { console . error ( "socket error,opt:" , connectOpt ) ; console . error ( event ) ; } ; DefaultNetEventHandler . prototype . onClosed = function ( event , connectOpt ) { console . error ( "socket close,opt:" , connectOpt ) ; console . error ( event ) ; } ; DefaultNetEventHandler . prototype . onStartReconnect = function ( reConnectCfg , connectOpt ) { console . log ( "start reconnect:" + connectOpt . url + ",opt:" , connectOpt ) ; } ; DefaultNetEventHandler . prototype . onReconnecting = function ( curCount , reConnectCfg , connectOpt ) { console . log ( "url:" + connectOpt . url + " reconnect count:" + curCount + ",less count:" + reConnectCfg . reconnectCount + ",opt:" , connectOpt ) ; } ; DefaultNetEventHandler . prototype . onReconnectEnd = function ( isOk , reConnectCfg , connectOpt ) { console . log ( "url:" + connectOpt . url + "reconnect end " + ( isOk ? "ok" : "fail" ) + " ,opt:" , connectOpt ) ; } ; DefaultNetEventHandler . prototype . onStartRequest = function ( reqCfg , connectOpt ) { console . log ( "start request:" + reqCfg . protoKey + ",id:" + reqCfg . reqId + ",opt:" , connectOpt ) ; console . log ( "reqCfg:" , reqCfg ) ; } ; DefaultNetEventHandler . prototype . onData = function ( dpkg , connectOpt ) { console . log ( "data :" + dpkg . key + ",opt:" , connectOpt ) ; } ; DefaultNetEventHandler . prototype . onRequestTimeout = function ( reqCfg , connectOpt ) { console . warn ( "request timeout:" + reqCfg . protoKey + ",opt:" , connectOpt ) ; } ; DefaultNetEventHandler . prototype . onCustomError = function ( dpkg , connectOpt ) { console . error ( "proto key:" + dpkg . key + ",reqId:" + dpkg . reqId + ",code:" + dpkg . code + ",errorMsg:" + dpkg . errorMsg + ",opt:" , connectOpt ) ; } ; DefaultNetEventHandler . prototype . onKick = function ( dpkg , copt ) { console . log ( "be kick,opt:" , copt ) ; } ; return DefaultNetEventHandler ; } ( ) ) ; ( function ( PackageType ) { /**握手 */ PackageType [ PackageType [ "HANDSHAKE" ] = 1 ] = "HANDSHAKE" ; /**握手回应 */ PackageType [ PackageType [ "HANDSHAKE_ACK" ] = 2 ] = "HANDSHAKE_ACK" ; /**心跳 */ PackageType [ PackageType [ "HEARTBEAT" ] = 3 ] = "HEARTBEAT" ; /**数据 */ PackageType [ PackageType [ "DATA" ] = 4 ] = "DATA" ; /**踢下线 */ PackageType [ PackageType [ "KICK" ] = 5 ] = "KICK" ; } ) ( exports . PackageType || ( exports . PackageType = { } ) ) ; ( function ( SocketState ) { /**连接中 */ SocketState [ SocketState [ "CONNECTING" ] = 0 ] = "CONNECTING" ; /**打开 */ SocketState [ SocketState [ "OPEN" ] = 1 ] = "OPEN" ; /**关闭中 */ SocketState [ SocketState [ "CLOSING" ] = 2 ] = "CLOSING" ; /**关闭了 */ SocketState [ SocketState [ "CLOSED" ] = 3 ] = "CLOSED" ; } ) ( exports . SocketState || ( exports . SocketState = { } ) ) ; var WSocket = class ( function ( ) { function WSocket ( ) { } Object . defineProperty ( WSocket . prototype , "state" , { get : function ( ) { return this . _sk ? this . _sk . readyState : exports . SocketState . CLOSED ; } , enumerable : false , configurable : true } ) ; Object . defineProperty ( WSocket . prototype , "isConnected" , { get : function ( ) { return this . _sk ? this . _sk . readyState === exports . SocketState . OPEN : false ; } , enumerable : false , configurable : true } ) ; WSocket . prototype . setEventHandler = function ( handler ) { this . _eventHandler = handler ; } ; WSocket . prototype . connect = function ( opt ) { var _a , _b , _c , _d , _e , _f , _g , _h ; var url = opt . url ; if ( ! url ) { if ( opt . host && opt . port ) { url = ( opt . protocol ? "wss" : "ws" ) + "://" + opt . host + ":" + opt . port ; } else { return false ; } } opt . url = url ; if ( this . _sk ) { this . close ( true ) ; } if ( ! this . _sk ) { this . _sk = new WebSocket ( url ) ; if ( ! opt . binaryType ) { opt . binaryType = "arraybuffer" ; } this . _sk . binaryType = opt . binaryType ; this . _sk . onclose = ( ( _a = this . _eventHandler ) === null || _a === void 0 ? void 0 : _a . onSocketClosed ) && ( ( _b = this . _eventHandler ) === null || _b === void 0 ? void 0 : _b . onSocketClosed ) ; this . _sk . onerror = ( ( _c = this . _eventHandler ) === null || _c === void 0 ? void 0 : _c . onSocketError ) && ( ( _d = this . _eventHandler ) === null || _d === void 0 ? void 0 : _d . onSocketError ) ; this . _sk . onmessage = ( ( _e = this . _eventHandler ) === null || _e === void 0 ? void 0 : _e . onSocketMsg ) && ( ( _f = this . _eventHandler ) === null || _f === void 0 ? void 0 : _f . onSocketMsg ) ; this . _sk . onopen = ( ( _g = this . _eventHandler ) === null || _g === void 0 ? void 0 : _g . onSocketConnected ) && ( ( _h = this . _eventHandler ) === null || _h === void 0 ? void 0 : _h . onSocketConnected ) ; } } ; WSocket . prototype . send = function ( data ) { if ( this . _sk ) { this . _sk . send ( data ) ; } else { console . error ( "socket is null" ) ; } } ; WSocket . prototype . close = function ( disconnect ) { var _a , _b ; if ( this . _sk ) { var isConnected = this . isConnected ; this . _sk . close ( ) ; this . _sk . onclose = null ; this . _sk . onerror = null ; this . _sk . onmessage = null ; this . _sk . onopen = null ; this . _sk = null ; if ( isConnected ) { ( ( _a = this . _eventHandler ) === null || _a === void 0 ? void 0 : _a . onSocketClosed ) && ( ( _b = this . _eventHandler ) === null || _b === void 0 ? void 0 : _b . onSocketClosed ( disconnect ) ) ; } } } ; return WSocket ; } ( ) ) ; var NetNode = class ( function ( ) { function NetNode ( ) { /**
             * 当前重连次数
             */ this . _curReconnectCount = 0 ; /**
             * 请求id
             * 会自增
             */ this . _reqId = 1 ; } Object . defineProperty ( NetNode . prototype , "socket" , { get : function ( ) { return this . _socket ; } , enumerable : false , configurable : true } ) ; Object . defineProperty ( NetNode . prototype , "netEventHandler" , { get : function ( ) { return this . _netEventHandler ; } , enumerable : false , configurable : true } ) ; Object . defineProperty ( NetNode . prototype , "protoHandler" , { get : function ( ) { return this . _protoHandler ; } , enumerable : false , configurable : true } ) ; Object . defineProperty ( NetNode . prototype , "socketEventHandler" , { /**
             * 获取socket事件处理器
             */ get : function ( ) { if ( ! this . _socketEventHandler ) { this . _socketEventHandler = { onSocketClosed : this . _onSocketClosed . bind ( this ) , onSocketConnected : this . _onSocketConnected . bind ( this ) , onSocketError : this . _onSocketError . bind ( this ) , onSocketMsg : this . _onSocketMsg . bind ( this ) } ; } return this . _socketEventHandler ; } , enumerable : false , configurable : true } ) ; NetNode . prototype . init = function ( config ) { if ( this . _inited ) return ; this . _protoHandler = config && config . protoHandler ? config . protoHandler : new DefaultProtoHandler ( ) ; this . _socket = config && config . socket ? config . socket : new WSocket ( ) ; this . _netEventHandler = config && config . netEventHandler ? config . netEventHandler : new DefaultNetEventHandler ( ) ; this . _pushHandlerMap = { } ; this . _oncePushHandlerMap = { } ; this . _reqCfgMap = { } ; var reConnectCfg = config && config . reConnectCfg ; if ( ! reConnectCfg ) { this . _reConnectCfg = { reconnectCount : 4 , connectTimeout : 60000 } ; } else { this . _reConnectCfg = reConnectCfg ; if ( isNaN ( reConnectCfg . reconnectCount ) ) { this . _reConnectCfg . reconnectCount = 4 ; } if ( isNaN ( reConnectCfg . connectTimeout ) ) { this . _reConnectCfg . connectTimeout = 60000 ; } } this . _gapThreashold = config && ! isNaN ( config . heartbeatGapThreashold ) ? config . heartbeatGapThreashold : 100 ; this . _useCrypto = config && config . useCrypto ; this . _inited = true ; this . _socket . setEventHandler ( this . socketEventHandler ) ; this . _pkgTypeHandlers = { } ; this . _pkgTypeHandlers [ exports . PackageType . HANDSHAKE ] = this . _onHandshake . bind ( this ) ; this . _pkgTypeHandlers [ exports . PackageType . HEARTBEAT ] = this . _heartbeat . bind ( this ) ; this . _pkgTypeHandlers [ exports . PackageType . DATA ] = this . _onData . bind ( this ) ; this . _pkgTypeHandlers [ exports . PackageType . KICK ] = this . _onKick . bind ( this ) ; } ; NetNode . prototype . connect = function ( option , connectEnd ) { var socket = this . _socket ; var socketInCloseState = socket && ( socket . state === exports . SocketState . CLOSING || socket . state === exports . SocketState . CLOSED ) ; if ( this . _inited && socketInCloseState ) { if ( typeof option === "string" ) { option = { url : option , connectEnd : connectEnd } ; } if ( connectEnd ) { option . connectEnd = connectEnd ; } this . _connectOpt = option ; this . _socket . connect ( option ) ; var netEventHandler = this . _netEventHandler ; netEventHandler . onStartConnenct && netEventHandler . onStartConnenct ( option ) ; } else { console . error ( "is not inited" + ( socket ? " , socket state" + socket . state : "" ) ) ; } } ; NetNode . prototype . disConnect = function ( ) { this . _socket . close ( true ) ; if ( this . _heartbeatTimeId ) { clearTimeout ( this . _heartbeatTimeId ) ; this . _heartbeatTimeId = undefined ; } if ( this . _heartbeatTimeoutId ) { clearTimeout ( this . _heartbeatTimeoutId ) ; this . _heartbeatTimeoutId = undefined ; } } ; NetNode . prototype . reConnect = function ( ) { var _this = this ; if ( ! this . _inited || ! this . _socket ) { return ; } if ( this . _curReconnectCount > this . _reConnectCfg . reconnectCount ) { this . _stopReconnect ( false ) ; return ; } if ( ! this . _isReconnecting ) { var netEventHandler_1 = this . _netEventHandler ; netEventHandler_1 . onStartReconnect && netEventHandler_1 . onStartReconnect ( this . _reConnectCfg , this . _connectOpt ) ; } this . _isReconnecting = true ; this . connect ( this . _connectOpt ) ; this . _curReconnectCount ++ ; var netEventHandler = this . _netEventHandler ; netEventHandler . onReconnecting && netEventHandler . onReconnecting ( this . _curReconnectCount , this . _reConnectCfg , this . _connectOpt ) ; this . _reconnectTimerId = setTimeout ( function ( ) { _this . reConnect ( ) ; } , this . _reConnectCfg . connectTimeout ) ; } ; NetNode . prototype . request = function ( protoKey , data , resHandler , arg ) { if ( ! this . _isSocketReady ( ) ) return ; var reqId = this . _reqId ; var protoHandler = this . _protoHandler ; var encodePkg = protoHandler . encodeMsg ( { key : protoKey , reqId : reqId , data : data } , this . _useCrypto ) ; if ( encodePkg ) { var reqCfg = { reqId : reqId , protoKey : protoHandler . protoKey2Key ( protoKey ) , data : data , resHandler : resHandler } ; if ( arg ) reqCfg = Object . assign ( reqCfg , arg ) ; this . _reqCfgMap [ reqId ] = reqCfg ; this . _reqId ++ ; this . _netEventHandler . onStartRequest && this . _netEventHandler . onStartRequest ( reqCfg , this . _connectOpt ) ; this . send ( encodePkg ) ; } } ; NetNode . prototype . notify = function ( protoKey , data ) { if ( ! this . _isSocketReady ( ) ) return ; var encodePkg = this . _protoHandler . encodeMsg ( { key : protoKey , data : data } , this . _useCrypto ) ; this . send ( encodePkg ) ; } ; NetNode . prototype . send = function ( netData ) { this . _socket . send ( netData ) ; } ; NetNode . prototype . onPush = function ( protoKey , handler ) { var key = this . _protoHandler . protoKey2Key ( protoKey ) ; if ( ! this . _pushHandlerMap [ key ] ) { this . _pushHandlerMap [ key ] = [ handler ] ; } else { this . _pushHandlerMap [ key ] . push ( handler ) ; } } ; NetNode . prototype . oncePush = function ( protoKey , handler ) { var key = this . _protoHandler . protoKey2Key ( protoKey ) ; if ( ! this . _oncePushHandlerMap [ key ] ) { this . _oncePushHandlerMap [ key ] = [ handler ] ; } else { this . _oncePushHandlerMap [ key ] . push ( handler ) ; } } ; NetNode . prototype . offPush = function ( protoKey , callbackHandler , context , onceOnly ) { var key = this . _protoHandler . protoKey2Key ( protoKey ) ; var handlers ; if ( onceOnly ) { handlers = this . _oncePushHandlerMap [ key ] ; } else { handlers = this . _pushHandlerMap [ key ] ; } if ( handlers ) { var handler = void 0 ; var isEqual = void 0 ; for ( var i = handlers . length - 1 ; i > - 1 ; i -- ) { handler = handlers [ i ] ; isEqual = false ; if ( typeof handler === "function" && handler === callbackHandler ) { isEqual = true ; } else if ( typeof handler === "object" && handler . method === callbackHandler && ( ! context || context === handler . context ) ) { isEqual = true ; } if ( isEqual ) { if ( i !== handlers . length ) { handlers [ i ] = handlers [ handlers . length - 1 ] ; handlers [ handlers . length - 1 ] = handler ; } handlers . pop ( ) ; } } } } ; NetNode . prototype . offPushAll = function ( protoKey ) { if ( protoKey ) { var key = this . _protoHandler . protoKey2Key ( protoKey ) ; delete this . _pushHandlerMap [ key ] ; delete this . _oncePushHandlerMap [ key ] ; } else { this . _pushHandlerMap = { } ; this . _oncePushHandlerMap = { } ; } } ; param dpkg NetNode . prototype . _onHandshake = function ( dpkg ) { if ( dpkg . errorMsg ) { return ; } this . _handshakeInit ( dpkg ) ; var ackPkg = this . _protoHandler . encodePkg ( { type : exports . PackageType . HANDSHAKE_ACK } ) ; this . send ( ackPkg ) ; var connectOpt = this . _connectOpt ; var handshakeRes = this . _protoHandler . handShakeRes ; connectOpt . connectEnd && connectOpt . connectEnd ( handshakeRes ) ; this . _netEventHandler . onConnectEnd && this . _netEventHandler . onConnectEnd ( connectOpt , handshakeRes ) ; } ; param dpkg NetNode . prototype . _handshakeInit = function ( dpkg ) { var heartbeatCfg = this . protoHandler . heartbeatConfig ; this . _heartbeatConfig = heartbeatCfg ; } ; param dpkg NetNode . prototype . _heartbeat = function ( dpkg ) { var _this = this ; var heartbeatCfg = this . _heartbeatConfig ; var protoHandler = this . _protoHandler ; if ( ! heartbeatCfg || ! heartbeatCfg . heartbeatInterval ) { return ; } if ( this . _heartbeatTimeoutId ) { return ; } this . _heartbeatTimeId = setTimeout ( function ( ) { _this . _heartbeatTimeId = undefined ; var heartbeatPkg = protoHandler . encodePkg ( { type : exports . PackageType . HEARTBEAT } , _this . _useCrypto ) ; _this . send ( heartbeatPkg ) ; _this . _nextHeartbeatTimeoutTime = Date . now ( ) + heartbeatCfg . heartbeatTimeout ; _this . _heartbeatTimeoutId = setTimeout ( _this . _heartbeatTimeoutCb . bind ( _this ) , heartbeatCfg . heartbeatTimeout ) ; } , heartbeatCfg . heartbeatInterval ) ; } ; /**
         * 心跳超时处理
         */ NetNode . prototype . _heartbeatTimeoutCb = function ( ) { var gap = this . _nextHeartbeatTimeoutTime - Date . now ( ) ; if ( gap > this . _reConnectCfg ) { this . _heartbeatTimeoutId = setTimeout ( this . _heartbeatTimeoutCb . bind ( this ) , gap ) ; } else { console . error ( "server heartbeat timeout" ) ; this . disConnect ( ) ; } } ; param dpkg NetNode . prototype . _onData = function ( dpkg ) { if ( dpkg . errorMsg ) { return ; } var reqCfg ; if ( ! isNaN ( dpkg . reqId ) && dpkg . reqId > 0 ) { var reqId = dpkg . reqId ; reqCfg = this . _reqCfgMap [ reqId ] ; if ( ! reqCfg ) return ; reqCfg . decodePkg = dpkg ; this . _runHandler ( reqCfg . resHandler , dpkg ) ; } else { var pushKey = dpkg . key ; var handlers = this . _pushHandlerMap [ pushKey ] ; var onceHandlers = this . _oncePushHandlerMap [ pushKey ] ; if ( ! handlers ) { handlers = onceHandlers ; } else if ( onceHandlers ) { handlers = handlers . concat ( onceHandlers ) ; } delete this . _oncePushHandlerMap [ pushKey ] ; if ( handlers ) { for ( var i = 0 ; i < handlers . length ; i ++ ) { this . _runHandler ( handlers [ i ] , dpkg ) ; } } } var netEventHandler = this . _netEventHandler ; netEventHandler . onData && netEventHandler . onData ( dpkg , this . _connectOpt , reqCfg ) ; } ; param dpkg NetNode . prototype . _onKick = function ( dpkg ) { this . _netEventHandler . onKick && this . _netEventHandler . onKick ( dpkg , this . _connectOpt ) ; } ; /**
         * socket状态是否准备好
         */ NetNode . prototype . _isSocketReady = function ( ) { var socket = this . _socket ; var socketIsReady = socket && ( socket . state === exports . SocketState . CONNECTING || socket . state === exports . SocketState . OPEN ) ; if ( this . _inited && socketIsReady ) { return true ; } else { console . error ( "" + ( this . _inited ? socketIsReady ? "socket is ready" : "socket is null or unready" : "netNode is unInited" ) ) ; return false ; } } ; param event NetNode . prototype . _onSocketConnected = function ( event ) { if ( this . _isReconnecting ) { this . _stopReconnect ( ) ; } else { var handler = this . _netEventHandler ; var connectOpt = this . _connectOpt ; var protoHandler = this . _protoHandler ; if ( protoHandler && connectOpt . handShakeReq ) { var handShakeNetData = protoHandler . encodePkg ( { type : exports . PackageType . HANDSHAKE , data : connectOpt . handShakeReq } ) ; this . send ( handShakeNetData ) ; } else { connectOpt . connectEnd && connectOpt . connectEnd ( ) ; handler . onConnectEnd && handler . onConnectEnd ( connectOpt ) ; } } } ; param event NetNode . prototype . _onSocketError = function ( event ) { var eventHandler = this . _netEventHandler ; eventHandler . onError && eventHandler . onError ( event , this . _connectOpt ) ; } ; param event NetNode . prototype . _onSocketMsg = function ( event ) { var depackage = this . _protoHandler . decodePkg ( event . data ) ; var netEventHandler = this . _netEventHandler ; var pkgTypeHandler = this . _pkgTypeHandlers [ depackage . type ] ; if ( pkgTypeHandler ) { pkgTypeHandler ( depackage ) ; } else { console . error ( "There is no handler of this type:" + depackage . type ) ; } if ( depackage . errorMsg ) { netEventHandler . onCustomError && netEventHandler . onCustomError ( depackage , this . _connectOpt ) ; } if ( this . _nextHeartbeatTimeoutTime ) { this . _nextHeartbeatTimeoutTime = Date . now ( ) + this . _heartbeatConfig . heartbeatTimeout ; } } ; param event NetNode . prototype . _onSocketClosed = function ( event ) { var netEventHandler = this . _netEventHandler ; if ( this . _isReconnecting ) { clearTimeout ( this . _reconnectTimerId ) ; this . reConnect ( ) ; } else { netEventHandler . onClosed && netEventHandler . onClosed ( event , this . _connectOpt ) ; } } ; param handler param depackage NetNode . prototype . _runHandler = function ( handler , depackage ) { if ( typeof handler === "function" ) { handler ( depackage ) ; } else if ( typeof handler === "object" ) { handler . method && handler . method . apply ( handler . context , handler . args ? [ depackage ] . concat ( handler . args ) : [ depackage ] ) ; } } ; param isOk NetNode . prototype . _stopReconnect = function ( isOk ) { if ( isOk === void 0 ) { isOk = true ; } if ( this . _isReconnecting ) { this . _isReconnecting = false ; clearTimeout ( this . _reconnectTimerId ) ; this . _curReconnectCount = 0 ; var eventHandler = this . _netEventHandler ; eventHandler . onReconnectEnd && eventHandler . onReconnectEnd ( isOk , this . _reConnectCfg , this . _connectOpt ) ; } } ; return NetNode ; } ( ) ) ; var DefaultProtoHandler = class ( function ( ) { function DefaultProtoHandler ( ) { } Object . defineProperty ( DefaultProtoHandler . prototype , "handShakeRes" , { get : function ( ) { return undefined ; } , enumerable : false , configurable : true } ) ; Object . defineProperty ( DefaultProtoHandler . prototype , "heartbeatConfig" , { get : function ( ) { return this . _heartbeatCfg ; } , enumerable : false , configurable : true } ) ; DefaultProtoHandler . prototype . encodePkg = function ( pkg , useCrypto ) { return JSON . stringify ( pkg ) ; } ; DefaultProtoHandler . prototype . protoKey2Key = function ( protoKey ) { return protoKey ; } ; DefaultProtoHandler . prototype . encodeMsg = function ( msg , useCrypto ) { return JSON . stringify ( { type : exports . PackageType . DATA , data : msg } ) ; } ; DefaultProtoHandler . prototype . decodePkg = function ( data ) { var parsedData = JSON . parse ( data ) ; var pkgType = parsedData . type ; if ( parsedData . type === exports . PackageType . DATA ) { var msg = parsedData . data ; return { key : msg && msg . key , type : pkgType , data : msg . data , reqId : parsedData . data && parsedData . data . reqId } ; } else { if ( pkgType === exports . PackageType . HANDSHAKE ) { this . _heartbeatCfg = parsedData . data ; } return { type : pkgType , data : parsedData . data } ; } } ; return DefaultProtoHandler ; } ( ) ) ; exports . DefaultNetEventHandler = DefaultNetEventHandler ; exports . NetNode = NetNode ; exports . WSocket = WSocket ; Object . defineProperty ( exports , '__esModule' , { value : true } ) ; return exports ; } ( { } ) ) ; var globalTarget = window ? window : global ; globalTarget . enet ? Object . assign ( { } , globalTarget . enet ) : ( globalTarget . enet = enet ) ;