implement client heartbeat #460.

This commit is contained in:
Miroslav Šedivý
2024-12-30 13:41:22 +01:00
parent 5169e0aad3
commit 3082d3241b
14 changed files with 62 additions and 7 deletions
+6
View File
@@ -20,6 +20,7 @@ export interface BaseEvents {
export abstract class BaseClient extends EventEmitter<BaseEvents> {
protected _ws?: WebSocket
protected _ws_heartbeat?: number
protected _peer?: RTCPeerConnection
protected _channel?: RTCDataChannel
protected _timeout?: number
@@ -82,6 +83,11 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
this._timeout = undefined
}
if (this._ws_heartbeat) {
clearInterval(this._ws_heartbeat)
this._ws_heartbeat = undefined
}
if (this._ws) {
// reset all events
this._ws.onmessage = () => {}
+5
View File
@@ -14,6 +14,9 @@ export const EVENT = {
DISCONNECT: 'system/disconnect',
ERROR: 'system/error',
},
CLIENT: {
HEARTBEAT: 'client/heartbeat'
},
SIGNAL: {
OFFER: 'signal/offer',
ANSWER: 'signal/answer',
@@ -69,6 +72,7 @@ export type Events = typeof EVENT
export type WebSocketEvents =
| SystemEvents
| ClientEvents
| ControlEvents
| MemberEvents
| SignalEvents
@@ -87,6 +91,7 @@ export type ControlEvents =
| typeof EVENT.CONTROL.KEYBOARD
export type SystemEvents = typeof EVENT.SYSTEM.DISCONNECT
export type ClientEvents = typeof EVENT.CLIENT.HEARTBEAT
export type MemberEvents = typeof EVENT.MEMBER.LIST | typeof EVENT.MEMBER.CONNECTED | typeof EVENT.MEMBER.DISCONNECTED
export type SignalEvents =
+6 -1
View File
@@ -134,7 +134,7 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
/////////////////////////////
// System Events
/////////////////////////////
protected [EVENT.SYSTEM.INIT]({ implicit_hosting, locks, file_transfer }: SystemInitPayload) {
protected [EVENT.SYSTEM.INIT]({ implicit_hosting, locks, file_transfer, heartbeat_interval }: SystemInitPayload) {
this.$accessor.remote.setImplicitHosting(implicit_hosting)
this.$accessor.remote.setFileTransfer(file_transfer)
@@ -145,6 +145,11 @@ export class NekoClient extends BaseClient implements EventEmitter<NekoEvents> {
id: locks[resource],
})
}
if (heartbeat_interval > 0) {
if (this._ws_heartbeat) clearInterval(this._ws_heartbeat)
this._ws_heartbeat = window.setInterval(() => this.sendMessage(EVENT.CLIENT.HEARTBEAT), heartbeat_interval * 1000)
}
}
protected [EVENT.SYSTEM.DISCONNECT]({ message }: SystemMessagePayload) {
+1
View File
@@ -61,6 +61,7 @@ export interface SystemInitPayload {
implicit_hosting: boolean
locks: Record<string, string>
file_transfer: boolean
heartbeat_interval: number
}
// system/disconnect