In case of ws, use the connected attribute

Instead of relying on non reliable source, directly
check the ws status
This commit is contained in:
Anthony Laibe 2018-10-17 11:27:12 +01:00 committed by Pascal Precht
parent 47dcb1552c
commit 27973461b1
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 15 additions and 4 deletions

View File

@ -130,7 +130,7 @@ class BlockchainConnector {
this.events.request("services:register", 'Ethereum', function (cb) {
async.waterfall([
function checkNodeConnection(next) {
if (!self.web3.currentProvider) {
if (!self.provider || !self.provider.connected()) {
return next(NO_NODE, {name: "No Blockchain node found", status: 'off'});
}
next();

View File

@ -26,8 +26,8 @@ class Provider {
// The best choice is to use void origin, BUT Geth rejects void origin, so to keep both clients happy we can use http://embark
self.provider = new this.web3.providers.WebsocketProvider(self.web3Endpoint, {headers: {Origin: "http://embark"}});
self.provider.on('error', e => self.logger.error('Websocket Error', e));
self.provider.on('end', e => self.logger.error('Websocket connection ended', e));
self.provider.on('error', () => self.logger.error('Websocket Error'));
self.provider.on('end', () => self.logger.error('Websocket connection ended'));
} else {
return callback(__("contracts config error: unknown deployment type %s", this.type));
}
@ -72,6 +72,16 @@ class Provider {
});
}
connected() {
if (this.type === 'rpc') {
return !!this.provider;
} else if (this.type === 'ws') {
return this.provider && this.provider.connected;
}
return false;
}
stop() {
if (this.provider && this.provider.removeAllListeners) {
this.provider.removeAllListeners('connect');
@ -79,8 +89,9 @@ class Provider {
this.provider.removeAllListeners('end');
this.provider.removeAllListeners('data');
this.provider.responseCallbacks = {};
this.provider = null;
}
this.provider = null;
this.web3.setProvider(null);
}
fundAccounts(callback) {