Skip to content

Commit

Permalink
Merge bf33e1f into 704e7a3
Browse files Browse the repository at this point in the history
  • Loading branch information
nivida committed Aug 2, 2019
2 parents 704e7a3 + bf33e1f commit 2ce618c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default class ProvidersModuleFactory {
connection = new window.WebSocket(url, options.protocol);
}

return new WebsocketProvider(connection, options.timeout);
return new WebsocketProvider(connection, options.timeout, options.reconnectionTimeout);
}

/**
Expand Down
28 changes: 24 additions & 4 deletions packages/web3-providers/src/providers/WebsocketProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,31 @@ import isArray from 'lodash/isArray';
export default class WebsocketProvider extends AbstractSocketProvider {
/**
* @param {WebSocket} connection
* @param {Number} timeout
* @param {Number} responseTimeout
* @param {Number} reconnectionTimeout
*
* @constructor
*/
constructor(connection, timeout) {
super(connection, timeout);
constructor(connection, responseTimeout, reconnectionTimeout = 5000) {
super(connection, responseTimeout);
this.host = this.connection.url;
this.reconnectionTimeout = reconnectionTimeout;
this.reconnecting = false;
}

/**
* Emits the connect event and checks if there are subscriptions defined that should be resubscribed.
*
* @method onConnect
*/
async onConnect() {
if (this.reconnecting) {
this.emit('reconnected');
}

await super.onConnect()

this.reconnecting = false;
}

/**
Expand Down Expand Up @@ -86,6 +104,8 @@ export default class WebsocketProvider extends AbstractSocketProvider {
* @method reconnect
*/
reconnect() {
this.reconnecting = true;

setTimeout(() => {
this.removeAllSocketListeners();

Expand All @@ -106,7 +126,7 @@ export default class WebsocketProvider extends AbstractSocketProvider {

this.connection = connection;
this.registerEventListeners();
}, 5000);
}, this.reconnectionTimeout);
}

/**
Expand Down

0 comments on commit 2ce618c

Please sign in to comment.