Skip to content

Commit 081aecc

Browse files
authored
Merge pull request #129 from GreenAsJade/ping_interval_option
Add ping_interval option to GobanSocket.options
2 parents 6fea965 + e9e3a4e commit 081aecc

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/GobanSocket.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ interface GobanSocketOptions {
4343
/** Don't automatically send pings */
4444
dont_ping?: boolean;
4545

46+
ping_interval?: number; // milliseconds
47+
4648
/** Don't log connection/disconnect things*/
4749
quiet?: boolean;
4850
}
@@ -96,7 +98,7 @@ export class GobanSocket<
9698
private reconnecting = false;
9799
private reconnect_tries = 0;
98100
private send_queue: (() => void)[] = [];
99-
private ping_interval?: ReturnType<typeof niceInterval>;
101+
private ping_timer?: ReturnType<typeof niceInterval>;
100102
private callbacks: Map<number, (data?: any, error?: ErrorResponse) => void> = new Map();
101103
private authentication?: DataArgument<SendProtocol["authenticate"]>;
102104
private manually_disconnected = false;
@@ -118,6 +120,7 @@ export class GobanSocket<
118120
this.latency = latency;
119121
this.clock_drift = drift;
120122
this.emit("latency", latency, drift);
123+
///console.log("Pong:", this.url);
121124
});
122125
}
123126

@@ -153,18 +156,18 @@ export class GobanSocket<
153156
latency: this.latency,
154157
} as DataArgument<SendProtocol["net/ping"]>);
155158
} else {
156-
if (this.ping_interval) {
157-
clearInterval(this.ping_interval);
158-
this.ping_interval = undefined;
159+
if (this.ping_timer) {
160+
clearInterval(this.ping_timer);
161+
this.ping_timer = undefined;
159162
}
160163
}
161164
};
162165

163-
if (this.ping_interval) {
164-
clearInterval(this.ping_interval);
166+
if (this.ping_timer) {
167+
clearInterval(this.ping_timer);
165168
}
166169

167-
this.ping_interval = niceInterval(ping, PING_INTERVAL);
170+
this.ping_timer = niceInterval(ping, this.options.ping_interval ?? PING_INTERVAL);
168171
ping();
169172
}
170173

0 commit comments

Comments
 (0)