Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reconnect event to ReconnectingWebsocket #106

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/events/ReconnectingWebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const debug = debugModule("awry:ReconnectingWebSocket");
* attempts are about to be made.
* signature (details: { reason, err, code, message })
*
* - 'reconnecting' - fired when the websocket is about to attempt to reconnect.
* signature (attempts, nextTimeout)
*
* - 'reconnected' - fired when the websocket was previously disconnected
* but has been reconnected automatically. signature ()
*
Expand Down Expand Up @@ -69,6 +72,9 @@ export default class ReconnectingWebSocket extends events.EventEmitter {
/** @private */
this._reconnect = reconnect;

/** @private */
this._reconnecting = false;

debug("attempting initial connection", { url });
this.connect(err => {
if (err) {
Expand All @@ -91,6 +97,7 @@ export default class ReconnectingWebSocket extends events.EventEmitter {
*/
connect(callback) {
const operation = retry.operation(this._retryOptions);
const timeouts = retry.timeouts(this._retryOptions);

operation.attempt(attemptNumber => {
const numRetries = attemptNumber - 1;
Expand All @@ -101,6 +108,13 @@ export default class ReconnectingWebSocket extends events.EventEmitter {
this._ws.close();
}

if (this._reconnecting) {
this.emit("reconnecting", {
attempts: attemptNumber,
nextTimeout: timeouts[attemptNumber]
});
}

/** @private */
this._ws = new WebSocket(this._url, this._wsOptions);

Expand All @@ -112,6 +126,7 @@ export default class ReconnectingWebSocket extends events.EventEmitter {
return;
}

this._reconnecting = true;
if (operation.retry(err)) {
return;
}
Expand All @@ -120,11 +135,13 @@ export default class ReconnectingWebSocket extends events.EventEmitter {
debug('connection attempts exhaused',
{ err, reconnect: this._reconnect, numRetries });

this._reconnecting = false;
callback(operation.mainError());
};

this._ws.once("error", handleConnectError);
this._ws.once("open", () => {
this._reconnecting = false;
debug("connected", { numRetries });
this._ws.removeListener("error", handleConnectError);
this._ws.on("message", this.emit.bind(this, "message"));
Expand Down Expand Up @@ -154,6 +171,7 @@ export default class ReconnectingWebSocket extends events.EventEmitter {
this.emit("disconnected", { reason: "error", err });
debug("attempting to reconnect");

this._reconnecting = true;
this.connect(err => {
if (err) {
this.emit("error", err);
Expand Down Expand Up @@ -182,6 +200,7 @@ export default class ReconnectingWebSocket extends events.EventEmitter {
this.emit("disconnected", { reason: "close", code, message });
debug("attempting to reconnect");

this._reconnecting = true;
this.connect(err => {
if (err) {
this.emit("error", err);
Expand Down