Skip to content

Commit 087ccbf

Browse files
Merge pull request #123 from kuzzleio/hotfix-122-too-many-reconnect
[MASTER] Hotfix #122 too many reconnect
2 parents 1896786 + 7dac17e commit 087ccbf

File tree

13 files changed

+315
-818
lines changed

13 files changed

+315
-818
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
*__note:__ the # at the end of lines are the pull request numbers on GitHub*
22

3+
# 2.1.5
4+
5+
* Hotfixed a bug when recovering from a websocket connection error created an exponential number of clients #122
6+
37
# 2.1.4
48

59
* https://github.com/kuzzleio/sdk-javascript/releases/tag/2.1.4

dist/kuzzle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.js.map

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "2.1.4",
3+
"version": "2.1.5",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/networkWrapper/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @param host
44
* @param wsPort
55
* @param ioPort
6+
* @param sslConnection
67
* @returns {Object} tnstantiated WebSocket/Socket.IO object
78
*/
89

@@ -11,7 +12,7 @@ function network(host, wsPort, ioPort, sslConnection) {
1112
if (typeof window !== 'undefined') {
1213
// use native websockets if the browser supports it
1314
if (typeof WebSocket !== 'undefined') {
14-
return new (require('./wrappers/wsbrowsers'))(host, wsPort, sslConnection);
15+
return new (require('./wrappers/websocket'))(host, wsPort, sslConnection);
1516
}
1617
// otherwise fallback to socket.io, if available
1718
else if (window.io) {
@@ -21,7 +22,7 @@ function network(host, wsPort, ioPort, sslConnection) {
2122
throw new Error('Aborting: no websocket support detected and no socket.io library loaded either.');
2223
}
2324

24-
return new (require('./wrappers/wsnode'))(host, wsPort, sslConnection);
25+
return new (require('./wrappers/websocket'))(host, wsPort, sslConnection);
2526
}
2627

2728
module.exports = network;

src/networkWrapper/wrappers/wsbrowsers.js renamed to src/networkWrapper/wrappers/websocket.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
function WSBrowsers(host, port, ssl) {
1+
function WSNode(host, port, ssl) {
22
var self = this;
3+
this.WebSocket = typeof WebSocket !== 'undefined' ? WebSocket : require('ws');
34
this.host = host;
45
this.port = port;
56
this.ssl = ssl;
67
this.client = null;
8+
this.wasConnected = false;
79
this.retrying = false;
10+
this.lasturl = null;
811

912
/*
10-
Listeners are stored using the following format:
11-
roomId: {
12-
fn: callback_function,
13-
once: boolean
14-
}
13+
Listeners are stored using the following format:
14+
roomId: {
15+
fn: callback_function,
16+
once: boolean
17+
}
1518
*/
1619
this.listeners = {
1720
error: [],
@@ -29,15 +32,25 @@ function WSBrowsers(host, port, ssl) {
2932
* @returns {Object} Socket
3033
*/
3134
this.connect = function (autoReconnect, reconnectionDelay) {
32-
this.client = new WebSocket((this.ssl ? 'wss://' : 'ws://') + this.host + ':' + this.port);
35+
var
36+
url = (this.ssl ? 'wss://' : 'ws://') + this.host + ':' + this.port,
37+
options = typeof window !== 'undefined' ? undefined : {perMessageDeflate: false};
38+
39+
if (url !== this.lasturl) {
40+
self.wasConnected = false;
41+
this.lasturl = url;
42+
}
43+
44+
this.client = new this.WebSocket(url, options);
3345

3446
this.client.onopen = function () {
35-
if (self.retrying) {
47+
if (self.wasConnected) {
3648
poke(self.listeners, 'reconnect');
3749
}
3850
else {
3951
poke(self.listeners, 'connect');
4052
}
53+
self.wasConnected = true;
4154
};
4255

4356
this.client.onclose = function (code, message) {
@@ -54,7 +67,7 @@ function WSBrowsers(host, port, ssl) {
5467
};
5568

5669
this.client.onmessage = function (payload) {
57-
var data = JSON.parse(payload.data);
70+
var data = JSON.parse(payload.data || payload);
5871

5972
if (data.room && self.listeners[data.room]) {
6073
poke(self.listeners, data.room, data);
@@ -190,7 +203,7 @@ function WSBrowsers(host, port, ssl) {
190203
reconnect: []
191204
};
192205

193-
this.retrying = false;
206+
this.wasConnected = false;
194207
this.client.close();
195208
this.client = null;
196209
};
@@ -242,15 +255,15 @@ function poke (listeners, roomId, payload) {
242255
function onClientError(autoReconnect, reconnectionDelay, message) {
243256
var self = this;
244257

245-
if (autoReconnect) {
258+
if (autoReconnect && !self.retrying) {
246259
self.retrying = true;
247260
setTimeout(function () {
261+
self.retrying = false;
248262
self.connect(autoReconnect, reconnectionDelay);
249263
}, reconnectionDelay);
250264
}
251265

252266
poke(self.listeners, 'error', message);
253267
}
254268

255-
256-
module.exports = WSBrowsers;
269+
module.exports = WSNode;

src/networkWrapper/wrappers/wsnode.js

Lines changed: 0 additions & 258 deletions
This file was deleted.

0 commit comments

Comments
 (0)