@@ -3,129 +3,128 @@ var ClusterWS = function() {
33 function t ( t ) {
44 return console . log ( t ) ;
55 }
6- var n = function ( ) {
7- function n ( t , n ) {
8- this . name = n , this . socket = t , this . subscribe ( ) ;
6+ var e = function ( ) {
7+ function e ( t , e ) {
8+ this . name = e , this . socket = t , this . subscribe ( ) ;
99 }
10- return n . prototype . watch = function ( n ) {
11- return "[object Function]" !== { } . toString . call ( n ) ? t ( "Listener must be a function" ) : ( this . listener = n ,
10+ return e . prototype . watch = function ( e ) {
11+ return "[object Function]" !== { } . toString . call ( e ) ? t ( "Listener must be a function" ) : ( this . listener = e ,
1212 this ) ;
13- } , n . prototype . publish = function ( t ) {
13+ } , e . prototype . publish = function ( t ) {
1414 return this . socket . send ( this . name , t , "publish" ) , this ;
15- } , n . prototype . unsubscribe = function ( ) {
15+ } , e . prototype . unsubscribe = function ( ) {
1616 this . socket . send ( "unsubscribe" , this . name , "system" ) , this . socket . channels [ this . name ] = null ;
17- } , n . prototype . onMessage = function ( t ) {
17+ } , e . prototype . onMessage = function ( t ) {
1818 this . listener && this . listener . call ( null , t ) ;
19- } , n . prototype . subscribe = function ( ) {
19+ } , e . prototype . subscribe = function ( ) {
2020 this . socket . send ( "subscribe" , this . name , "system" ) ;
21- } , n ;
22- } ( ) , e = function ( ) {
23- function n ( ) {
21+ } , e ;
22+ } ( ) , n = function ( ) {
23+ function e ( ) {
2424 this . events = { } ;
2525 }
26- return n . prototype . on = function ( n , e ) {
27- if ( "[object Function]" !== { } . toString . call ( e ) ) return t ( "Listener must be a function" ) ;
28- this . events [ n ] = e ;
29- } , n . prototype . emit = function ( t ) {
30- for ( var n , e = [ ] , o = 1 ; o < arguments . length ; o ++ ) e [ o - 1 ] = arguments [ o ] ;
31- this . events [ t ] && ( n = this . events [ t ] ) . call . apply ( n , [ null ] . concat ( e ) ) ;
32- } , n . prototype . removeAllEvents = function ( ) {
26+ return e . prototype . on = function ( e , n ) {
27+ if ( "[object Function]" !== { } . toString . call ( n ) ) return t ( "Listener must be a function" ) ;
28+ this . events [ e ] = n ;
29+ } , e . prototype . emit = function ( t ) {
30+ 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 ) ) ;
32+ } , e . prototype . removeAllEvents = function ( ) {
3333 this . events = { } ;
34- } , n ;
34+ } , e ;
3535 } ( ) ;
36- function o ( t , n , e ) {
36+ function o ( t , e , n ) {
3737 var o = {
3838 emit : {
39- "#" : [ "e" , t , n ]
39+ "#" : [ "e" , t , e ]
4040 } ,
4141 publish : {
42- "#" : [ "p" , t , n ]
42+ "#" : [ "p" , t , e ]
4343 } ,
4444 system : {
4545 subscribe : {
46- "#" : [ "s" , "s" , n ]
46+ "#" : [ "s" , "s" , e ]
4747 } ,
4848 unsubscribe : {
49- "#" : [ "s" , "u" , n ]
49+ "#" : [ "s" , "u" , e ]
5050 }
5151 }
5252 } ;
53- return "ping" === e ? t : JSON . stringify ( "system" === e ? o [ e ] [ t ] : o [ e ] ) ;
53+ return "ping" === n ? t : JSON . stringify ( "system" === n ? o [ n ] [ t ] : o [ n ] ) ;
5454 }
5555 return function ( ) {
56- function s ( n ) {
57- return this . events = new e ( ) , this . channels = { } , this . useBinary = ! 1 , this . missedPing = 0 ,
58- this . reconnectionAttempted = 0 , n . url ? ( this . options = {
59- url : n . url ,
60- autoReconnect : n . autoReconnect || ! 1 ,
61- autoReconnectOptions : n . autoReconnectOptions ? {
62- attempts : n . autoReconnectOptions . attempts || 0 ,
63- minInterval : n . autoReconnectOptions . minInterval || 1e3 ,
64- maxInterval : n . autoReconnectOptions . maxInterval || 5e3
56+ function s ( e ) {
57+ return this . events = new n ( ) , this . isAlive = ! 0 , this . channels = { } , this . useBinary = ! 1 ,
58+ this . missedPing = 0 , this . reconnectionAttempted = 0 , e . url ? ( this . options = {
59+ url : e . url ,
60+ autoReconnect : e . autoReconnect || ! 1 ,
61+ autoReconnectOptions : e . autoReconnectOptions ? {
62+ attempts : e . autoReconnectOptions . attempts || 0 ,
63+ minInterval : e . autoReconnectOptions . minInterval || 1e3 ,
64+ maxInterval : e . autoReconnectOptions . maxInterval || 5e3
6565 } : {
6666 attempts : 0 ,
6767 minInterval : 1e3 ,
6868 maxInterval : 5e3
6969 }
70- } , 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 string" ) ;
70+ } , 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" ) ;
7171 }
72- return s . prototype . on = function ( t , n ) {
73- this . events . on ( t , n ) ;
74- } , s . prototype . send = function ( t , n , e ) {
75- void 0 === e && ( e = "emit" ) , this . websocket . send ( this . useBinary ? function ( t ) {
76- for ( var n = t . length , e = new Uint8Array ( n ) , o = 0 ; o < n ; o ++ ) e [ o ] = t . charCodeAt ( o ) ;
77- return e . buffer ;
78- } ( o ( t , n , e ) ) : o ( t , n , e ) ) ;
79- } , s . prototype . disconnect = function ( t , n ) {
80- this . websocket . close ( t || 1e3 , n ) ;
72+ return s . prototype . on = function ( t , e ) {
73+ 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+ this . websocket . close ( t || 1e3 , e ) ;
8181 } , s . prototype . subscribe = function ( t ) {
82- return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new n ( this , t ) ;
82+ return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new e ( this , t ) ;
8383 } , s . prototype . getChannelByName = function ( t ) {
8484 return this . channels [ t ] ;
8585 } , s . prototype . getState = function ( ) {
8686 return this . websocket . readyState ;
8787 } , s . prototype . create = function ( ) {
88- var n = this , e = window . MozWebSocket || window . WebSocket ;
89- this . websocket = new e ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
88+ var e = this , n = window . MozWebSocket || window . WebSocket ;
89+ this . websocket = new n ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
9090 this . websocket . onopen = function ( ) {
91- n . reconnectionAttempted = 0 ;
92- for ( var t = 0 , e = Object . keys ( n . channels ) , o = e . length ; t < o ; t ++ ) n . channels [ e [ t ] ] && n . channels [ e [ t ] ] . subscribe ( ) ;
91+ e . reconnectionAttempted = 0 ;
92+ for ( var t = 0 , n = Object . keys ( e . channels ) , o = n . length ; t < o ; t ++ ) e . channels [ n [ t ] ] && e . channels [ n [ t ] ] . subscribe ( ) ;
9393 } , this . websocket . onerror = function ( t ) {
94- return n . events . emit ( "error" , t ) ;
95- } , this . websocket . onmessage = function ( e ) {
96- var o = "string" != typeof e . data ? String . fromCharCode . apply ( null , new Uint8Array ( e . data ) ) : e . data ;
97- if ( "#0" === o ) return n . missedPing = 0 , n . send ( "#1" , null , "ping" ) ;
94+ return e . events . emit ( "error" , t ) ;
95+ } , 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" ) ;
9898 try {
99- o = JSON . parse ( o ) ;
100- } catch ( n ) {
101- return t ( n ) ;
102- }
103- ! function ( t , n ) {
104- var e = {
105- e : function ( ) {
106- return t . events . emit ( n [ "#" ] [ 1 ] , n [ "#" ] [ 2 ] ) ;
107- } ,
108- p : function ( ) {
109- return t . channels [ n [ "#" ] [ 1 ] ] && t . channels [ n [ "#" ] [ 1 ] ] . onMessage ( n [ "#" ] [ 2 ] ) ;
110- } ,
111- s : {
112- c : function ( ) {
113- t . pingInterval = setInterval ( function ( ) {
114- return t . missedPing ++ > 2 && t . disconnect ( 4001 , "Did not get pings" ) ;
115- } , n [ "#" ] [ 2 ] . ping ) , t . useBinary = n [ "#" ] [ 2 ] . binary , t . events . emit ( "connect" ) ;
99+ o = JSON . parse ( o ) , function ( t , e ) {
100+ var n = {
101+ e : function ( ) {
102+ return t . events . emit ( e [ "#" ] [ 1 ] , e [ "#" ] [ 2 ] ) ;
103+ } ,
104+ p : function ( ) {
105+ return t . channels [ e [ "#" ] [ 1 ] ] && t . channels [ e [ "#" ] [ 1 ] ] . onMessage ( e [ "#" ] [ 2 ] ) ;
106+ } ,
107+ s : {
108+ 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" ) ;
112+ }
116113 }
117- }
118- } ;
119- "s" === n [ "#" ] [ 0 ] ? e [ n [ "#" ] [ 0 ] ] [ n [ "#" ] [ 1 ] ] && e [ n [ "#" ] [ 0 ] ] [ n [ "#" ] [ 1 ] ] . call ( null ) : e [ n [ "#" ] [ 0 ] ] && e [ n [ "#" ] [ 0 ] ] . call ( null ) ;
120- } ( n , o ) ;
114+ } ;
115+ "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 ) ;
117+ } catch ( e ) {
118+ return t ( e ) ;
119+ }
121120 } , this . websocket . onclose = function ( t ) {
122- if ( n . missedPing = 0 , clearInterval ( n . pingInterval ) , n . events . emit ( "disconnect" , t . code , t . reason ) ,
123- n . options . autoReconnect && 1e3 !== t . code && ( 0 === n . options . autoReconnectOptions . attempts || n . reconnectionAttempted < n . options . autoReconnectOptions . attempts ) ) n . websocket . readyState === n . websocket . CLOSED ? ( n . reconnectionAttempted ++ ,
124- n . websocket = void 0 , setTimeout ( function ( ) {
125- return n . create ( ) ;
126- } , Math . floor ( Math . random ( ) * ( n . options . autoReconnectOptions . maxInterval - n . options . autoReconnectOptions . minInterval + 1 ) ) ) ) : console . log ( "Some thing wrong with close event please contact developer" ) ; else {
127- n . events . removeAllEvents ( ) ;
128- for ( var e = 0 , o = Object . keys ( n ) , s = o . length ; e < s ; e ++ ) n [ o [ e ] ] = null ;
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 ;
129128 }
130129 } ;
131130 } , s ;
0 commit comments