att-client
Advanced tools
Comparing version 0.2.18 to 0.2.19
@@ -11,2 +11,3 @@ import type { Client } from '../Client'; | ||
private migrationDelay?; | ||
private pingInterval?; | ||
private resolveMigration?; | ||
@@ -35,3 +36,3 @@ private subscriptions; | ||
*/ | ||
migrate(): Promise<void>; | ||
migrate(migrationToken?: string): Promise<void>; | ||
/** | ||
@@ -38,0 +39,0 @@ * Sends a migration token over a given WebSocket instance and promises to handle its response. |
@@ -53,3 +53,2 @@ "use strict"; | ||
const that = this; | ||
let interval; | ||
function handleError(error) { | ||
@@ -70,3 +69,3 @@ that.logger.error('An error occurred on the WebSocket.', error); | ||
this.off('pong', handlePong); | ||
clearInterval(interval); | ||
clearInterval(that.pingInterval); | ||
/* Migrations close WebSocket with code 3000 or 3001. */ | ||
@@ -111,3 +110,4 @@ if (code !== 3000 && code !== 3001) { | ||
that.logger.debug('Registering WebSocket ping interval.'); | ||
interval = setInterval(() => { | ||
clearInterval(that.pingInterval); | ||
that.pingInterval = setInterval(() => { | ||
that.ping(this); | ||
@@ -131,3 +131,3 @@ }, that.client.config.webSocketPingInterval); | ||
*/ | ||
async migrate() { | ||
async migrate(migrationToken) { | ||
await this.halted; | ||
@@ -147,10 +147,17 @@ if (typeof this.ws === 'undefined') { | ||
clearTimeout(this.migrationDelay); | ||
this.logger.debug('Retrieving migration token.'); | ||
const requestMigrateResponse = await this.send('GET', 'migrate'); | ||
if (typeof requestMigrateResponse === 'undefined') { | ||
await this.retryMigration(); | ||
return; | ||
let token; | ||
if (typeof migrationToken === 'undefined') { | ||
this.logger.debug('Retrieving migration token.'); | ||
const requestMigrateResponse = await this.send('GET', 'migrate'); | ||
if (typeof requestMigrateResponse === 'undefined') { | ||
await this.retryMigration(); | ||
return; | ||
} | ||
token = requestMigrateResponse.content.token; | ||
this.logger.debug('Received migration token.', token); | ||
} | ||
const { token } = requestMigrateResponse.content; | ||
this.logger.debug('Received migration token.', token); | ||
else { | ||
token = migrationToken; | ||
this.logger.debug('Reusing migration token.', token); | ||
} | ||
/* Track migration state. Will halt all outbound messages. */ | ||
@@ -170,2 +177,3 @@ this.halted = new Promise(resolve => { | ||
catch (error) { | ||
clearInterval(this.pingInterval); | ||
this.ws.removeAllListeners(); | ||
@@ -176,3 +184,3 @@ this.ws.close(3001, 'Migration aborted.'); | ||
this.clearMigration(); | ||
await this.retryMigration(); | ||
await this.retryMigration(token); | ||
return; | ||
@@ -213,6 +221,6 @@ } | ||
*/ | ||
async retryMigration() { | ||
async retryMigration(migrationToken) { | ||
this.logger.error(`Client failed to migrate WebSocket. Retrying in ${this.client.config.webSocketMigrationRetryDelay} ms.`); | ||
await new Promise(resolve => setTimeout(resolve, this.client.config.webSocketMigrationRetryDelay)); | ||
await this.migrate(); | ||
await this.migrate(migrationToken); | ||
} | ||
@@ -219,0 +227,0 @@ /** |
{ | ||
"name": "att-client", | ||
"version": "0.2.18", | ||
"version": "0.2.19", | ||
"description": "Node bot library for A Township Tale, a VR game by Alta Reality Pty Ltd.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
97805
2313