Skip to content

Commit b190b6c

Browse files
committed
Make it so that ping_interval can be changed dynamically
1 parent 2831055 commit b190b6c

File tree

1 file changed

+45
-27
lines changed

1 file changed

+45
-27
lines changed

src/GobanSocket.ts

+45-27
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ interface GobanSocketOptions {
4545
/** Don't automatically send pings */
4646
dont_ping?: boolean;
4747

48-
ping_interval?: number; // milliseconds
48+
// Note: you can't turn off ping by setting ping interval to zero or undefined.
49+
ping_interval?: number; // milliseconds, applied if non-zero.
4950
timeout_delay?: number;
5051

5152
/** Don't log connection/disconnect things*/
@@ -61,7 +62,7 @@ const RECONNECTION_INTERVALS = [
6162
[250, 750], // if that doesn't work, keep trying in try again in 250-750ms intervals
6263
];
6364

64-
const PING_INTERVAL = 10000;
65+
const DEFAULT_PING_INTERVAL = 10000;
6566

6667
export type DataArgument<Entry> = Entry extends (...args: infer A) => void ? A[0] : never;
6768
export type ResponseType<Entry> = Entry extends (...args: any[]) => infer R ? R : never;
@@ -86,6 +87,8 @@ export class GobanSocket<
8687
public readonly url: string;
8788
public clock_drift = 0.0;
8889
public latency = 0.0;
90+
public options: GobanSocketOptions;
91+
8992
private socket: WebSocket;
9093
private last_request_id = 0;
9194
private promises_in_flight: Map<
@@ -106,7 +109,7 @@ export class GobanSocket<
106109
private callbacks: Map<number, (data?: any, error?: ErrorResponse) => void> = new Map();
107110
private authentication?: DataArgument<SendProtocol["authenticate"]>;
108111
private manually_disconnected = false;
109-
public options: GobanSocketOptions;
112+
private current_ping_interval: number;
110113

111114
constructor(url: string, options: GobanSocketOptions = {}) {
112115
super();
@@ -115,6 +118,8 @@ export class GobanSocket<
115118
url = url.replace(/^http/, "ws");
116119

117120
this.url = url;
121+
this.current_ping_interval = options.ping_interval || DEFAULT_PING_INTERVAL;
122+
118123
this.socket = this.connect();
119124

120125
this.on("net/pong", ({ client, server }: { client: number; server: number }) => {
@@ -148,32 +153,42 @@ export class GobanSocket<
148153
this.emit("timeout");
149154
};
150155

151-
private startPing(): void {
152-
if (!this.connected) {
153-
throw new Error("GobanSocket not connected");
156+
ping = () => {
157+
if (this.options.dont_ping) {
158+
return;
154159
}
155160

156-
const ping = () => {
157-
if (this.options.dont_ping) {
158-
return;
161+
if (this.connected) {
162+
this.send("net/ping", {
163+
client: Date.now(),
164+
drift: this.clock_drift,
165+
latency: this.latency,
166+
} as DataArgument<SendProtocol["net/ping"]>);
167+
if (this.options.timeout_delay) {
168+
this.timeout_timer = setTimeout(this.signalTimeout, this.options.timeout_delay);
159169
}
160-
161-
if (this.connected) {
162-
this.send("net/ping", {
163-
client: Date.now(),
164-
drift: this.clock_drift,
165-
latency: this.latency,
166-
} as DataArgument<SendProtocol["net/ping"]>);
167-
if (this.options.timeout_delay) {
168-
this.timeout_timer = setTimeout(this.signalTimeout, this.options.timeout_delay);
169-
}
170-
} else {
171-
if (this.ping_timer) {
172-
clearInterval(this.ping_timer);
173-
this.ping_timer = undefined;
174-
}
170+
if (
171+
this.options.ping_interval &&
172+
this.options.ping_interval !== this.current_ping_interval
173+
) {
174+
clearInterval(this.ping_timer);
175+
this.ping_timer = niceInterval(
176+
this.ping,
177+
this.options.ping_interval || DEFAULT_PING_INTERVAL,
178+
);
175179
}
176-
};
180+
} else {
181+
if (this.ping_timer) {
182+
clearInterval(this.ping_timer);
183+
this.ping_timer = undefined;
184+
}
185+
}
186+
};
187+
188+
private startPing(): void {
189+
if (!this.connected) {
190+
throw new Error("GobanSocket not connected");
191+
}
177192

178193
if (this.ping_timer) {
179194
clearInterval(this.ping_timer);
@@ -182,8 +197,11 @@ export class GobanSocket<
182197
clearTimeout(this.timeout_timer);
183198
}
184199

185-
this.ping_timer = niceInterval(ping, this.options.ping_interval ?? PING_INTERVAL);
186-
ping();
200+
this.ping_timer = niceInterval(
201+
this.ping,
202+
this.options.ping_interval || DEFAULT_PING_INTERVAL,
203+
);
204+
this.ping();
187205
}
188206

189207
private connect(): WebSocket {

0 commit comments

Comments
 (0)