Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Commit faa8fac

Browse files
committed
Improve reconnection, update to the server version
1 parent e90454e commit faa8fac

File tree

15 files changed

+434
-405
lines changed

15 files changed

+434
-405
lines changed

dist/browser/clusterws.js

Lines changed: 85 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,133 @@
11
var ClusterWS = function() {
22
"use strict";
3-
function n(n) {
4-
return console.log(n);
3+
function t(t) {
4+
return console.log(t);
55
}
6-
var t = function() {
7-
function t(n, t) {
8-
this.socket = n, this.name = t, this.subscribe();
6+
var n = function() {
7+
function n(t, n) {
8+
this.name = n, this.socket = t, this.subscribe();
99
}
10-
return t.prototype.watch = function(t) {
11-
return "[object Function]" !== {}.toString.call(t) ? n("Listener must be a function") : (this.listener = t,
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-
}, t.prototype.publish = function(n) {
14-
return this.socket.send(this.name, n, "publish"), this;
15-
}, t.prototype.unsubscribe = function() {
13+
}, n.prototype.publish = function(t) {
14+
return this.socket.send(this.name, t, "publish"), this;
15+
}, n.prototype.unsubscribe = function() {
1616
this.socket.send("unsubscribe", this.name, "system"), this.socket.channels[this.name] = null;
17-
}, t.prototype.onMessage = function(n) {
18-
this.listener && this.listener.call(null, n);
19-
}, t.prototype.subscribe = function() {
17+
}, n.prototype.onMessage = function(t) {
18+
this.listener && this.listener.call(null, t);
19+
}, n.prototype.subscribe = function() {
2020
this.socket.send("subscribe", this.name, "system");
21-
}, t;
21+
}, n;
2222
}(), e = function() {
23-
function t() {
23+
function n() {
2424
this.events = {};
2525
}
26-
return t.prototype.on = function(t, e) {
27-
if ("[object Function]" !== {}.toString.call(e)) return n("Listener must be a function");
28-
this.events[t] = e;
29-
}, t.prototype.emit = function(n) {
30-
for (var t, e = [], o = 1; o < arguments.length; o++) e[o - 1] = arguments[o];
31-
this.events[n] && (t = this.events[n]).call.apply(t, [ null ].concat(e));
32-
}, t.prototype.removeAllEvents = function() {
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() {
3333
this.events = {};
34-
}, t;
34+
}, n;
3535
}();
36-
function o(n, t, e) {
36+
function o(t, n, e) {
3737
var o = {
3838
emit: {
39-
"#": [ "e", n, t ]
39+
"#": [ "e", t, n ]
4040
},
4141
publish: {
42-
"#": [ "p", n, t ]
42+
"#": [ "p", t, n ]
4343
},
4444
system: {
4545
subscribe: {
46-
"#": [ "s", "s", t ]
46+
"#": [ "s", "s", n ]
4747
},
4848
unsubscribe: {
49-
"#": [ "s", "u", t ]
49+
"#": [ "s", "u", n ]
5050
}
5151
}
5252
};
53-
return "ping" === e ? n : JSON.stringify("system" === e ? o[e][n] : o[e]);
53+
return "ping" === e ? t : JSON.stringify("system" === e ? o[e][t] : o[e]);
5454
}
5555
return function() {
56-
function i(t) {
56+
function s(n) {
5757
return this.events = new e(), this.channels = {}, this.useBinary = !1, this.missedPing = 0,
58-
this.inReconnection = !1, this.reconnectionAttempted = 0, t.url ? (this.options = {
59-
url: t.url,
60-
autoReconnect: t.autoReconnect || !1,
61-
reconnectionAttempts: t.reconnectionAttempts || 0,
62-
reconnectionIntervalMin: t.reconnectionIntervalMin || 1e3,
63-
reconnectionIntervalMax: t.reconnectionIntervalMax || 5e3
64-
}, this.options.reconnectionIntervalMin > this.options.reconnectionIntervalMax ? n("reconnectionIntervalMin can not be more then reconnectionIntervalMax") : void this.create()) : n("Url must be provided and it must be string");
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
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");
6571
}
66-
return i.prototype.create = function() {
67-
var t = this, e = window.MozWebSocket || window.WebSocket;
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() {
88+
var n = this, e = window.MozWebSocket || window.WebSocket;
6889
this.websocket = new e(this.options.url), this.websocket.binaryType = "arraybuffer",
6990
this.websocket.onopen = function() {
70-
for (var n in t.reconnectionAttempted = 0, t.channels) t.channels[n] && t.channels[n].subscribe();
71-
}, this.websocket.onerror = function(n) {
72-
return t.events.emit("error", n);
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();
93+
}, this.websocket.onerror = function(t) {
94+
return n.events.emit("error", t);
7395
}, this.websocket.onmessage = function(e) {
7496
var o = "string" != typeof e.data ? String.fromCharCode.apply(null, new Uint8Array(e.data)) : e.data;
75-
if ("#0" === o) return t.missedPing = 0, t.send("#1", null, "ping");
97+
if ("#0" === o) return n.missedPing = 0, n.send("#1", null, "ping");
7698
try {
7799
o = JSON.parse(o);
78-
} catch (t) {
79-
return n(t);
100+
} catch (n) {
101+
return t(n);
80102
}
81-
!function(n, t) {
103+
!function(t, n) {
82104
var e = {
83105
e: function() {
84-
return n.events.emit(t["#"][1], t["#"][2]);
106+
return t.events.emit(n["#"][1], n["#"][2]);
85107
},
86108
p: function() {
87-
return n.channels[t["#"][1]] && n.channels[t["#"][1]].onMessage(t["#"][2]);
109+
return t.channels[n["#"][1]] && t.channels[n["#"][1]].onMessage(n["#"][2]);
88110
},
89111
s: {
90112
c: function() {
91-
n.pingInterval = setInterval(function() {
92-
return n.missedPing++ > 2 && n.disconnect(4001, "Did not get pings");
93-
}, t["#"][2].ping), n.useBinary = t["#"][2].binary, n.events.emit("connect");
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");
94116
}
95117
}
96118
};
97-
"s" === t["#"][0] ? e[t["#"][0]][t["#"][1]] && e[t["#"][0]][t["#"][1]].call(null) : e[t["#"][0]] && e[t["#"][0]].call(null);
98-
}(t, o);
99-
}, this.websocket.onclose = function(n) {
100-
if (t.missedPing = 0, clearInterval(t.pingInterval), t.events.emit("disconnect", n.code, n.reason),
101-
t.options.autoReconnect && 1e3 !== n.code && (0 === t.options.reconnectionAttempts || t.reconnectionAttempted < t.options.reconnectionAttempts)) t.websocket.readyState === t.websocket.CLOSED && (t.reconnectionAttempted++,
102-
t.websocket = null, setTimeout(function() {
103-
return t.create();
104-
}, Math.floor(Math.random() * (t.options.reconnectionIntervalMax - t.options.reconnectionIntervalMin + 1)))); else for (var e in t.events.removeAllEvents(),
105-
t) t[e] && (t[e] = null);
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);
121+
}, 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;
129+
}
106130
};
107-
}, i.prototype.on = function(n, t) {
108-
this.events.on(n, t);
109-
}, i.prototype.send = function(n, t, e) {
110-
void 0 === e && (e = "emit"), this.websocket.send(this.useBinary ? function(n) {
111-
for (var t = n.length, e = new Uint8Array(t), o = 0; o < t; o++) e[o] = n.charCodeAt(o);
112-
return e.buffer;
113-
}(o(n, t, e)) : o(n, t, e));
114-
}, i.prototype.disconnect = function(n, t) {
115-
this.websocket.close(n || 1e3, t);
116-
}, i.prototype.subscribe = function(n) {
117-
return this.channels[n] ? this.channels[n] : this.channels[n] = new t(this, n);
118-
}, i.prototype.getChannelByName = function(n) {
119-
return this.channels[n];
120-
}, i.prototype.getState = function() {
121-
return this.websocket.readyState;
122-
}, i;
131+
}, s;
123132
}();
124133
}();

dist/browser/clusterws.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)