Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Removed check node up from WS #3587
Browse files Browse the repository at this point in the history
  • Loading branch information
ngotchac committed Nov 23, 2016
1 parent bb6fe16 commit 0c3d87f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 45 deletions.
47 changes: 13 additions & 34 deletions js/src/api/transport/ws/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ export default class Ws extends JsonRpcBase {
}
}

_checkNodeUp () {
return fetch('/', { method: 'HEAD' })
.then((r) => {
return r.status === 200;
}, () => {
return false;
})
.catch(() => false);
}

_onOpen = (event) => {
console.log('ws:onOpen', event);
this._connected = true;
Expand All @@ -127,36 +117,25 @@ export default class Ws extends JsonRpcBase {
this._connected = false;
this._connecting = false;

this._checkNodeUp()
.then((up) => {
// If the connection has been closed and the node
// is up, it means we have a wrong token
// Event code 1006 for WS means there is an error
// (not just closed by server)
if (up && event.code === 1006) {
event.status = 403;
}

this._lastError = event;
this._lastError = event;

if (this._autoConnect) {
const timeout = this.retryTimeout;
if (this._autoConnect) {
const timeout = this.retryTimeout;

const time = timeout < 1000
? Math.round(timeout) + 'ms'
: (Math.round(timeout / 10) / 100) + 's';
const time = timeout < 1000
? Math.round(timeout) + 'ms'
: (Math.round(timeout / 10) / 100) + 's';

console.log('ws:onClose', `trying again in ${time}...`);
console.log('ws:onClose', `trying again in ${time}...`);

this._reconnectTimeoutId = setTimeout(() => {
this._connect();
}, timeout);
this._reconnectTimeoutId = setTimeout(() => {
this._connect();
}, timeout);

return;
}
return;
}

console.log('ws:onClose', event);
});
console.log('ws:onClose', event);
}

_onError = (event) => {
Expand Down
37 changes: 26 additions & 11 deletions js/src/secureApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ export default class SecureApi extends Api {
console.log('SecureApi:setToken', this._transport.token);
}

_checkNodeUp () {
return fetch('/', { method: 'HEAD' })
.then(
(r) => r.status === 200,
() => false
)
.catch(() => false);
}

_followConnection = () => {
const nextTick = () => {
if (this._followConnectionTimeoutId) {
Expand All @@ -65,17 +74,23 @@ export default class SecureApi extends Api {
if (isConnected) {
return this.connectSuccess();
} else if (lastError) {
const nextToken = this._tokensToTry[0] || 'initial';
const nextState = nextToken !== 'initial' ? 0 : 1;

// If previous token was wrong, delete it
if (lastError.status === 403) {
this._tokensToTry = this._tokensToTry.slice(1);
}

if (nextToken !== this._transport.token) {
this.updateToken(nextToken, nextState);
}
return this
._checkNodeUp()
.then((isNodeUp) => {
const nextToken = this._tokensToTry[0] || 'initial';
const nextState = nextToken !== 'initial' ? 0 : 1;

// If previous token was wrong (error while node up), delete it
if (isNodeUp) {
this._tokensToTry = this._tokensToTry.slice(1);
}

if (nextToken !== this._transport.token) {
this.updateToken(nextToken, nextState);
}

nextTick();
});
}
break;

Expand Down

0 comments on commit 0c3d87f

Please sign in to comment.