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

Commit f63a0f9

Browse files
authored
Merge pull request #38 from ClusterWS/next
Next
2 parents 36b743b + f0f2502 commit f63a0f9

File tree

14 files changed

+245
-193
lines changed

14 files changed

+245
-193
lines changed

dist/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
<h6 align="center">Build Scalable Node.js WebSocket Applications</h6>
33

44
<p align="center">
5-
<img src="https://cdn.rawgit.com/goriunov/159120ca6a883d8d4e75543ec395d361/raw/146220360173a2428fceb44e7fc9b2cda8a17832/clusterws.svg" width="450">
5+
<img src="https://cdn.rawgit.com/goriunov/159120ca6a883d8d4e75543ec395d361/raw/d22028ecc726d7d3cc30a2a85cc7cc454b0afada/clusterws.svg" width="450">
66
</p>
77

88
<p align="center">
9-
<a href="https://github.com/ClusterWS/ClusterWS-Client-JS/blob/master/LICENSE"><img src="https://img.shields.io/github/license/ClusterWS/ClusterWS-Client-JS.svg?style=for-the-badge" alt="GitHub license"/></a>
10-
<a href="https://www.npmjs.com/package/clusterws-client-js"><img src="https://img.shields.io/badge/npm-2.2.1-blue.svg?style=for-the-badge" alt="NPM Version" /></a>
11-
<a href="https://github.com/ClusterWS/ClusterWS-Client-JS/graphs/commit-activity"><img src="https://img.shields.io/badge/Maintain-Yes-green.svg?style=for-the-badge" alt="Maintain" /></a>
9+
<a href="https://www.npmjs.com/package/clusterws-client-js"><img src="https://img.shields.io/badge/npm-3.0.0-AE1E80.svg?style=for-the-badge" alt="NPM Version" /></a>
10+
<a href="https://github.com/ClusterWS/ClusterWS-Client-JS/graphs/commit-activity"><img src="https://img.shields.io/badge/Maintain-Yes-AE1E80.svg?style=for-the-badge" alt="Maintain" /></a>
11+
<a href="https://github.com/ClusterWS/ClusterWS-Client-JS/blob/master/LICENSE"><img src="https://img.shields.io/badge/LICENSE-MIT-AE1E80.svg?style=for-the-badge" alt="GitHub license"/></a>
1212
</p>
1313

1414
<p align="center">

dist/browser/clusterws.js

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ var ClusterWS = function() {
33
function t(t) {
44
return console.log(t);
55
}
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() {
711
function e(t, e) {
812
this.name = e, this.socket = t, this.subscribe();
913
}
@@ -19,7 +23,7 @@ var ClusterWS = function() {
1923
}, e.prototype.subscribe = function() {
2024
this.socket.send("subscribe", this.name, "system");
2125
}, e;
22-
}(), n = function() {
26+
}(), o = function() {
2327
function e() {
2428
this.events = {};
2529
}
@@ -28,12 +32,12 @@ var ClusterWS = function() {
2832
this.events[e] = n;
2933
}, e.prototype.emit = function(t) {
3034
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);
3236
}, e.prototype.removeAllEvents = function() {
3337
this.events = {};
3438
}, e;
3539
}();
36-
function o(t, e, n) {
40+
function s(t, e, n) {
3741
var o = {
3842
emit: {
3943
"#": [ "e", t, e ]
@@ -50,83 +54,90 @@ var ClusterWS = function() {
5054
}
5155
}
5256
};
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]);
5458
}
59+
var i = window.MozWebSocket || window.WebSocket;
5560
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
6570
} : {
6671
attempts: 0,
6772
minInterval: 1e3,
6873
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");
7177
}
72-
return s.prototype.on = function(t, e) {
78+
return r.prototype.on = function(t, e) {
7379
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) {
8088
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) {
8495
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",
9099
this.websocket.onopen = function() {
91100
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+
}
95111
}, 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();
98114
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 = {
101117
e: function() {
102-
return t.events.emit(e["#"][1], e["#"][2]);
118+
return t.events.emit(e["#"][1], n);
103119
},
104120
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);
106122
},
107123
s: {
108124
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");
112126
}
113127
}
114128
};
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)));
117135
} catch (e) {
118136
return t(e);
119137
}
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);
129140
};
130-
}, s;
141+
}, r;
131142
}();
132143
}();

0 commit comments

Comments
 (0)