From aadc4fc5bb64fe2d9f6ff6726b90d6f3dbf8bb4f Mon Sep 17 00:00:00 2001 From: maciejhirsz Date: Thu, 26 Jul 2018 13:53:21 +0200 Subject: [PATCH] Fix reconnecting --- packages/backend/src/message.ts | 2 ++ packages/common/src/index.ts | 2 +- packages/frontend/src/Connection.ts | 21 ++++++++++++++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/message.ts b/packages/backend/src/message.ts index d15d84e..f906315 100644 --- a/packages/backend/src/message.ts +++ b/packages/backend/src/message.ts @@ -5,6 +5,8 @@ export function parseMessage(data: Data): Maybe { try { const message = JSON.parse(data.toString()); + console.log(message); + if (message && typeof message.msg === 'string' && typeof message.ts === 'string') { message.ts = new Date(message.ts); diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 0494500..c330025 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -7,4 +7,4 @@ import * as FeedMessage from './feed'; export { Types, FeedMessage }; // Increment this if breaking changes were made to types in `feed.ts` -export const VERSION: Types.FeedVersion = 7 as Types.FeedVersion; +export const VERSION: Types.FeedVersion = 8 as Types.FeedVersion; diff --git a/packages/frontend/src/Connection.ts b/packages/frontend/src/Connection.ts index 659e627..6e47a3a 100644 --- a/packages/frontend/src/Connection.ts +++ b/packages/frontend/src/Connection.ts @@ -24,7 +24,7 @@ export class Connection { while (!socket) { await sleep(timeout); - timeout = Math.max(timeout * 2, TIMEOUT_MAX) as Types.Milliseconds; + timeout = Math.min(timeout * 2, TIMEOUT_MAX) as Types.Milliseconds; socket = await Connection.trySocket(); } @@ -60,6 +60,7 @@ export class Connection { private pingId = 0; private pingTimeout: NodeJS.Timer; private pingSent: Maybe = null; + private resubscribeTo: Maybe = null; private socket: WebSocket; private state: Readonly; private readonly update: Update; @@ -82,9 +83,8 @@ export class Connection { nodes: new Map() }); - // Re-subscribe to previously selected chain if (this.state.subscribed) { - // TODO: Remember the previous subscription for after we get chain info + this.resubscribeTo = this.state.subscribed; this.state = this.update({ subscribed: null }); } @@ -102,6 +102,8 @@ export class Connection { this.pingId += 1; this.pingSent = timestamp(); this.socket.send(`ping:${this.pingId}`); + + this.pingTimeout = setTimeout(this.ping, 30000); } private pong(id: number) { @@ -122,12 +124,11 @@ export class Connection { this.pingSent = null; console.log('latency', latency); - - this.pingTimeout = setTimeout(this.ping, 30000); } private clean() { clearTimeout(this.pingTimeout); + this.pingSent = null; this.socket.removeEventListener('message', this.handleMessages); this.socket.removeEventListener('close', this.handleDisconnect); @@ -283,11 +284,21 @@ export class Connection { private autoSubscribe() { const { subscribed, chains } = this.state; + const { resubscribeTo } = this; if (subscribed) { return; } + if (resubscribeTo) { + this.resubscribeTo = null; + + if (chains.has(resubscribeTo)) { + this.subscribe(resubscribeTo); + return; + } + } + let topLabel: Maybe = null; let topCount: Types.NodeCount = 0 as Types.NodeCount;