mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
Refactor websocket into separate services, clean up socket status communication (#4433)
* Refactor websocket into separate services, clean up socket status communication * cleanup * add EOF lines * fix keepalive logged in check * undo change * fix keepalive connection check * cleanup * add typings * secure connection Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
This commit is contained in:
parent
19333c53f6
commit
e9ba195d7d
52 changed files with 815 additions and 757 deletions
43
webclient/src/websocket/services/KeepAliveService.tsx
Normal file
43
webclient/src/websocket/services/KeepAliveService.tsx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { Subject } from "rxjs";
|
||||
|
||||
import { WebSocketService } from "./WebSocketService";
|
||||
|
||||
export class KeepAliveService {
|
||||
private socket: WebSocketService;
|
||||
|
||||
private keepalivecb: NodeJS.Timeout;
|
||||
private lastPingPending: boolean;
|
||||
|
||||
public disconnected$ = new Subject<void>();
|
||||
|
||||
constructor(socket: WebSocketService) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public startPingLoop(interval: number, ping: Function): void {
|
||||
this.keepalivecb = setInterval(() => {
|
||||
// check if the previous ping got no reply
|
||||
if (this.lastPingPending) {
|
||||
this.disconnected$.next();
|
||||
}
|
||||
|
||||
// stop the ping loop if we"re disconnected
|
||||
if (!this.socket.checkReadyState(WebSocket.OPEN)) {
|
||||
this.endPingLoop();
|
||||
return;
|
||||
}
|
||||
|
||||
this.lastPingPending = true;
|
||||
ping(() => this.lastPingPending = false);
|
||||
}, interval);
|
||||
}
|
||||
|
||||
public endPingLoop() {
|
||||
clearInterval(this.keepalivecb);
|
||||
this.keepalivecb = null;
|
||||
}
|
||||
|
||||
public resetPingFlag() {
|
||||
this.lastPingPending = false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue