@@ -5,7 +5,7 @@ var ClusterWS = function() {
55 }
66 var n = function ( ) {
77 function n ( t , n ) {
8- this . socket = t , this . name = n , this . subscribe ( ) ;
8+ this . name = n , this . socket = t , this . subscribe ( ) ;
99 }
1010 return n . prototype . watch = function ( n ) {
1111 return "[object Function]" !== { } . toString . call ( n ) ? t ( "Listener must be a function" ) : ( this . listener = n ,
@@ -32,79 +32,66 @@ var ClusterWS = function() {
3232 } , n . prototype . removeAllEvents = function ( ) {
3333 this . events = { } ;
3434 } , n ;
35- } ( ) , o = function ( ) {
36- function t ( t ) {
37- this . socket = t , this . inReconnectionState = ! 1 , this . reconnectionAttempted = 0 ,
38- this . autoReconnect = this . socket . options . autoReconnect ;
39- }
40- return t . prototype . isConnected = function ( ) {
41- clearTimeout ( this . timer ) , clearInterval ( this . interval ) , this . inReconnectionState = ! 1 ,
42- this . reconnectionAttempted = 0 ;
43- for ( var t in this . socket . channels ) this . socket . channels [ t ] && this . socket . channels [ t ] . subscribe ( ) ;
44- } , t . prototype . reconnect = function ( ) {
45- var t = this ;
46- this . inReconnectionState || ( this . inReconnectionState = ! 0 , this . interval = setInterval ( function ( ) {
47- t . socket . getState ( ) === t . socket . websocket . CLOSED && ( t . reconnectionAttempted ++ ,
48- 0 !== t . socket . options . reconnectionAttempts && t . reconnectionAttempted >= t . socket . options . reconnectionAttempts && ( clearInterval ( t . interval ) ,
49- t . autoReconnect = ! 1 , t . inReconnectionState = ! 1 ) , clearTimeout ( t . timer ) , t . timer = setTimeout ( function ( ) {
50- return t . socket . create ( ) ;
51- } , Math . floor ( Math . random ( ) * ( t . socket . options . reconnectionIntervalMax - t . socket . options . reconnectionIntervalMin + 1 ) ) ) ) ;
52- } , this . socket . options . reconnectionIntervalMin ) ) ;
53- } , t ;
5435 } ( ) ;
55- function i ( t , n , e ) {
56- switch ( e ) {
57- case "ping" :
58- return t ;
59-
60- case "emit" :
61- return JSON . stringify ( {
36+ function o ( t , n , e ) {
37+ var o = {
38+ emit : {
6239 "#" : [ "e" , t , n ]
63- } ) ;
64-
65- case "publish" :
66- return JSON . stringify ( {
40+ } ,
41+ publish : {
6742 "#" : [ "p" , t , n ]
68- } ) ;
69-
70- case "system" :
71- switch ( t ) {
72- case "subscribe" :
73- return JSON . stringify ( {
43+ } ,
44+ system : {
45+ subscribe : {
7446 "#" : [ "s" , "s" , n ]
75- } ) ;
76-
77- case "unsubscribe" :
78- return JSON . stringify ( {
47+ } ,
48+ unsubscribe : {
7949 "#" : [ "s" , "u" , n ]
80- } ) ;
81-
82- case "configuration" :
83- return JSON . stringify ( {
84- "#" : [ "s" , "c" , n ]
85- } ) ;
50+ }
8651 }
87- }
52+ } ;
53+ return "ping" === e ? t : JSON . stringify ( "system" === e ? o [ e ] [ t ] : o [ e ] ) ;
8854 }
8955 return function ( ) {
9056 function s ( n ) {
91- return this . channels = { } , this . events = new e ( ) , this . missedPing = 0 , this . useBinary = ! 1 ,
92- n . url ? ( this . options = {
57+ return this . events = new e ( ) , this . channels = { } , this . useBinary = ! 1 , this . missedPing = 0 ,
58+ this . reconnectionAttempted = 0 , n . url ? ( this . options = {
9359 url : n . url ,
9460 autoReconnect : n . autoReconnect || ! 1 ,
95- reconnectionAttempts : n . reconnectionAttempts || 0 ,
96- reconnectionIntervalMin : n . reconnectionIntervalMin || 1e3 ,
97- reconnectionIntervalMax : n . reconnectionIntervalMax || 5e3
98- } , this . options . reconnectionIntervalMin > this . options . reconnectionIntervalMax ? t ( "reconnectionIntervalMin can not be more then reconnectionIntervalMax" ) : ( this . reconnection = new o ( this ) ,
99- void this . create ( ) ) ) : t ( "Url must be provided and it must be string" ) ;
61+ autoReconnectOptions : n . autoReconnectOptions ? {
62+ attempts : n . autoReconnectOptions . attempts || 0 ,
63+ minInterval : n . autoReconnectOptions . minInterval || 1e3 ,
64+ maxInterval : n . autoReconnectOptions . maxInterval || 5e3
65+ } : {
66+ attempts : 0 ,
67+ minInterval : 1e3 ,
68+ maxInterval : 5e3
69+ }
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" ) ;
10071 }
101- return s . prototype . create = function ( ) {
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 ) ;
81+ } , s . prototype . subscribe = function ( t ) {
82+ return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new n ( this , t ) ;
83+ } , s . prototype . getChannelByName = function ( t ) {
84+ return this . channels [ t ] ;
85+ } , s . prototype . getState = function ( ) {
86+ return this . websocket . readyState ;
87+ } , s . prototype . create = function ( ) {
10288 var n = this , e = window . MozWebSocket || window . WebSocket ;
10389 this . websocket = new e ( this . options . url ) , this . websocket . binaryType = "arraybuffer" ,
10490 this . websocket . onopen = function ( ) {
105- return n . reconnection . isConnected ( ) ;
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 ( ) ;
10693 } , this . websocket . onerror = function ( t ) {
107- return n . events . emit ( "error" , t . message ) ;
94+ return n . events . emit ( "error" , t ) ;
10895 } , this . websocket . onmessage = function ( e ) {
10996 var o = "string" != typeof e . data ? String . fromCharCode . apply ( null , new Uint8Array ( e . data ) ) : e . data ;
11097 if ( "#0" === o ) return n . missedPing = 0 , n . send ( "#1" , null , "ping" ) ;
@@ -114,43 +101,33 @@ var ClusterWS = function() {
114101 return t ( n ) ;
115102 }
116103 ! function ( t , n ) {
117- switch ( n [ "#" ] [ 0 ] ) {
118- case "e" :
119- return t . events . emit ( n [ "#" ] [ 1 ] , n [ "#" ] [ 2 ] ) ;
120-
121- case "p" :
122- t . channels [ n [ "#" ] [ 1 ] ] && t . channels [ n [ "#" ] [ 1 ] ] . onMessage ( n [ "#" ] [ 2 ] ) ;
123-
124- case "s" :
125- switch ( n [ "#" ] [ 1 ] ) {
126- case "c" :
127- t . pingInterval = setInterval ( function ( ) {
128- return t . missedPing ++ > 2 && t . disconnect ( 4001 , "Did not get pings ") ;
129- } , n [ "#" ] [ 2 ] . ping ) , t . useBinary = n [ "#" ] [ 2 ] . binary , t . events . emit ( "connect" ) ;
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 ") ;
116+ }
130117 }
131- }
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 ) ;
132120 } ( n , o ) ;
133121 } , this . websocket . onclose = function ( t ) {
134122 if ( n . missedPing = 0 , clearInterval ( n . pingInterval ) , n . events . emit ( "disconnect" , t . code , t . reason ) ,
135- n . options . autoReconnect && 1e3 !== t . code ) return n . reconnection . reconnect ( ) ;
136- n . events . removeAllEvents ( ) ;
137- for ( var e in n ) n [ e ] && ( n [ e ] = null ) ;
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 ;
129+ }
138130 } ;
139- } , s . prototype . on = function ( t , n ) {
140- this . events . on ( t , n ) ;
141- } , s . prototype . send = function ( t , n , e ) {
142- void 0 === e && ( e = "emit" ) , this . websocket . send ( this . useBinary ? function ( t ) {
143- for ( var n = t . length , e = new Uint8Array ( n ) , o = 0 ; o < n ; o ++ ) e [ o ] = t . charCodeAt ( o ) ;
144- return e . buffer ;
145- } ( i ( t , n , e ) ) : i ( t , n , e ) ) ;
146- } , s . prototype . disconnect = function ( t , n ) {
147- this . websocket . close ( t || 1e3 , n ) ;
148- } , s . prototype . getState = function ( ) {
149- return this . websocket . readyState ;
150- } , s . prototype . subscribe = function ( t ) {
151- return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new n ( this , t ) ;
152- } , s . prototype . getChannelByName = function ( t ) {
153- return this . channels [ t ] ;
154131 } , s ;
155132 } ( ) ;
156133} ( ) ;
0 commit comments