From 5ee0b5908da711dcd596b3c2f22c47f2b5f06a6a Mon Sep 17 00:00:00 2001 From: Dmitrii Goriunov Date: Thu, 29 Mar 2018 23:58:57 +1300 Subject: [PATCH 1/3] Clean up event, implement application level heartbit --- dist/README.md | 4 +- dist/browser/clusterws.js | 163 +++++++++++++++++----------------- dist/browser/clusterws.min.js | 2 +- dist/index.d.ts | 2 +- dist/index.js | 19 ++-- docs/README.md | 2 +- src/index.ts | 18 ++-- src/modules/parser.ts | 9 +- 8 files changed, 111 insertions(+), 108 deletions(-) diff --git a/dist/README.md b/dist/README.md index d70f26a..4980e17 100644 --- a/dist/README.md +++ b/dist/README.md @@ -2,11 +2,11 @@
Build Scalable Node.js WebSocket Applications

- +

- GitHub license + GitHub license NPM Version Maintain

diff --git a/dist/browser/clusterws.js b/dist/browser/clusterws.js index c1ab56c..3aa9845 100644 --- a/dist/browser/clusterws.js +++ b/dist/browser/clusterws.js @@ -3,129 +3,128 @@ var ClusterWS = function() { function t(t) { return console.log(t); } - var n = function() { - function n(t, n) { - this.name = n, this.socket = t, this.subscribe(); + var e = function() { + function e(t, e) { + this.name = e, this.socket = t, this.subscribe(); } - return n.prototype.watch = function(n) { - return "[object Function]" !== {}.toString.call(n) ? t("Listener must be a function") : (this.listener = n, + return e.prototype.watch = function(e) { + return "[object Function]" !== {}.toString.call(e) ? t("Listener must be a function") : (this.listener = e, this); - }, n.prototype.publish = function(t) { + }, e.prototype.publish = function(t) { return this.socket.send(this.name, t, "publish"), this; - }, n.prototype.unsubscribe = function() { + }, e.prototype.unsubscribe = function() { this.socket.send("unsubscribe", this.name, "system"), this.socket.channels[this.name] = null; - }, n.prototype.onMessage = function(t) { + }, e.prototype.onMessage = function(t) { this.listener && this.listener.call(null, t); - }, n.prototype.subscribe = function() { + }, e.prototype.subscribe = function() { this.socket.send("subscribe", this.name, "system"); - }, n; - }(), e = function() { - function n() { + }, e; + }(), n = function() { + function e() { this.events = {}; } - return n.prototype.on = function(n, e) { - if ("[object Function]" !== {}.toString.call(e)) return t("Listener must be a function"); - this.events[n] = e; - }, n.prototype.emit = function(t) { - for (var n, e = [], o = 1; o < arguments.length; o++) e[o - 1] = arguments[o]; - this.events[t] && (n = this.events[t]).call.apply(n, [ null ].concat(e)); - }, n.prototype.removeAllEvents = function() { + return e.prototype.on = function(e, n) { + if ("[object Function]" !== {}.toString.call(n)) return t("Listener must be a function"); + this.events[e] = n; + }, e.prototype.emit = function(t) { + for (var e, n = [], o = 1; o < arguments.length; o++) n[o - 1] = arguments[o]; + this.events[t] && (e = this.events[t]).call.apply(e, [ null ].concat(n)); + }, e.prototype.removeAllEvents = function() { this.events = {}; - }, n; + }, e; }(); - function o(t, n, e) { + function o(t, e, n) { var o = { emit: { - "#": [ "e", t, n ] + "#": [ "e", t, e ] }, publish: { - "#": [ "p", t, n ] + "#": [ "p", t, e ] }, system: { subscribe: { - "#": [ "s", "s", n ] + "#": [ "s", "s", e ] }, unsubscribe: { - "#": [ "s", "u", n ] + "#": [ "s", "u", e ] } } }; - return "ping" === e ? t : JSON.stringify("system" === e ? o[e][t] : o[e]); + return "ping" === n ? t : JSON.stringify("system" === n ? o[n][t] : o[n]); } return function() { - function s(n) { - return this.events = new e(), this.channels = {}, this.useBinary = !1, this.missedPing = 0, - this.reconnectionAttempted = 0, n.url ? (this.options = { - url: n.url, - autoReconnect: n.autoReconnect || !1, - autoReconnectOptions: n.autoReconnectOptions ? { - attempts: n.autoReconnectOptions.attempts || 0, - minInterval: n.autoReconnectOptions.minInterval || 1e3, - maxInterval: n.autoReconnectOptions.maxInterval || 5e3 + function s(e) { + return this.events = new n(), this.isAlive = !0, this.channels = {}, this.useBinary = !1, + this.reconnectionAttempted = 0, e.url ? (this.options = { + url: e.url, + autoReconnect: e.autoReconnect || !1, + autoReconnectOptions: e.autoReconnectOptions ? { + attempts: e.autoReconnectOptions.attempts || 0, + minInterval: e.autoReconnectOptions.minInterval || 1e3, + maxInterval: e.autoReconnectOptions.maxInterval || 5e3 } : { attempts: 0, minInterval: 1e3, maxInterval: 5e3 } - }, 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"); + }, 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"); } - return s.prototype.on = function(t, n) { - this.events.on(t, n); - }, s.prototype.send = function(t, n, e) { - void 0 === e && (e = "emit"), this.websocket.send(this.useBinary ? function(t) { - for (var n = t.length, e = new Uint8Array(n), o = 0; o < n; o++) e[o] = t.charCodeAt(o); - return e.buffer; - }(o(t, n, e)) : o(t, n, e)); - }, s.prototype.disconnect = function(t, n) { - this.websocket.close(t || 1e3, n); + return s.prototype.on = function(t, e) { + this.events.on(t, e); + }, s.prototype.send = function(t, e, n) { + void 0 === n && (n = "emit"), this.websocket.send(this.useBinary ? function(t) { + for (var e = t.length, n = new Uint8Array(e), o = 0; o < e; o++) n[o] = t.charCodeAt(o); + return n.buffer; + }(o(t, e, n)) : o(t, e, n)); + }, s.prototype.disconnect = function(t, e) { + this.websocket.close(t || 1e3, e); }, s.prototype.subscribe = function(t) { - return this.channels[t] ? this.channels[t] : this.channels[t] = new n(this, t); + return this.channels[t] ? this.channels[t] : this.channels[t] = new e(this, t); }, s.prototype.getChannelByName = function(t) { return this.channels[t]; }, s.prototype.getState = function() { return this.websocket.readyState; }, s.prototype.create = function() { - var n = this, e = window.MozWebSocket || window.WebSocket; - this.websocket = new e(this.options.url), this.websocket.binaryType = "arraybuffer", + var e = this, n = window.MozWebSocket || window.WebSocket; + this.websocket = new n(this.options.url), this.websocket.binaryType = "arraybuffer", this.websocket.onopen = function() { - n.reconnectionAttempted = 0; - for (var t = 0, e = Object.keys(n.channels), o = e.length; t < o; t++) n.channels[e[t]] && n.channels[e[t]].subscribe(); + e.reconnectionAttempted = 0; + for (var t = 0, n = Object.keys(e.channels), o = n.length; t < o; t++) e.channels[n[t]] && e.channels[n[t]].subscribe(); }, this.websocket.onerror = function(t) { - return n.events.emit("error", t); - }, this.websocket.onmessage = function(e) { - var o = "string" != typeof e.data ? String.fromCharCode.apply(null, new Uint8Array(e.data)) : e.data; - if ("#0" === o) return n.missedPing = 0, n.send("#1", null, "ping"); + return e.events.emit("error", t); + }, this.websocket.onmessage = function(n) { + var o = "string" != typeof n.data ? String.fromCharCode.apply(null, new Uint8Array(n.data)) : n.data; + if ("#10" === o) return e.isAlive = !0; try { - o = JSON.parse(o); - } catch (n) { - return t(n); - } - !function(t, n) { - var e = { - e: function() { - return t.events.emit(n["#"][1], n["#"][2]); - }, - p: function() { - return t.channels[n["#"][1]] && t.channels[n["#"][1]].onMessage(n["#"][2]); - }, - s: { - c: function() { - t.pingInterval = setInterval(function() { - return t.missedPing++ > 2 && t.disconnect(4001, "Did not get pings"); - }, n["#"][2].ping), t.useBinary = n["#"][2].binary, t.events.emit("connect"); + o = JSON.parse(o), function(t, e) { + var n = { + e: function() { + return t.events.emit(e["#"][1], e["#"][2]); + }, + p: function() { + return t.channels[e["#"][1]] && t.channels[e["#"][1]].onMessage(e["#"][2]); + }, + s: { + c: function() { + t.useBinary = e["#"][2].binary, t.pingInterval = setInterval(function() { + t.isAlive ? (t.send("#9", null, "ping"), t.isAlive = !1) : t.disconnect(4001, "No pong from the server"); + }, e["#"][2].ping), t.events.emit("connect"); + } } - } - }; - "s" === n["#"][0] ? e[n["#"][0]][n["#"][1]] && e[n["#"][0]][n["#"][1]].call(null) : e[n["#"][0]] && e[n["#"][0]].call(null); - }(n, o); + }; + "s" === e["#"][0] ? n[e["#"][0]][e["#"][1]] && n[e["#"][0]][e["#"][1]].call(null) : n[e["#"][0]] && n[e["#"][0]].call(null); + }(e, o); + } catch (e) { + return t(e); + } }, this.websocket.onclose = function(t) { - if (n.missedPing = 0, clearInterval(n.pingInterval), n.events.emit("disconnect", t.code, t.reason), - 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++, - n.websocket = void 0, setTimeout(function() { - return n.create(); - }, 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 { - n.events.removeAllEvents(); - for (var e = 0, o = Object.keys(n), s = o.length; e < s; e++) n[o[e]] = null; + if (clearInterval(e.pingInterval), e.events.emit("disconnect", t.code, t.reason), + 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++, + e.websocket = void 0, setTimeout(function() { + return e.create(); + }, 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 { + e.events.removeAllEvents(); + for (var n = 0, o = Object.keys(e), s = o.length; n < s; n++) e[o[n]] = null; } }; }, s; diff --git a/dist/browser/clusterws.min.js b/dist/browser/clusterws.min.js index 53f9305..54ca898 100644 --- a/dist/browser/clusterws.min.js +++ b/dist/browser/clusterws.min.js @@ -1 +1 @@ -var ClusterWS=function(){"use strict";function t(t){return console.log(t)}var n=function(){function n(t,n){this.name=n,this.socket=t,this.subscribe()}return n.prototype.watch=function(n){return"[object Function]"!=={}.toString.call(n)?t("Listener must be a function"):(this.listener=n,this)},n.prototype.publish=function(t){return this.socket.send(this.name,t,"publish"),this},n.prototype.unsubscribe=function(){this.socket.send("unsubscribe",this.name,"system"),this.socket.channels[this.name]=null},n.prototype.onMessage=function(t){this.listener&&this.listener.call(null,t)},n.prototype.subscribe=function(){this.socket.send("subscribe",this.name,"system")},n}(),e=function(){function n(){this.events={}}return n.prototype.on=function(n,e){if("[object Function]"!=={}.toString.call(e))return t("Listener must be a function");this.events[n]=e},n.prototype.emit=function(t){for(var n,e=[],o=1;othis.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")}return s.prototype.on=function(t,n){this.events.on(t,n)},s.prototype.send=function(t,n,e){void 0===e&&(e="emit"),this.websocket.send(this.useBinary?function(t){for(var n=t.length,e=new Uint8Array(n),o=0;o2&&t.disconnect(4001,"Did not get pings")},n["#"][2].ping),t.useBinary=n["#"][2].binary,t.events.emit("connect")}}};"s"===n["#"][0]?e[n["#"][0]][n["#"][1]]&&e[n["#"][0]][n["#"][1]].call(null):e[n["#"][0]]&&e[n["#"][0]].call(null)}(n,o)},this.websocket.onclose=function(t){if(n.missedPing=0,clearInterval(n.pingInterval),n.events.emit("disconnect",t.code,t.reason),n.options.autoReconnect&&1e3!==t.code&&(0===n.options.autoReconnectOptions.attempts||n.reconnectionAttemptedthis.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")}return s.prototype.on=function(t,e){this.events.on(t,e)},s.prototype.send=function(t,e,n){void 0===n&&(n="emit"),this.websocket.send(this.useBinary?function(t){for(var e=t.length,n=new Uint8Array(e),o=0;o void): void; diff --git a/dist/index.js b/dist/index.js index 4f27aed..ebbac1b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -50,9 +50,9 @@ function decode(t, e) { }, s: { c: function() { - t.pingInterval = setInterval(function() { - return t.missedPing++ > 2 && t.disconnect(4001, "Did not get pings"); - }, e["#"][2].ping), t.useBinary = e["#"][2].binary, t.events.emit("connect"); + t.useBinary = e["#"][2].binary, t.pingInterval = setInterval(function() { + t.isAlive ? (t.send("#9", null, "ping"), t.isAlive = !1) : t.disconnect(4001, "No pong from the server"); + }, e["#"][2].ping), t.events.emit("connect"); } } }; @@ -81,8 +81,8 @@ function encode(t, e, n) { var ClusterWS = function() { function t(t) { - return this.events = new EventEmitter(), this.channels = {}, this.useBinary = !1, - this.missedPing = 0, this.reconnectionAttempted = 0, t.url ? (this.options = { + return this.events = new EventEmitter(), this.isAlive = !0, this.channels = {}, + this.useBinary = !1, this.reconnectionAttempted = 0, t.url ? (this.options = { url: t.url, autoReconnect: t.autoReconnect || !1, autoReconnectOptions: t.autoReconnectOptions ? { @@ -94,7 +94,7 @@ var ClusterWS = function() { minInterval: 1e3, maxInterval: 5e3 } - }, this.options.autoReconnectOptions.minInterval > this.options.autoReconnectOptions.maxInterval ? logError("minInterval option can not be more than maxInterval option") : void this.create()) : logError("Url must be provided and it must be string"); + }, this.options.autoReconnectOptions.minInterval > this.options.autoReconnectOptions.maxInterval ? logError("minInterval option can not be more than maxInterval option") : void this.create()) : logError("Url must be provided and it must be a string"); } return t.prototype.on = function(t, e) { this.events.on(t, e); @@ -118,15 +118,14 @@ var ClusterWS = function() { return t.events.emit("error", e); }, this.websocket.onmessage = function(e) { var n = "string" != typeof e.data ? String.fromCharCode.apply(null, new Uint8Array(e.data)) : e.data; - if ("#0" === n) return t.missedPing = 0, t.send("#1", null, "ping"); + if ("#10" === n) return t.isAlive = !0; try { - n = JSON.parse(n); + n = JSON.parse(n), decode(t, n); } catch (t) { return logError(t); } - decode(t, n); }, this.websocket.onclose = function(e) { - if (t.missedPing = 0, clearInterval(t.pingInterval), t.events.emit("disconnect", e.code, e.reason), + if (clearInterval(t.pingInterval), t.events.emit("disconnect", e.code, e.reason), 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++, t.websocket = void 0, setTimeout(function() { return t.create(); diff --git a/docs/README.md b/docs/README.md index 75274cd..4980e17 100644 --- a/docs/README.md +++ b/docs/README.md @@ -6,7 +6,7 @@

- GitHub license + GitHub license NPM Version Maintain

diff --git a/src/index.ts b/src/index.ts index 96efcf9..e9e0386 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,9 +8,9 @@ declare const window: any export default class ClusterWS { public events: EventEmitter = new EventEmitter() + public isAlive: boolean = true public channels: CustomObject = {} public useBinary: boolean = false - public missedPing: number = 0 public pingInterval: any private options: Options @@ -19,7 +19,7 @@ export default class ClusterWS { constructor(configurations: Configurations) { if (!configurations.url) - return logError('Url must be provided and it must be string') + return logError('Url must be provided and it must be a string') this.options = { url: configurations.url, @@ -80,22 +80,20 @@ export default class ClusterWS { this.channels[keys[i]] && this.channels[keys[i]].subscribe() } this.websocket.onerror = (err: any): void => this.events.emit('error', err) - this.websocket.onmessage = (message: any): void => { + this.websocket.onmessage = (message: any): boolean => { let data: string = typeof message.data !== 'string' ? String.fromCharCode.apply(null, new Uint8Array(message.data)) : message.data - if (data === '#0') { - this.missedPing = 0 - return this.send('#1', null, 'ping') - } + if (data === '#10') + return this.isAlive = true + try { data = JSON.parse(data) + decode(this, data) } catch (e) { return logError(e) } - - decode(this, data) } + this.websocket.onclose = (event: CloseEvent): void => { - this.missedPing = 0 clearInterval(this.pingInterval) this.events.emit('disconnect', event.code, event.reason) diff --git a/src/modules/parser.ts b/src/modules/parser.ts index 97b84dd..ef1da3a 100644 --- a/src/modules/parser.ts +++ b/src/modules/parser.ts @@ -14,8 +14,15 @@ export function decode(socket: ClusterWS, message: any): void { p: (): void => socket.channels[message['#'][1]] && socket.channels[message['#'][1]].onMessage(message['#'][2]), s: { c: (): void => { - socket.pingInterval = setInterval((): void => socket.missedPing++ > 2 && socket.disconnect(4001, 'Did not get pings'), message['#'][2].ping) socket.useBinary = message['#'][2].binary + socket.pingInterval = setInterval( + (): void => { + if (socket.isAlive) { + socket.send('#9', null, 'ping') + socket.isAlive = false + } else socket.disconnect(4001, 'No pong from the server') + }, + message['#'][2].ping) socket.events.emit('connect') } } From 395db439581abdfed46e05012566d7693107fc5b Mon Sep 17 00:00:00 2001 From: Dmitrii Goriunov Date: Fri, 30 Mar 2018 00:55:01 +1300 Subject: [PATCH 2/3] New version 2.2.0 --- dist/browser/clusterws.js | 8 ++++---- dist/browser/clusterws.min.js | 2 +- dist/index.d.ts | 1 + dist/index.js | 8 ++++---- dist/package.json | 2 +- package.json | 4 ++-- src/index.ts | 10 +++++++--- src/modules/parser.ts | 9 +-------- 8 files changed, 21 insertions(+), 23 deletions(-) diff --git a/dist/browser/clusterws.js b/dist/browser/clusterws.js index 3aa9845..b09768f 100644 --- a/dist/browser/clusterws.js +++ b/dist/browser/clusterws.js @@ -55,7 +55,7 @@ var ClusterWS = function() { return function() { function s(e) { return this.events = new n(), this.isAlive = !0, this.channels = {}, this.useBinary = !1, - this.reconnectionAttempted = 0, e.url ? (this.options = { + this.missedPing = 0, this.reconnectionAttempted = 0, e.url ? (this.options = { url: e.url, autoReconnect: e.autoReconnect || !1, autoReconnectOptions: e.autoReconnectOptions ? { @@ -94,7 +94,7 @@ var ClusterWS = function() { return e.events.emit("error", t); }, this.websocket.onmessage = function(n) { var o = "string" != typeof n.data ? String.fromCharCode.apply(null, new Uint8Array(n.data)) : n.data; - if ("#10" === o) return e.isAlive = !0; + if ("#0" === o) return e.missedPing = 0, e.send("#1", null, "ping"); try { o = JSON.parse(o), function(t, e) { var n = { @@ -107,7 +107,7 @@ var ClusterWS = function() { s: { c: function() { t.useBinary = e["#"][2].binary, t.pingInterval = setInterval(function() { - t.isAlive ? (t.send("#9", null, "ping"), t.isAlive = !1) : t.disconnect(4001, "No pong from the server"); + return t.missedPing++ > 2 && t.disconnect(4001, "Did not get pings"); }, e["#"][2].ping), t.events.emit("connect"); } } @@ -118,7 +118,7 @@ var ClusterWS = function() { return t(e); } }, this.websocket.onclose = function(t) { - if (clearInterval(e.pingInterval), e.events.emit("disconnect", t.code, t.reason), + if (e.missedPing = 0, clearInterval(e.pingInterval), e.events.emit("disconnect", t.code, t.reason), 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++, e.websocket = void 0, setTimeout(function() { return e.create(); diff --git a/dist/browser/clusterws.min.js b/dist/browser/clusterws.min.js index 54ca898..5d8fb7e 100644 --- a/dist/browser/clusterws.min.js +++ b/dist/browser/clusterws.min.js @@ -1 +1 @@ -var ClusterWS=function(){"use strict";function t(t){return console.log(t)}var e=function(){function e(t,e){this.name=e,this.socket=t,this.subscribe()}return e.prototype.watch=function(e){return"[object Function]"!=={}.toString.call(e)?t("Listener must be a function"):(this.listener=e,this)},e.prototype.publish=function(t){return this.socket.send(this.name,t,"publish"),this},e.prototype.unsubscribe=function(){this.socket.send("unsubscribe",this.name,"system"),this.socket.channels[this.name]=null},e.prototype.onMessage=function(t){this.listener&&this.listener.call(null,t)},e.prototype.subscribe=function(){this.socket.send("subscribe",this.name,"system")},e}(),n=function(){function e(){this.events={}}return e.prototype.on=function(e,n){if("[object Function]"!=={}.toString.call(n))return t("Listener must be a function");this.events[e]=n},e.prototype.emit=function(t){for(var e,n=[],o=1;othis.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")}return s.prototype.on=function(t,e){this.events.on(t,e)},s.prototype.send=function(t,e,n){void 0===n&&(n="emit"),this.websocket.send(this.useBinary?function(t){for(var e=t.length,n=new Uint8Array(e),o=0;othis.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")}return s.prototype.on=function(t,e){this.events.on(t,e)},s.prototype.send=function(t,e,n){void 0===n&&(n="emit"),this.websocket.send(this.useBinary?function(t){for(var e=t.length,n=new Uint8Array(e),o=0;o2&&t.disconnect(4001,"Did not get pings")},e["#"][2].ping),t.events.emit("connect")}}};"s"===e["#"][0]?n[e["#"][0]][e["#"][1]]&&n[e["#"][0]][e["#"][1]].call(null):n[e["#"][0]]&&n[e["#"][0]].call(null)}(e,o)}catch(e){return t(e)}},this.websocket.onclose=function(t){if(e.missedPing=0,clearInterval(e.pingInterval),e.events.emit("disconnect",t.code,t.reason),e.options.autoReconnect&&1e3!==t.code&&(0===e.options.autoReconnectOptions.attempts||e.reconnectionAttempted void): void; diff --git a/dist/index.js b/dist/index.js index ebbac1b..dc73f3b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -51,7 +51,7 @@ function decode(t, e) { s: { c: function() { t.useBinary = e["#"][2].binary, t.pingInterval = setInterval(function() { - t.isAlive ? (t.send("#9", null, "ping"), t.isAlive = !1) : t.disconnect(4001, "No pong from the server"); + return t.missedPing++ > 2 && t.disconnect(4001, "Did not get pings"); }, e["#"][2].ping), t.events.emit("connect"); } } @@ -82,7 +82,7 @@ function encode(t, e, n) { var ClusterWS = function() { function t(t) { return this.events = new EventEmitter(), this.isAlive = !0, this.channels = {}, - this.useBinary = !1, this.reconnectionAttempted = 0, t.url ? (this.options = { + this.useBinary = !1, this.missedPing = 0, this.reconnectionAttempted = 0, t.url ? (this.options = { url: t.url, autoReconnect: t.autoReconnect || !1, autoReconnectOptions: t.autoReconnectOptions ? { @@ -118,14 +118,14 @@ var ClusterWS = function() { return t.events.emit("error", e); }, this.websocket.onmessage = function(e) { var n = "string" != typeof e.data ? String.fromCharCode.apply(null, new Uint8Array(e.data)) : e.data; - if ("#10" === n) return t.isAlive = !0; + if ("#0" === n) return t.missedPing = 0, t.send("#1", null, "ping"); try { n = JSON.parse(n), decode(t, n); } catch (t) { return logError(t); } }, this.websocket.onclose = function(e) { - if (clearInterval(t.pingInterval), t.events.emit("disconnect", e.code, e.reason), + if (t.missedPing = 0, clearInterval(t.pingInterval), t.events.emit("disconnect", e.code, e.reason), 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++, t.websocket = void 0, setTimeout(function() { return t.create(); diff --git a/dist/package.json b/dist/package.json index 9523e11..ff485f6 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "clusterws-client-js", - "version": "2.1.0", + "version": "2.2.0", "description": "JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js.", "main": "index.js", "types": "index.d.ts", diff --git a/package.json b/package.json index 02596f6..0093a3d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "clusterws-client-js", - "version": "2.1.0", + "version": "2.2.0", "description": "JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js.", "main": "index.js", "types": "index.d.ts", @@ -31,4 +31,4 @@ "ws client", "javascript" ] -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e9e0386..5e0aba4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ export default class ClusterWS { public isAlive: boolean = true public channels: CustomObject = {} public useBinary: boolean = false + public missedPing: number = 0 public pingInterval: any private options: Options @@ -80,12 +81,14 @@ export default class ClusterWS { this.channels[keys[i]] && this.channels[keys[i]].subscribe() } this.websocket.onerror = (err: any): void => this.events.emit('error', err) - this.websocket.onmessage = (message: any): boolean => { + this.websocket.onmessage = (message: any): void => { let data: string = typeof message.data !== 'string' ? String.fromCharCode.apply(null, new Uint8Array(message.data)) : message.data - if (data === '#10') - return this.isAlive = true + if (data === '#0') { + this.missedPing = 0 + return this.send('#1', null, 'ping') + } try { data = JSON.parse(data) @@ -94,6 +97,7 @@ export default class ClusterWS { } this.websocket.onclose = (event: CloseEvent): void => { + this.missedPing = 0 clearInterval(this.pingInterval) this.events.emit('disconnect', event.code, event.reason) diff --git a/src/modules/parser.ts b/src/modules/parser.ts index ef1da3a..2f21d5a 100644 --- a/src/modules/parser.ts +++ b/src/modules/parser.ts @@ -15,14 +15,7 @@ export function decode(socket: ClusterWS, message: any): void { s: { c: (): void => { socket.useBinary = message['#'][2].binary - socket.pingInterval = setInterval( - (): void => { - if (socket.isAlive) { - socket.send('#9', null, 'ping') - socket.isAlive = false - } else socket.disconnect(4001, 'No pong from the server') - }, - message['#'][2].ping) + socket.pingInterval = setInterval((): void => socket.missedPing++ > 2 && socket.disconnect(4001, 'Did not get pings'), message['#'][2].ping) socket.events.emit('connect') } } From 1176d4692d52258443582e63d9a80447e2fbfeb7 Mon Sep 17 00:00:00 2001 From: Dmitrii Goriunov Date: Fri, 30 Mar 2018 01:01:53 +1300 Subject: [PATCH 3/3] Update readme --- dist/README.md | 2 +- dist/package.json | 2 +- docs/README.md | 2 +- package.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/README.md b/dist/README.md index 4980e17..6752f30 100644 --- a/dist/README.md +++ b/dist/README.md @@ -7,7 +7,7 @@

GitHub license - NPM Version + NPM Version Maintain

diff --git a/dist/package.json b/dist/package.json index ff485f6..b3635f1 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "clusterws-client-js", - "version": "2.2.0", + "version": "2.2.1", "description": "JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js.", "main": "index.js", "types": "index.d.ts", diff --git a/docs/README.md b/docs/README.md index 4980e17..6752f30 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,7 +7,7 @@

GitHub license - NPM Version + NPM Version Maintain

diff --git a/package.json b/package.json index 0093a3d..b16d6de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "clusterws-client-js", - "version": "2.2.0", + "version": "2.2.1", "description": "JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js.", "main": "index.js", "types": "index.d.ts",