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

Commit 395db43

Browse files
committed
New version 2.2.0
1 parent 5ee0b59 commit 395db43

File tree

8 files changed

+21
-23
lines changed

8 files changed

+21
-23
lines changed

dist/browser/clusterws.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var ClusterWS = function() {
5555
return function() {
5656
function s(e) {
5757
return this.events = new n(), this.isAlive = !0, this.channels = {}, this.useBinary = !1,
58-
this.reconnectionAttempted = 0, e.url ? (this.options = {
58+
this.missedPing = 0, this.reconnectionAttempted = 0, e.url ? (this.options = {
5959
url: e.url,
6060
autoReconnect: e.autoReconnect || !1,
6161
autoReconnectOptions: e.autoReconnectOptions ? {
@@ -94,7 +94,7 @@ var ClusterWS = function() {
9494
return e.events.emit("error", t);
9595
}, this.websocket.onmessage = function(n) {
9696
var o = "string" != typeof n.data ? String.fromCharCode.apply(null, new Uint8Array(n.data)) : n.data;
97-
if ("#10" === o) return e.isAlive = !0;
97+
if ("#0" === o) return e.missedPing = 0, e.send("#1", null, "ping");
9898
try {
9999
o = JSON.parse(o), function(t, e) {
100100
var n = {
@@ -107,7 +107,7 @@ var ClusterWS = function() {
107107
s: {
108108
c: function() {
109109
t.useBinary = e["#"][2].binary, t.pingInterval = setInterval(function() {
110-
t.isAlive ? (t.send("#9", null, "ping"), t.isAlive = !1) : t.disconnect(4001, "No pong from the server");
110+
return t.missedPing++ > 2 && t.disconnect(4001, "Did not get pings");
111111
}, e["#"][2].ping), t.events.emit("connect");
112112
}
113113
}
@@ -118,7 +118,7 @@ var ClusterWS = function() {
118118
return t(e);
119119
}
120120
}, this.websocket.onclose = function(t) {
121-
if (clearInterval(e.pingInterval), e.events.emit("disconnect", t.code, t.reason),
121+
if (e.missedPing = 0, clearInterval(e.pingInterval), e.events.emit("disconnect", t.code, t.reason),
122122
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++,
123123
e.websocket = void 0, setTimeout(function() {
124124
return e.create();

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default class ClusterWS {
55
isAlive: boolean;
66
channels: CustomObject;
77
useBinary: boolean;
8+
missedPing: number;
89
pingInterval: any;
910
constructor(configurations: Configurations);
1011
on(event: 'error', listener: (err: any) => void): void;

dist/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function decode(t, e) {
5151
s: {
5252
c: function() {
5353
t.useBinary = e["#"][2].binary, t.pingInterval = setInterval(function() {
54-
t.isAlive ? (t.send("#9", null, "ping"), t.isAlive = !1) : t.disconnect(4001, "No pong from the server");
54+
return t.missedPing++ > 2 && t.disconnect(4001, "Did not get pings");
5555
}, e["#"][2].ping), t.events.emit("connect");
5656
}
5757
}
@@ -82,7 +82,7 @@ function encode(t, e, n) {
8282
var ClusterWS = function() {
8383
function t(t) {
8484
return this.events = new EventEmitter(), this.isAlive = !0, this.channels = {},
85-
this.useBinary = !1, this.reconnectionAttempted = 0, t.url ? (this.options = {
85+
this.useBinary = !1, this.missedPing = 0, this.reconnectionAttempted = 0, t.url ? (this.options = {
8686
url: t.url,
8787
autoReconnect: t.autoReconnect || !1,
8888
autoReconnectOptions: t.autoReconnectOptions ? {
@@ -118,14 +118,14 @@ var ClusterWS = function() {
118118
return t.events.emit("error", e);
119119
}, this.websocket.onmessage = function(e) {
120120
var n = "string" != typeof e.data ? String.fromCharCode.apply(null, new Uint8Array(e.data)) : e.data;
121-
if ("#10" === n) return t.isAlive = !0;
121+
if ("#0" === n) return t.missedPing = 0, t.send("#1", null, "ping");
122122
try {
123123
n = JSON.parse(n), decode(t, n);
124124
} catch (t) {
125125
return logError(t);
126126
}
127127
}, this.websocket.onclose = function(e) {
128-
if (clearInterval(t.pingInterval), t.events.emit("disconnect", e.code, e.reason),
128+
if (t.missedPing = 0, clearInterval(t.pingInterval), t.events.emit("disconnect", e.code, e.reason),
129129
t.options.autoReconnect && 1e3 !== e.code && (0 === t.options.autoReconnectOptions.attempts || t.reconnectionAttempted < t.options.autoReconnectOptions.attempts)) t.websocket.readyState === t.websocket.CLOSED ? (t.reconnectionAttempted++,
130130
t.websocket = void 0, setTimeout(function() {
131131
return t.create();

dist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "clusterws-client-js",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js.",
55
"main": "index.js",
66
"types": "index.d.ts",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "clusterws-client-js",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js.",
55
"main": "index.js",
66
"types": "index.d.ts",
@@ -31,4 +31,4 @@
3131
"ws client",
3232
"javascript"
3333
]
34-
}
34+
}

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class ClusterWS {
1111
public isAlive: boolean = true
1212
public channels: CustomObject = {}
1313
public useBinary: boolean = false
14+
public missedPing: number = 0
1415
public pingInterval: any
1516

1617
private options: Options
@@ -80,12 +81,14 @@ export default class ClusterWS {
8081
this.channels[keys[i]] && this.channels[keys[i]].subscribe()
8182
}
8283
this.websocket.onerror = (err: any): void => this.events.emit('error', err)
83-
this.websocket.onmessage = (message: any): boolean => {
84+
this.websocket.onmessage = (message: any): void => {
8485
let data: string = typeof message.data !== 'string' ?
8586
String.fromCharCode.apply(null, new Uint8Array(message.data)) : message.data
8687

87-
if (data === '#10')
88-
return this.isAlive = true
88+
if (data === '#0') {
89+
this.missedPing = 0
90+
return this.send('#1', null, 'ping')
91+
}
8992

9093
try {
9194
data = JSON.parse(data)
@@ -94,6 +97,7 @@ export default class ClusterWS {
9497
}
9598

9699
this.websocket.onclose = (event: CloseEvent): void => {
100+
this.missedPing = 0
97101
clearInterval(this.pingInterval)
98102
this.events.emit('disconnect', event.code, event.reason)
99103

src/modules/parser.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@ export function decode(socket: ClusterWS, message: any): void {
1515
s: {
1616
c: (): void => {
1717
socket.useBinary = message['#'][2].binary
18-
socket.pingInterval = setInterval(
19-
(): void => {
20-
if (socket.isAlive) {
21-
socket.send('#9', null, 'ping')
22-
socket.isAlive = false
23-
} else socket.disconnect(4001, 'No pong from the server')
24-
},
25-
message['#'][2].ping)
18+
socket.pingInterval = setInterval((): void => socket.missedPing++ > 2 && socket.disconnect(4001, 'Did not get pings'), message['#'][2].ping)
2619
socket.events.emit('connect')
2720
}
2821
}

0 commit comments

Comments
 (0)