@@ -3,7 +3,11 @@ var ClusterWS = function() {
3
3
function t ( t ) {
4
4
return console . log ( t ) ;
5
5
}
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 ( ) {
7
11
function e ( t , e ) {
8
12
this . name = e , this . socket = t , this . subscribe ( ) ;
9
13
}
@@ -19,7 +23,7 @@ var ClusterWS = function() {
19
23
} , e . prototype . subscribe = function ( ) {
20
24
this . socket . send ( "subscribe" , this . name , "system" ) ;
21
25
} , e ;
22
- } ( ) , n = function ( ) {
26
+ } ( ) , o = function ( ) {
23
27
function e ( ) {
24
28
this . events = { } ;
25
29
}
@@ -28,12 +32,12 @@ var ClusterWS = function() {
28
32
this . events [ e ] = n ;
29
33
} , e . prototype . emit = function ( t ) {
30
34
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 ) ) ;
35
+ this . events [ t ] && ( e = this . events ) [ t ] . apply ( e , n ) ;
32
36
} , e . prototype . removeAllEvents = function ( ) {
33
37
this . events = { } ;
34
38
} , e ;
35
39
} ( ) ;
36
- function o ( t , e , n ) {
40
+ function s ( t , e , n ) {
37
41
var o = {
38
42
emit : {
39
43
"#" : [ "e" , t , e ]
@@ -50,83 +54,90 @@ var ClusterWS = function() {
50
54
}
51
55
}
52
56
} ;
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 ] ) ;
54
58
}
59
+ var i = window . MozWebSocket || window . WebSocket ;
55
60
return function ( ) {
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
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
65
70
} : {
66
71
attempts : 0 ,
67
72
minInterval : 1e3 ,
68
73
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 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" ) ;
71
77
}
72
- return s . prototype . on = function ( t , e ) {
78
+ return r . prototype . on = function ( t , e ) {
73
79
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
+ } , r . prototype . getState = function ( ) {
81
+ return this . websocket . readyState ;
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 ) {
80
88
this . websocket . close ( t || 1e3 , e ) ;
81
- } , s . prototype . subscribe = function ( t ) {
82
- return this . channels [ t ] ? this . channels [ t ] : this . channels [ t ] = new e ( this , t ) ;
83
- } , s . prototype . getChannelByName = function ( t ) {
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 ) {
84
95
return this . channels [ t ] ;
85
- } , s . prototype . getState = function ( ) {
86
- return this . websocket . readyState ;
87
- } , s . prototype . create = function ( ) {
88
- var e = this , n = window . MozWebSocket || window . WebSocket ;
89
- 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" ,
90
99
this . websocket . onopen = function ( ) {
91
100
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 ( ) ;
93
- } , this . websocket . onerror = function ( t ) {
94
- return e . events . emit ( "error" , t ) ;
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 ( ) ;
102
+ } , this . websocket . onclose = function ( t ) {
103
+ if ( clearTimeout ( e . pingTimeout ) , e . events . emit ( "disconnect" , t . code , t . reason ) ,
104
+ 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 ++ ,
105
+ e . websocket = void 0 , setTimeout ( function ( ) {
106
+ return e . create ( ) ;
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 {
108
+ e . events . removeAllEvents ( ) ;
109
+ for ( var n = 0 , o = Object . keys ( e ) , s = o . length ; n < s ; n ++ ) e [ o [ n ] ] = null ;
110
+ }
95
111
} , 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" ) ;
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 ( ) ;
98
114
try {
99
- o = JSON . parse ( o ) , function ( t , e ) {
100
- var n = {
115
+ ! function ( t , e ) {
116
+ var n = t . options . encodeDecodeEngine ? t . options . encodeDecodeEngine . decode ( e [ "#" ] [ 2 ] ) : e [ "#" ] [ 2 ] , o = {
101
117
e : function ( ) {
102
- return t . events . emit ( e [ "#" ] [ 1 ] , e [ "#" ] [ 2 ] ) ;
118
+ return t . events . emit ( e [ "#" ] [ 1 ] , n ) ;
103
119
} ,
104
120
p : function ( ) {
105
- return t . channels [ e [ "#" ] [ 1 ] ] && t . channels [ e [ "#" ] [ 1 ] ] . onMessage ( e [ "#" ] [ 2 ] ) ;
121
+ return t . channels [ e [ "#" ] [ 1 ] ] && t . channels [ e [ "#" ] [ 1 ] ] . onMessage ( n ) ;
106
122
} ,
107
123
s : {
108
124
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" ) ;
125
+ t . useBinary = n . binary , t . resetPing ( n . ping ) , t . events . emit ( "connect" ) ;
112
126
}
113
127
}
114
128
} ;
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 ) ;
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 ) ) ) ;
117
135
} catch ( e ) {
118
136
return t ( e ) ;
119
137
}
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
- }
138
+ } , this . websocket . onerror = function ( t ) {
139
+ return e . events . emit ( "error" , t ) ;
129
140
} ;
130
- } , s ;
141
+ } , r ;
131
142
} ( ) ;
132
143
} ( ) ;
0 commit comments