@@ -28,12 +28,16 @@ var ClusterWS = function() {
2828 this . events [ e ] = n ;
2929 } , e . prototype . emit = function ( t ) {
3030 for ( var e , n = [ ] , o = 1 ; o < arguments . length ; o ++ ) n [ o - 1 ] = arguments [ o ] ;
31- this . events [ t ] && ( e = this . events [ t ] ) . call . apply ( e , [ null ] . concat ( n ) ) ;
31+ this . events [ t ] && ( e = this . events ) [ t ] . apply ( e , n ) ;
3232 } , e . prototype . removeAllEvents = function ( ) {
3333 this . events = { } ;
3434 } , e ;
3535 } ( ) ;
36- function o ( t , e , n ) {
36+ function o ( t ) {
37+ for ( var e = t . length , n = new Uint8Array ( e ) , o = 0 ; o < e ; o ++ ) n [ o ] = t . charCodeAt ( o ) ;
38+ return n . buffer ;
39+ }
40+ function s ( t , e , n ) {
3741 var o = {
3842 emit : {
3943 "#" : [ "e" , t , e ]
@@ -50,12 +54,12 @@ var ClusterWS = function() {
5054 }
5155 }
5256 } ;
53- return "ping" === n ? t : JSON . stringify ( "system" === n ? o [ n ] [ t ] : o [ n ] ) ;
57+ return JSON . stringify ( "system" === n ? o [ n ] [ t ] : o [ n ] ) ;
5458 }
5559 return function ( ) {
56- function s ( e ) {
60+ function i ( e ) {
5761 return this . events = new n ( ) , this . isAlive = ! 0 , this . channels = { } , this . useBinary = ! 1 ,
58- this . missedPing = 0 , this . reconnectionAttempted = 0 , e . url ? ( this . options = {
62+ this . reconnectionAttempted = 0 , e . url ? ( this . options = {
5963 url : e . url ,
6064 autoReconnect : e . autoReconnect || ! 1 ,
6165 autoReconnectOptions : e . autoReconnectOptions ? {
@@ -69,34 +73,43 @@ var ClusterWS = function() {
6973 }
7074 } , this . options . autoReconnectOptions . minInterval > this . options . autoReconnectOptions . maxInterval ? t ( "minInterval option can not be more than maxInterval option" ) : void this . create ( ) ) : t ( "Url must be provided and it must be a string" ) ;
7175 }
72- return s . prototype . on = function ( t , e ) {
76+ return i . prototype . send = function ( t , e , n ) {
77+ void 0 === n && ( n = "emit" ) , this . websocket . send ( this . useBinary ? o ( s ( t , e , n ) ) : s ( t , e , n ) ) ;
78+ } , i . prototype . on = function ( t , e ) {
7379 this . events . on ( t , e ) ;
74- } , s . prototype . send = function ( t , e , n ) {
75- void 0 === n && ( n = "emit" ) , this . websocket . send ( this . useBinary ? function ( t ) {
76- for ( var e = t . length , n = new Uint8Array ( e ) , o = 0 ; o < e ; o ++ ) n [ o ] = t . charCodeAt ( o ) ;
77- return n . buffer ;
78- } ( o ( t , e , n ) ) : o ( t , e , n ) ) ;
79- } , s . prototype . disconnect = function ( t , e ) {
80+ } , i . prototype . disconnect = function ( t , e ) {
8081 this . websocket . close ( t || 1e3 , e ) ;
81- } , s . prototype . subscribe = function ( t ) {
82+ } , i . prototype . getState = function ( ) {
83+ return this . websocket . readyState ;
84+ } , i . prototype . subscribe = function ( t ) {
8285 return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new e ( this , t ) ;
83- } , s . prototype . getChannelByName = function ( t ) {
86+ } , i . prototype . getChannelByName = function ( t ) {
8487 return this . channels [ t ] ;
85- } , s . prototype . getState = function ( ) {
86- return this . websocket . readyState ;
87- } , s . prototype . create = function ( ) {
88+ } , i . prototype . ping = function ( ) {
89+ var t = this ;
90+ clearTimeout ( this . pingTimeout ) , this . pingTimeout = setTimeout ( function ( ) {
91+ return t . disconnect ( 4001 , "Did not get pings" ) ;
92+ } , 2 * this . pingInterval ) ;
93+ } , i . prototype . create = function ( ) {
8894 var e = this , n = window . MozWebSocket || window . WebSocket ;
8995 this . websocket = new n ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
9096 this . websocket . onopen = function ( ) {
9197 e . reconnectionAttempted = 0 ;
9298 for ( var t = 0 , n = Object . keys ( e . channels ) , o = n . length ; t < o ; t ++ ) e . channels [ n [ t ] ] && e . channels [ n [ t ] ] . subscribe ( ) ;
93- } , this . websocket . onerror = function ( t ) {
94- return e . events . emit ( "error" , t ) ;
99+ } , this . websocket . onclose = function ( t ) {
100+ if ( clearTimeout ( e . pingTimeout ) , e . events . emit ( "disconnect" , t . code , t . reason ) ,
101+ e . options . autoReconnect && 1e3 !== t . code && ( 0 === e . options . autoReconnectOptions . attempts || e . reconnectionAttempted < e . options . autoReconnectOptions . attempts ) ) e . websocket . readyState === e . websocket . CLOSED ? ( e . reconnectionAttempted ++ ,
102+ e . websocket = void 0 , setTimeout ( function ( ) {
103+ return e . create ( ) ;
104+ } , Math . floor ( Math . random ( ) * ( e . options . autoReconnectOptions . maxInterval - e . options . autoReconnectOptions . minInterval + 1 ) ) ) ) : console . log ( "Some thing wrong with close event please contact developer" ) ; else {
105+ e . events . removeAllEvents ( ) ;
106+ for ( var n = 0 , o = Object . keys ( e ) , s = o . length ; n < s ; n ++ ) e [ o [ n ] ] = null ;
107+ }
95108 } , this . websocket . onmessage = function ( n ) {
96- var o = "string" != typeof n . data ? String . fromCharCode . apply ( null , new Uint8Array ( n . data ) ) : n . data ;
97- if ( "#0 " === o ) return e . missedPing = 0 , e . send ( "#1" , null , " ping" ) ;
109+ var s = "string" != typeof n . data ? String . fromCharCode . apply ( null , new Uint8Array ( n . data ) ) : n . data ;
110+ if ( "9 " === s ) return e . websocket . send ( o ( "A" ) ) , e . ping ( ) ;
98111 try {
99- o = JSON . parse ( o ) , function ( t , e ) {
112+ s = JSON . parse ( s ) , function ( t , e ) {
100113 var n = {
101114 e : function ( ) {
102115 return t . events . emit ( e [ "#" ] [ 1 ] , e [ "#" ] [ 2 ] ) ;
@@ -106,27 +119,18 @@ var ClusterWS = function() {
106119 } ,
107120 s : {
108121 c : function ( ) {
109- t . useBinary = e [ "#" ] [ 2 ] . binary , t . pingInterval = setInterval ( function ( ) {
110- return t . missedPing ++ > 2 && t . disconnect ( 4001 , "Did not get pings" ) ;
111- } , e [ "#" ] [ 2 ] . ping ) , t . events . emit ( "connect" ) ;
122+ t . useBinary = e [ "#" ] [ 2 ] . binary , t . pingInterval = e [ "#" ] [ 2 ] . ping , t . ping ( ) , t . events . emit ( "connect" ) ;
112123 }
113124 }
114125 } ;
115126 "s" === e [ "#" ] [ 0 ] ? n [ e [ "#" ] [ 0 ] ] [ e [ "#" ] [ 1 ] ] && n [ e [ "#" ] [ 0 ] ] [ e [ "#" ] [ 1 ] ] . call ( null ) : n [ e [ "#" ] [ 0 ] ] && n [ e [ "#" ] [ 0 ] ] . call ( null ) ;
116- } ( e , o ) ;
127+ } ( e , s ) ;
117128 } catch ( e ) {
118129 return t ( e ) ;
119130 }
120- } , this . websocket . onclose = function ( t ) {
121- if ( e . missedPing = 0 , clearInterval ( e . pingInterval ) , e . events . emit ( "disconnect" , t . code , t . reason ) ,
122- e . options . autoReconnect && 1e3 !== t . code && ( 0 === e . options . autoReconnectOptions . attempts || e . reconnectionAttempted < e . options . autoReconnectOptions . attempts ) ) e . websocket . readyState === e . websocket . CLOSED ? ( e . reconnectionAttempted ++ ,
123- e . websocket = void 0 , setTimeout ( function ( ) {
124- return e . create ( ) ;
125- } , Math . floor ( Math . random ( ) * ( e . options . autoReconnectOptions . maxInterval - e . options . autoReconnectOptions . minInterval + 1 ) ) ) ) : console . log ( "Some thing wrong with close event please contact developer" ) ; else {
126- e . events . removeAllEvents ( ) ;
127- for ( var n = 0 , o = Object . keys ( e ) , s = o . length ; n < s ; n ++ ) e [ o [ n ] ] = null ;
128- }
131+ } , this . websocket . onerror = function ( t ) {
132+ return e . events . emit ( "error" , t ) ;
129133 } ;
130- } , s ;
134+ } , i ;
131135 } ( ) ;
132136} ( ) ;
0 commit comments