@@ -3,7 +3,11 @@ var ClusterWS = function() {
33 function t ( t ) {
44 return console . log ( t ) ;
55 }
6- var e = function ( ) {
6+ function e ( t ) {
7+ for ( var e = t . length , n = new Uint8Array ( e ) , o = 0 ; o < e ; o ++ ) n [ o ] = t . charCodeAt ( o ) ;
8+ return n . buffer ;
9+ }
10+ var n = function ( ) {
711 function e ( t , e ) {
812 this . name = e , this . socket = t , this . subscribe ( ) ;
913 }
@@ -19,7 +23,7 @@ var ClusterWS = function() {
1923 } , e . prototype . subscribe = function ( ) {
2024 this . socket . send ( "subscribe" , this . name , "system" ) ;
2125 } , e ;
22- } ( ) , n = function ( ) {
26+ } ( ) , o = function ( ) {
2327 function e ( ) {
2428 this . events = { } ;
2529 }
@@ -33,10 +37,6 @@ var ClusterWS = function() {
3337 this . events = { } ;
3438 } , e ;
3539 } ( ) ;
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- }
4040 function s ( t , e , n ) {
4141 var o = {
4242 emit : {
@@ -56,81 +56,88 @@ var ClusterWS = function() {
5656 } ;
5757 return JSON . stringify ( "system" === n ? o [ n ] [ t ] : o [ n ] ) ;
5858 }
59+ var i = window . MozWebSocket || window . WebSocket ;
5960 return function ( ) {
60- function i ( e ) {
61- return this . events = new n ( ) , this . isAlive = ! 0 , this . channels = { } , this . useBinary = ! 1 ,
62- this . reconnectionAttempted = 0 , e . url ? ( this . options = {
63- url : e . url ,
64- autoReconnect : e . autoReconnect || ! 1 ,
65- autoReconnectOptions : e . autoReconnectOptions ? {
66- attempts : e . autoReconnectOptions . attempts || 0 ,
67- minInterval : e . autoReconnectOptions . minInterval || 1e3 ,
68- maxInterval : e . autoReconnectOptions . maxInterval || 5e3
61+ function r ( n ) {
62+ return this . events = new o ( ) , this . channels = { } , this . pong = e ( "A" ) , this . reconnectionAttempted = 0 ,
63+ this . options = {
64+ url : n . url ,
65+ autoReconnect : n . autoReconnect || ! 1 ,
66+ autoReconnectOptions : n . autoReconnectOptions ? {
67+ attempts : n . autoReconnectOptions . attempts || 0 ,
68+ minInterval : n . autoReconnectOptions . minInterval || 1e3 ,
69+ maxInterval : n . autoReconnectOptions . maxInterval || 5e3
6970 } : {
7071 attempts : 0 ,
7172 minInterval : 1e3 ,
7273 maxInterval : 5e3
73- }
74- } , 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" ) ;
74+ } ,
75+ encodeDecodeEngine : n . encodeDecodeEngine || ! 1
76+ } , this . options . url ? 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" ) ;
7577 }
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 ) {
78+ return r . prototype . on = function ( t , e ) {
7979 this . events . on ( t , e ) ;
80- } , i . prototype . disconnect = function ( t , e ) {
81- this . websocket . close ( t || 1e3 , e ) ;
82- } , i . prototype . getState = function ( ) {
80+ } , r . prototype . getState = function ( ) {
8381 return this . websocket . readyState ;
84- } , i . prototype . subscribe = function ( t ) {
85- return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new e ( this , t ) ;
86- } , i . prototype . getChannelByName = function ( t ) {
82+ } , r . prototype . resetPing = function ( t ) {
83+ var e = this ;
84+ t && ( this . pingInterval = t ) , clearTimeout ( this . pingTimeout ) , this . pingTimeout = setTimeout ( function ( ) {
85+ return e . disconnect ( 4001 , "Did not get pings" ) ;
86+ } , 2 * this . pingInterval + 100 ) ;
87+ } , r . prototype . disconnect = function ( t , e ) {
88+ this . websocket . close ( t || 1e3 , e ) ;
89+ } , r . prototype . send = function ( t , n , o ) {
90+ void 0 === o && ( o = "emit" ) , n = this . options . encodeDecodeEngine ? this . options . encodeDecodeEngine . encode ( n ) : n ,
91+ this . websocket . send ( this . useBinary ? e ( s ( t , n , o ) ) : s ( t , n , o ) ) ;
92+ } , r . prototype . subscribe = function ( t ) {
93+ return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new n ( this , t ) ;
94+ } , r . prototype . getChannelByName = function ( t ) {
8795 return this . channels [ t ] ;
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 ( ) {
94- var e = this , n = window . MozWebSocket || window . WebSocket ;
95- this . websocket = new n ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
96+ } , r . prototype . create = function ( ) {
97+ var e = this ;
98+ this . websocket = new i ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
9699 this . websocket . onopen = function ( ) {
97100 e . reconnectionAttempted = 0 ;
98- for ( var t = 0 , n = Object . keys ( e . channels ) , o = n . length ; t < o ; t ++ ) e . channels [ n [ t ] ] && e . channels [ n [ t ] ] . subscribe ( ) ;
101+ for ( var t = 0 , n = Object . keys ( e . channels ) , o = n . length ; t < o ; t ++ ) e . channels . hasOwnProperty ( n [ t ] ) && e . channels [ n [ t ] ] . subscribe ( ) ;
99102 } , this . websocket . onclose = function ( t ) {
100103 if ( clearTimeout ( e . pingTimeout ) , e . events . emit ( "disconnect" , t . code , t . reason ) ,
101104 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 ++ ,
102105 e . websocket = void 0 , setTimeout ( function ( ) {
103106 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 {
107+ } , Math . floor ( Math . random ( ) * ( e . options . autoReconnectOptions . maxInterval - e . options . autoReconnectOptions . minInterval + 1 ) ) ) ) : console . log ( "Some thing went wrong with close event please contact developer" ) ; else {
105108 e . events . removeAllEvents ( ) ;
106109 for ( var n = 0 , o = Object . keys ( e ) , s = o . length ; n < s ; n ++ ) e [ o [ n ] ] = null ;
107110 }
108111 } , this . websocket . onmessage = function ( n ) {
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 ( ) ;
112+ var o = "string" != typeof n . data ? new Uint8Array ( n . data ) : n . data ;
113+ if ( 57 === o [ 0 ] ) return e . websocket . send ( e . pong ) , e . resetPing ( ) ;
111114 try {
112- s = JSON . parse ( s ) , function ( t , e ) {
113- var n = {
115+ ! function ( t , e ) {
116+ var n = t . options . encodeDecodeEngine ? t . options . encodeDecodeEngine . decode ( e [ "#" ] [ 2 ] ) : e [ "#" ] [ 2 ] , o = {
114117 e : function ( ) {
115- return t . events . emit ( e [ "#" ] [ 1 ] , e [ "#" ] [ 2 ] ) ;
118+ return t . events . emit ( e [ "#" ] [ 1 ] , n ) ;
116119 } ,
117120 p : function ( ) {
118- return t . channels [ e [ "#" ] [ 1 ] ] && t . channels [ e [ "#" ] [ 1 ] ] . onMessage ( e [ "#" ] [ 2 ] ) ;
121+ return t . channels [ e [ "#" ] [ 1 ] ] && t . channels [ e [ "#" ] [ 1 ] ] . onMessage ( n ) ;
119122 } ,
120123 s : {
121124 c : function ( ) {
122- t . useBinary = e [ "#" ] [ 2 ] . binary , t . pingInterval = e [ "#" ] [ 2 ] . ping , t . ping ( ) , t . events . emit ( "connect" ) ;
125+ t . useBinary = n . binary , t . resetPing ( n . ping ) , t . events . emit ( "connect" ) ;
123126 }
124127 }
125128 } ;
126- "s" === e [ "#" ] [ 0 ] ? n [ e [ "#" ] [ 0 ] ] [ e [ "#" ] [ 1 ] ] && n [ e [ "#" ] [ 0 ] ] [ e [ "#" ] [ 1 ] ] . call ( null ) : n [ e [ "#" ] [ 0 ] ] && n [ e [ "#" ] [ 0 ] ] . call ( null ) ;
127- } ( e , s ) ;
129+ "s" === e [ "#" ] [ 0 ] ? o [ e [ "#" ] [ 0 ] ] [ e [ "#" ] [ 1 ] ] && o [ e [ "#" ] [ 0 ] ] [ e [ "#" ] [ 1 ] ] ( ) : o [ e [ "#" ] [ 0 ] ] && o [ e [ "#" ] [ 0 ] ] ( ) ;
130+ } ( e , JSON . parse ( function ( t ) {
131+ for ( var e = "" , n = 65535 , o = t . length , s = 0 ; s < o ; s += n ) s + n > o && ( n = o - s ) ,
132+ e += String . fromCharCode . apply ( null , t . subarray ( s , s + n ) ) ;
133+ return e ;
134+ } ( o ) ) ) ;
128135 } catch ( e ) {
129136 return t ( e ) ;
130137 }
131138 } , this . websocket . onerror = function ( t ) {
132139 return e . events . emit ( "error" , t ) ;
133140 } ;
134- } , i ;
141+ } , r ;
135142 } ( ) ;
136143} ( ) ;
0 commit comments