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

Commit 173d9ab

Browse files
committed
Update ping, clean up code
1 parent 19af4a9 commit 173d9ab

File tree

12 files changed

+130
-123
lines changed

12 files changed

+130
-123
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-9B1F82.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-9B1F82.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-9B1F82.svg?style=for-the-badge" alt="GitHub license"/></a>
1212
</p>
1313

1414
<p align="center">

dist/browser/clusterws.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ var ClusterWS = function() {
2828
this.events[e] = n;
2929
}, e.prototype.emit = function(t) {
3030
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));
31+
this.events[t] && (e = this.events)[t].apply(e, n);
3232
}, e.prototype.removeAllEvents = function() {
3333
this.events = {};
3434
}, e;
3535
}();
36-
function o(t, e, n) {
36+
function o(t) {
37+
for (var e = t.length, n = new Uint8Array(e), o = 0; o < e; o++) n[o] = t.charCodeAt(o);
38+
return n.buffer;
39+
}
40+
function s(t, e, n) {
3741
var o = {
3842
emit: {
3943
"#": [ "e", t, e ]
@@ -50,12 +54,12 @@ 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
}
5559
return function() {
56-
function s(e) {
60+
function i(e) {
5761
return this.events = new n(), this.isAlive = !0, this.channels = {}, this.useBinary = !1,
58-
this.missedPing = 0, this.reconnectionAttempted = 0, e.url ? (this.options = {
62+
this.reconnectionAttempted = 0, e.url ? (this.options = {
5963
url: e.url,
6064
autoReconnect: e.autoReconnect || !1,
6165
autoReconnectOptions: e.autoReconnectOptions ? {
@@ -69,34 +73,43 @@ var ClusterWS = function() {
6973
}
7074
}, 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");
7175
}
72-
return s.prototype.on = function(t, e) {
76+
return i.prototype.send = function(t, e, n) {
77+
void 0 === n && (n = "emit"), this.websocket.send(this.useBinary ? o(s(t, e, n)) : s(t, e, n));
78+
}, i.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+
}, i.prototype.disconnect = function(t, e) {
8081
this.websocket.close(t || 1e3, e);
81-
}, s.prototype.subscribe = function(t) {
82+
}, i.prototype.getState = function() {
83+
return this.websocket.readyState;
84+
}, i.prototype.subscribe = function(t) {
8285
return this.channels[t] ? this.channels[t] : this.channels[t] = new e(this, t);
83-
}, s.prototype.getChannelByName = function(t) {
86+
}, i.prototype.getChannelByName = function(t) {
8487
return this.channels[t];
85-
}, s.prototype.getState = function() {
86-
return this.websocket.readyState;
87-
}, s.prototype.create = function() {
88+
}, i.prototype.ping = function() {
89+
var t = this;
90+
clearTimeout(this.pingTimeout), this.pingTimeout = setTimeout(function() {
91+
return t.disconnect(4001, "Did not get pings");
92+
}, 2 * this.pingInterval);
93+
}, i.prototype.create = function() {
8894
var e = this, n = window.MozWebSocket || window.WebSocket;
8995
this.websocket = new n(this.options.url), this.websocket.binaryType = "arraybuffer",
9096
this.websocket.onopen = function() {
9197
e.reconnectionAttempted = 0;
9298
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);
99+
}, this.websocket.onclose = function(t) {
100+
if (clearTimeout(e.pingTimeout), e.events.emit("disconnect", t.code, t.reason),
101+
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++,
102+
e.websocket = void 0, setTimeout(function() {
103+
return e.create();
104+
}, 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 {
105+
e.events.removeAllEvents();
106+
for (var n = 0, o = Object.keys(e), s = o.length; n < s; n++) e[o[n]] = null;
107+
}
95108
}, 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");
109+
var s = "string" != typeof n.data ? String.fromCharCode.apply(null, new Uint8Array(n.data)) : n.data;
110+
if ("9" === s) return e.websocket.send(o("A")), e.ping();
98111
try {
99-
o = JSON.parse(o), function(t, e) {
112+
s = JSON.parse(s), function(t, e) {
100113
var n = {
101114
e: function() {
102115
return t.events.emit(e["#"][1], e["#"][2]);
@@ -106,27 +119,18 @@ var ClusterWS = function() {
106119
},
107120
s: {
108121
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");
122+
t.useBinary = e["#"][2].binary, t.pingInterval = e["#"][2].ping, t.ping(), t.events.emit("connect");
112123
}
113124
}
114125
};
115126
"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);
127+
}(e, s);
117128
} catch (e) {
118129
return t(e);
119130
}
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-
}
131+
}, this.websocket.onerror = function(t) {
132+
return e.events.emit("error", t);
129133
};
130-
}, s;
134+
}, i;
131135
}();
132136
}();

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.

dist/index.d.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@ export default class ClusterWS {
55
isAlive: boolean;
66
channels: CustomObject;
77
useBinary: boolean;
8-
missedPing: number;
8+
pingTimeout: any;
99
pingInterval: any;
1010
constructor(configurations: Configurations);
11+
send(event: string, message: Message, eventType?: string): void;
1112
on(event: 'error', listener: (err: any) => void): void;
1213
on(event: 'connect', listener: () => void): void;
1314
on(event: 'disconnect', listener: (code?: number, reason?: string) => void): void;
1415
on(event: string, listener: Listener): void;
15-
send(event: string, message: Message, eventType?: string): void;
16-
disconnect(code?: number, reason?: any): void;
16+
disconnect(code?: number, reason?: string): void;
17+
getState(): number;
1718
subscribe(channelName: string): Channel;
1819
getChannelByName(channelName: string): Channel;
19-
getState(): number;
20+
ping(): void;
2021
}
2122

2223
export class Channel {
2324
name: string;
2425
constructor(socket: ClusterWS, name: string);
2526
watch(listener: Listener): Channel;
26-
publish(data: any): Channel;
27+
publish(data: Message): Channel;
2728
unsubscribe(): void;
28-
onMessage(data: any): void;
29+
onMessage(data: Message): void;
2930
subscribe(): void;
3031
}
3132

@@ -36,8 +37,8 @@ export class EventEmitter {
3637
}
3738

3839
export function buffer(str: string): ByteString;
39-
export function decode(socket: ClusterWS, message: any): void;
40-
export function encode(event: string, data: any, eventType: string): string;
40+
export function decode(socket: ClusterWS, message: Message): void;
41+
export function encode(event: string, data: Message, eventType: string): string;
4142

4243
export function logError<T>(data: T): any;
4344

0 commit comments

Comments
 (0)