diff --git a/src/Socket.js b/src/Socket.js index 2dda0f5..fff0548 100644 --- a/src/Socket.js +++ b/src/Socket.js @@ -61,7 +61,7 @@ export class Socket extends EventEmitter { } debug('connected') this.emit('connected') - resolve(this.auth(this.api.token)) + resolve(this._auth()) }) this.ws.on('close', () => { clearInterval(this.keepAliveInter) @@ -172,6 +172,21 @@ export class Socket extends EventEmitter { } } + async _auth () { + try { + await this.auth(this.api.token) + } catch (err) { + if (err.message === 'socket auth failed') { + // Retry with a new token (in case the token is invalid after a reconnect) + debug('auth retry') + await this.api.auth() + await this.auth(this.api.token) + } else { + throw err + } + } + } + auth (token) { return new Promise((resolve, reject) => { this.send(`auth ${token}`)