Fix reconnecting

This commit is contained in:
maciejhirsz
2018-07-26 13:53:21 +02:00
parent 6f5daad6c1
commit aadc4fc5bb
3 changed files with 19 additions and 6 deletions
+2
View File
@@ -5,6 +5,8 @@ export function parseMessage(data: Data): Maybe<Message> {
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);
+1 -1
View File
@@ -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;
+16 -5
View File
@@ -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<Types.Timestamp> = null;
private resubscribeTo: Maybe<Types.ChainLabel> = null;
private socket: WebSocket;
private state: Readonly<State>;
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<Types.ChainLabel> = null;
let topCount: Types.NodeCount = 0 as Types.NodeCount;