Skip to content

Commit

Permalink
If our pinger fails to receive a response, properly disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
sebirdman committed Dec 21, 2017
1 parent 9a9d40c commit b0f8717
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default class Client extends EventEmitter {
* @param {string} [userName] - Authentication username for this connection.
* @param {string} [password] - Authentication password for this connection.
* @param {Message} [willMessage] - sent by the server when the client disconnects abnormally.
* @param {number} [keepAliveInterval=60] - ping the server every n ms to avoid being disconnected by the remote end.
* @param {number} [keepAliveInterval=60] - ping the server every n seconds to avoid being disconnected by the remote end.
* @param {number} [mqttVersion=4] - protocol version to use (3 or 4).
* @param {boolean} [cleanSession=true] - if true the client and server persistent state is deleted on successful connect.
*/
Expand Down
1 change: 1 addition & 0 deletions src/ClientImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ class ClientImplementation {

case MESSAGE_TYPE.PINGRESP:
// We don't care whether the server is still there (yet)
this.sendPinger && this.sendPinger.reset();
break;

case MESSAGE_TYPE.DISCONNECT:
Expand Down
17 changes: 13 additions & 4 deletions src/Pinger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @flow */

import WireMessage from './WireMessage';
import { MESSAGE_TYPE } from './constants';
import { ERROR, MESSAGE_TYPE } from './constants';
import { format } from './util';
import ClientImplementation from './ClientImplementation';

/**
Expand All @@ -11,6 +12,7 @@ import ClientImplementation from './ClientImplementation';
export default class {
_client: ClientImplementation;
_keepAliveIntervalMs: number;
isReset: boolean = false;
pingReq: ArrayBuffer = new WireMessage(MESSAGE_TYPE.PINGREQ).encode();
timeout: ?number;

Expand All @@ -21,12 +23,19 @@ export default class {
}

_doPing() {
this._client._trace('Pinger.doPing', 'send PINGREQ');
this._client.socket && this._client.socket.send(this.pingReq);
this.timeout = setTimeout(() => this._doPing(), this._keepAliveIntervalMs);
if (!this.isReset) {
this._client._trace('Pinger.doPing', 'Timed out');
this._client._disconnected(ERROR.PING_TIMEOUT.code, format(ERROR.PING_TIMEOUT));
} else {
this.isReset = false;
this._client._trace('Pinger.doPing', 'send PINGREQ');
this._client.socket && this._client.socket.send(this.pingReq);
this.timeout = setTimeout(() => this._doPing(), this._keepAliveIntervalMs);
}
}

reset() {
this.isReset = true;
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = null;
Expand Down

0 comments on commit b0f8717

Please sign in to comment.