Skip to content

Commit fce607b

Browse files
authored
[HOTFIX] Clear PingTimeout (#607)
Fixes the ping clearTimeout
1 parent 9eaf648 commit fce607b

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

package-lock.json

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": "7.5.5",
3+
"version": "7.5.6",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <support@kuzzle.io>",
66
"repository": {

src/protocols/WebSocket.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
1414
private client: any;
1515
private lasturl: any;
1616
private ping: any;
17+
private pongTimeoutId: ReturnType<typeof setTimeout>;
18+
private pingIntervalId: ReturnType<typeof setInterval>;
19+
private _pingInterval: number;
20+
private _pongTimeout: number;
1721

1822
/**
1923
* @param host Kuzzle server hostname or IP
@@ -66,6 +70,8 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
6670
}
6771
}
6872

73+
this._pingInterval = typeof options.pingInterval === 'number' ? options.pingInterval : 2000;
74+
this._pongTimeout = this._pingInterval;
6975
this.client = null;
7076
this.lasturl = null;
7177
}
@@ -108,16 +114,13 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
108114
* Send pings to the server
109115
*/
110116
this.pingIntervalId = setInterval(() => {
111-
if (this.client.readyState === 1) {
117+
if (this.client && this.client.readyState === 1) {
112118
this.ping();
113119
}
114120
this.pongTimeoutId = setTimeout(() => {
115121
const error: any = new Error('Connection lost.');
116122
error.status = 503;
117123
this.clientNetworkError(error);
118-
this.emit('disconnect');
119-
clearInterval(this.pingIntervalId);
120-
clearTimeout(this.pongTimeoutId);
121124
}, this._pongTimeout);
122125
}, this._pingInterval);
123126
return resolve();
@@ -149,8 +152,6 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
149152
error.status = status;
150153
this.clientNetworkError(error);
151154
}
152-
clearInterval(this.pingIntervalId);
153-
clearTimeout(this.pongTimeoutId);
154155
};
155156

156157
this.client.onerror = error => {
@@ -219,6 +220,25 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
219220
}
220221
}
221222

223+
/**
224+
* @override
225+
*/
226+
clientDisconnected() {
227+
clearInterval(this.pingIntervalId);
228+
clearTimeout(this.pongTimeoutId);
229+
super.clientDisconnected();
230+
}
231+
232+
/**
233+
* @override
234+
*
235+
* @param {Error} error
236+
*/
237+
clientNetworkError (error) {
238+
clearInterval(this.pingIntervalId);
239+
clearTimeout(this.pongTimeoutId);
240+
super.clientNetworkError(error);
241+
}
222242
/**
223243
* Closes the connection
224244
*/

src/protocols/abstract/Realtime.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@ import { KuzzleAbstractProtocol } from './Base';
55
export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol {
66
protected _autoReconnect: boolean;
77
protected _reconnectionDelay: number;
8-
protected _pingInterval: number;
9-
protected _pongTimeout: number;
108
protected wasConnected: boolean;
119
protected stopRetryingToConnect: boolean;
1210
protected retrying: boolean;
13-
protected pongTimeoutId: ReturnType<typeof setTimeout>;
14-
protected pingIntervalId: ReturnType<typeof setInterval>;
1511

1612
constructor (host, options: any = {}, name: string) {
1713
super(host, options, name);
1814

1915
this._autoReconnect = typeof options.autoReconnect === 'boolean' ? options.autoReconnect : true;
2016
this._reconnectionDelay = typeof options.reconnectionDelay === 'number' ? options.reconnectionDelay : 1000;
21-
this._pingInterval = typeof options.pingInterval === 'number' ? options.pingInterval : 2000;
22-
this._pongTimeout = this._pingInterval;
2317

2418
this.wasConnected = false;
2519
this.stopRetryingToConnect = false;

test/protocol/WebSocket.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,45 @@ describe('WebSocket networking module', () => {
127127
should(cb).be.calledOnce();
128128
});
129129

130+
it('should clear pongTimeout and pingInterval on a networkError', () => {
131+
const cb = sinon.stub();
132+
const clearTimeout = sinon.stub(clock, 'clearTimeout');
133+
const clearInterval = sinon.stub(clock, 'clearInterval');
134+
135+
websocket.retrying = false;
136+
websocket.addListener('networkError', cb);
137+
should(websocket.listeners('networkError').length).be.eql(1);
138+
139+
websocket.connect();
140+
websocket.connect = sinon.stub().rejects();
141+
clientStub.onopen();
142+
clientStub.onerror();
143+
should(clearTimeout)
144+
.be.calledOnce();
145+
should(clearInterval)
146+
.be.calledOnce();
147+
});
148+
149+
it('should clear pongTimeout and pingInterval when the connection closes', () => {
150+
const cb = sinon.stub();
151+
const clearTimeout = sinon.stub(clock, 'clearTimeout');
152+
const clearInterval = sinon.stub(clock, 'clearInterval');
153+
154+
websocket.retrying = false;
155+
websocket.addListener('disconnect', cb);
156+
should(websocket.listeners('disconnect').length).be.eql(1);
157+
158+
websocket.connect();
159+
websocket.connect = sinon.stub().resolves();
160+
clientStub.onopen();
161+
clientStub.onclose(1000);
162+
websocket.close();
163+
should(clearTimeout)
164+
.be.calledOnce();
165+
should(clearInterval)
166+
.be.calledOnce();
167+
});
168+
130169
it('should try to reconnect on a connection error with autoReconnect = true', () => {
131170
const cb = sinon.stub();
132171

0 commit comments

Comments
 (0)