mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-06-14 00:21:01 +00:00
Fix reconnecting
This commit is contained in:
@@ -5,6 +5,8 @@ export function parseMessage(data: Data): Maybe<Message> {
|
|||||||
try {
|
try {
|
||||||
const message = JSON.parse(data.toString());
|
const message = JSON.parse(data.toString());
|
||||||
|
|
||||||
|
console.log(message);
|
||||||
|
|
||||||
if (message && typeof message.msg === 'string' && typeof message.ts === 'string') {
|
if (message && typeof message.msg === 'string' && typeof message.ts === 'string') {
|
||||||
message.ts = new Date(message.ts);
|
message.ts = new Date(message.ts);
|
||||||
|
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ import * as FeedMessage from './feed';
|
|||||||
export { Types, FeedMessage };
|
export { Types, FeedMessage };
|
||||||
|
|
||||||
// Increment this if breaking changes were made to types in `feed.ts`
|
// 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;
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export class Connection {
|
|||||||
while (!socket) {
|
while (!socket) {
|
||||||
await sleep(timeout);
|
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();
|
socket = await Connection.trySocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +60,7 @@ export class Connection {
|
|||||||
private pingId = 0;
|
private pingId = 0;
|
||||||
private pingTimeout: NodeJS.Timer;
|
private pingTimeout: NodeJS.Timer;
|
||||||
private pingSent: Maybe<Types.Timestamp> = null;
|
private pingSent: Maybe<Types.Timestamp> = null;
|
||||||
|
private resubscribeTo: Maybe<Types.ChainLabel> = null;
|
||||||
private socket: WebSocket;
|
private socket: WebSocket;
|
||||||
private state: Readonly<State>;
|
private state: Readonly<State>;
|
||||||
private readonly update: Update;
|
private readonly update: Update;
|
||||||
@@ -82,9 +83,8 @@ export class Connection {
|
|||||||
nodes: new Map()
|
nodes: new Map()
|
||||||
});
|
});
|
||||||
|
|
||||||
// Re-subscribe to previously selected chain
|
|
||||||
if (this.state.subscribed) {
|
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 });
|
this.state = this.update({ subscribed: null });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,6 +102,8 @@ export class Connection {
|
|||||||
this.pingId += 1;
|
this.pingId += 1;
|
||||||
this.pingSent = timestamp();
|
this.pingSent = timestamp();
|
||||||
this.socket.send(`ping:${this.pingId}`);
|
this.socket.send(`ping:${this.pingId}`);
|
||||||
|
|
||||||
|
this.pingTimeout = setTimeout(this.ping, 30000);
|
||||||
}
|
}
|
||||||
|
|
||||||
private pong(id: number) {
|
private pong(id: number) {
|
||||||
@@ -122,12 +124,11 @@ export class Connection {
|
|||||||
this.pingSent = null;
|
this.pingSent = null;
|
||||||
|
|
||||||
console.log('latency', latency);
|
console.log('latency', latency);
|
||||||
|
|
||||||
this.pingTimeout = setTimeout(this.ping, 30000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private clean() {
|
private clean() {
|
||||||
clearTimeout(this.pingTimeout);
|
clearTimeout(this.pingTimeout);
|
||||||
|
this.pingSent = null;
|
||||||
|
|
||||||
this.socket.removeEventListener('message', this.handleMessages);
|
this.socket.removeEventListener('message', this.handleMessages);
|
||||||
this.socket.removeEventListener('close', this.handleDisconnect);
|
this.socket.removeEventListener('close', this.handleDisconnect);
|
||||||
@@ -283,11 +284,21 @@ export class Connection {
|
|||||||
|
|
||||||
private autoSubscribe() {
|
private autoSubscribe() {
|
||||||
const { subscribed, chains } = this.state;
|
const { subscribed, chains } = this.state;
|
||||||
|
const { resubscribeTo } = this;
|
||||||
|
|
||||||
if (subscribed) {
|
if (subscribed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (resubscribeTo) {
|
||||||
|
this.resubscribeTo = null;
|
||||||
|
|
||||||
|
if (chains.has(resubscribeTo)) {
|
||||||
|
this.subscribe(resubscribeTo);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let topLabel: Maybe<Types.ChainLabel> = null;
|
let topLabel: Maybe<Types.ChainLabel> = null;
|
||||||
let topCount: Types.NodeCount = 0 as Types.NodeCount;
|
let topCount: Types.NodeCount = 0 as Types.NodeCount;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user