@@ -3,36 +3,36 @@ var ClusterWS = function() {
33 function t ( t ) {
44 return console . log ( t ) ;
55 }
6- var e = function ( ) {
7- function e ( t , e ) {
8- this . socket = t , this . name = e , this . subscribe ( ) ;
6+ var n = function ( ) {
7+ function n ( t , n ) {
8+ this . socket = t , this . name = n , this . subscribe ( ) ;
99 }
10- return e . prototype . watch = function ( e ) {
11- return "[object Function]" !== { } . toString . call ( e ) ? t ( "Listener must be a function" ) : ( this . listener = e ,
10+ return n . prototype . watch = function ( n ) {
11+ return "[object Function]" !== { } . toString . call ( n ) ? t ( "Listener must be a function" ) : ( this . listener = n ,
1212 this ) ;
13- } , e . prototype . publish = function ( t ) {
13+ } , n . prototype . publish = function ( t ) {
1414 return this . socket . send ( this . name , t , "publish" ) , this ;
15- } , e . prototype . unsubscribe = function ( ) {
15+ } , n . prototype . unsubscribe = function ( ) {
1616 this . socket . send ( "unsubscribe" , this . name , "system" ) , this . socket . channels [ this . name ] = null ;
17- } , e . prototype . onMessage = function ( t ) {
17+ } , n . prototype . onMessage = function ( t ) {
1818 this . listener && this . listener . call ( null , t ) ;
19- } , e . prototype . subscribe = function ( ) {
19+ } , n . prototype . subscribe = function ( ) {
2020 this . socket . send ( "subscribe" , this . name , "system" ) ;
21- } , e ;
22- } ( ) , n = function ( ) {
23- function e ( ) {
21+ } , n ;
22+ } ( ) , e = function ( ) {
23+ function n ( ) {
2424 this . events = { } ;
2525 }
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 = 1 ; n < arguments . length ; n ++ ) e [ n - 1 ] = arguments [ n ] ;
31- this . events [ t ] && ( o = this . events [ t ] ) . call . apply ( o , [ null ] . concat ( e ) ) ;
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 = 1 ; e < arguments . length ; e ++ ) n [ e - 1 ] = arguments [ e ] ;
31+ this . events [ t ] && ( o = this . events [ t ] ) . call . apply ( o , [ null ] . concat ( n ) ) ;
3232 var o ;
33- } , e . prototype . removeAllEvents = function ( ) {
33+ } , n . prototype . removeAllEvents = function ( ) {
3434 this . events = { } ;
35- } , e ;
35+ } , n ;
3636 } ( ) , o = function ( ) {
3737 function t ( t ) {
3838 this . socket = t , this . inReconnectionState = ! 1 , this . reconnectionAttempted = 0 ,
@@ -53,104 +53,105 @@ var ClusterWS = function() {
5353 } , this . socket . options . reconnectionIntervalMin ) ) ;
5454 } , t ;
5555 } ( ) ;
56+ function i ( t , n , e ) {
57+ switch ( e ) {
58+ case "ping" :
59+ return t ;
60+
61+ case "emit" :
62+ return JSON . stringify ( {
63+ "#" : [ "e" , t , n ]
64+ } ) ;
65+
66+ case "publish" :
67+ return JSON . stringify ( {
68+ "#" : [ "p" , t , n ]
69+ } ) ;
70+
71+ case "system" :
72+ switch ( t ) {
73+ case "subscribe" :
74+ return JSON . stringify ( {
75+ "#" : [ "s" , "s" , n ]
76+ } ) ;
77+
78+ case "unsubscribe" :
79+ return JSON . stringify ( {
80+ "#" : [ "s" , "u" , n ]
81+ } ) ;
82+
83+ case "configuration" :
84+ return JSON . stringify ( {
85+ "#" : [ "s" , "c" , n ]
86+ } ) ;
87+ }
88+ }
89+ }
5690 return function ( ) {
57- function i ( e ) {
58- return this . channels = { } , this . events = new n ( ) , this . missedPing = 0 , this . useBinary = ! 1 ,
59- e . url ? ( this . options = {
60- url : e . url ,
61- autoReconnect : e . autoReconnect || ! 1 ,
62- reconnectionAttempts : e . reconnectionAttempts || 0 ,
63- reconnectionIntervalMin : e . reconnectionIntervalMin || 1e3 ,
64- reconnectionIntervalMax : e . reconnectionIntervalMax || 5e3
91+ function s ( n ) {
92+ return this . channels = { } , this . events = new e ( ) , this . missedPing = 0 , this . useBinary = ! 1 ,
93+ n . url ? ( this . options = {
94+ url : n . url ,
95+ autoReconnect : n . autoReconnect || ! 1 ,
96+ reconnectionAttempts : n . reconnectionAttempts || 0 ,
97+ reconnectionIntervalMin : n . reconnectionIntervalMin || 1e3 ,
98+ reconnectionIntervalMax : n . reconnectionIntervalMax || 5e3
6599 } , this . options . reconnectionIntervalMin > this . options . reconnectionIntervalMax ? t ( "reconnectionIntervalMin can not be more then reconnectionIntervalMax" ) : ( this . reconnection = new o ( this ) ,
66100 void this . create ( ) ) ) : t ( "Url must be provided and it must be string" ) ;
67101 }
68- return i . prototype . create = function ( ) {
69- var e = this , n = window . MozWebSocket || window . WebSocket ;
70- this . websocket = new n ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
102+ return s . prototype . create = function ( ) {
103+ var n = this , e = window . MozWebSocket || window . WebSocket ;
104+ this . websocket = new e ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
71105 this . websocket . onopen = function ( ) {
72- return e . reconnection . isConnected ( ) ;
106+ return n . reconnection . isConnected ( ) ;
73107 } , this . websocket . onerror = function ( t ) {
74- return e . events . emit ( "error" , t . message ) ;
75- } , this . websocket . onmessage = function ( n ) {
76- var o = "string" != typeof n . data ? String . fromCharCode . apply ( null , new Uint8Array ( n . data ) ) : n . data ;
77- if ( "#0" === o ) return e . missedPing = 0 , e . send ( "#1" , null , "ping" ) ;
108+ return n . events . emit ( "error" , t . message ) ;
109+ } , this . websocket . onmessage = function ( e ) {
110+ var o = "string" != typeof e . data ? String . fromCharCode . apply ( null , new Uint8Array ( e . data ) ) : e . data ;
111+ if ( "#0" === o ) return n . missedPing = 0 , n . send ( "#1" , null , "ping" ) ;
78112 try {
79113 o = JSON . parse ( o ) ;
80- } catch ( e ) {
81- return t ( e ) ;
114+ } catch ( n ) {
115+ return t ( n ) ;
82116 }
83- i . decode ( e , o ) ;
117+ ! function ( t , n ) {
118+ switch ( n [ "#" ] [ 0 ] ) {
119+ case "e" :
120+ return t . events . emit ( n [ "#" ] [ 1 ] , n [ "#" ] [ 2 ] ) ;
121+
122+ case "p" :
123+ t . channels [ n [ "#" ] [ 1 ] ] && t . channels [ n [ "#" ] [ 1 ] ] . onMessage ( n [ "#" ] [ 2 ] ) ;
124+
125+ case "s" :
126+ switch ( n [ "#" ] [ 1 ] ) {
127+ case "c" :
128+ t . pingInterval = setInterval ( function ( ) {
129+ return t . missedPing ++ > 2 && t . disconnect ( 4001 , "Did not get pings" ) ;
130+ } , n [ "#" ] [ 2 ] . ping ) , t . useBinary = n [ "#" ] [ 2 ] . binary , t . events . emit ( "connect" ) ;
131+ }
132+ }
133+ } ( n , o ) ;
84134 } , this . websocket . onclose = function ( t ) {
85- if ( e . missedPing = 0 , clearInterval ( e . pingInterval ) , e . events . emit ( "disconnect" , t . code , t . reason ) ,
86- e . options . autoReconnect && 1e3 !== t . code ) return e . reconnection . reconnect ( ) ;
87- e . events . removeAllEvents ( ) ;
88- for ( var n in e ) e [ n ] && ( e [ n ] = null ) ;
135+ if ( n . missedPing = 0 , clearInterval ( n . pingInterval ) , n . events . emit ( "disconnect" , t . code , t . reason ) ,
136+ n . options . autoReconnect && 1e3 !== t . code ) return n . reconnection . reconnect ( ) ;
137+ n . events . removeAllEvents ( ) ;
138+ for ( var e in n ) n [ e ] && ( n [ e ] = null ) ;
89139 } ;
90- } , i . prototype . on = function ( t , e ) {
91- this . events . on ( t , e ) ;
92- } , i . prototype . send = function ( t , e , n ) {
93- void 0 === n && ( n = "emit" ) , this . websocket . send ( this . useBinary ? i . buffer ( i . encode ( t , e , n ) ) : i . encode ( t , e , n ) ) ;
94- } , i . prototype . disconnect = function ( t , e ) {
95- this . websocket . close ( t || 1e3 , e ) ;
96- } , i . prototype . getState = function ( ) {
140+ } , s . prototype . on = function ( t , n ) {
141+ this . events . on ( t , n ) ;
142+ } , s . prototype . send = function ( t , n , e ) {
143+ void 0 === e && ( e = "emit" ) , this . websocket . send ( this . useBinary ? function ( t ) {
144+ for ( var n = t . length , e = new Uint8Array ( n ) , o = 0 ; o < n ; o ++ ) e [ o ] = t . charCodeAt ( o ) ;
145+ return e . buffer ;
146+ } ( i ( t , n , e ) ) : i ( t , n , e ) ) ;
147+ } , s . prototype . disconnect = function ( t , n ) {
148+ this . websocket . close ( t || 1e3 , n ) ;
149+ } , s . prototype . getState = function ( ) {
97150 return this . websocket . readyState ;
98- } , i . prototype . subscribe = function ( t ) {
99- return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new e ( this , t ) ;
100- } , i . prototype . getChannelByName = function ( t ) {
151+ } , s . prototype . subscribe = function ( t ) {
152+ return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new n ( this , t ) ;
153+ } , s . prototype . getChannelByName = function ( t ) {
101154 return this . channels [ t ] ;
102- } , i . buffer = function ( t ) {
103- for ( var e = t . length , n = new Uint8Array ( e ) , o = 0 ; o < e ; o ++ ) n [ o ] = t . charCodeAt ( o ) ;
104- return n . buffer ;
105- } , i . decode = function ( t , e ) {
106- switch ( e [ "#" ] [ 0 ] ) {
107- case "e" :
108- return t . events . emit ( e [ "#" ] [ 1 ] , e [ "#" ] [ 2 ] ) ;
109-
110- case "p" :
111- t . channels [ e [ "#" ] [ 1 ] ] && t . channels [ e [ "#" ] [ 1 ] ] . onMessage ( e [ "#" ] [ 2 ] ) ;
112-
113- case "s" :
114- switch ( e [ "#" ] [ 1 ] ) {
115- case "c" :
116- t . pingInterval = setInterval ( function ( ) {
117- return t . missedPing ++ > 2 && t . disconnect ( 4001 , "Did not get pings" ) ;
118- } , e [ "#" ] [ 2 ] . ping ) , t . useBinary = e [ "#" ] [ 2 ] . binary , t . events . emit ( "connect" ) ;
119- }
120- }
121- } , i . encode = function ( t , e , n ) {
122- switch ( n ) {
123- case "ping" :
124- return t ;
125-
126- case "emit" :
127- return JSON . stringify ( {
128- "#" : [ "e" , t , e ]
129- } ) ;
130-
131- case "publish" :
132- return JSON . stringify ( {
133- "#" : [ "p" , t , e ]
134- } ) ;
135-
136- case "system" :
137- switch ( t ) {
138- case "subscribe" :
139- return JSON . stringify ( {
140- "#" : [ "s" , "s" , e ]
141- } ) ;
142-
143- case "unsubscribe" :
144- return JSON . stringify ( {
145- "#" : [ "s" , "u" , e ]
146- } ) ;
147-
148- case "configuration" :
149- return JSON . stringify ( {
150- "#" : [ "s" , "c" , e ]
151- } ) ;
152- }
153- }
154- } , i ;
155+ } , s ;
155156 } ( ) ;
156157} ( ) ;
0 commit comments