mirror of
https://github.com/m1k1o/neko.git
synced 2026-11-03 00:11:48 -06:00
feat(general): add heartbeat to websocket control (#460)
* chore: ignore .idea * feat: add heartbeat event to websocket * feat: add HeartbeatInterval to websocket config * feat: send HeartbeatInterval in SYSTEM_INIT message * feat: add heartbeat logic * fix: client events is not in WebSocketEvents type
This commit is contained in:
@@ -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
|
||||
@@ -80,6 +81,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 = () => {}
|
||||
|
||||
@@ -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 =
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -61,6 +61,7 @@ export interface SystemInitPayload {
|
||||
implicit_hosting: boolean
|
||||
locks: Record<string, string>
|
||||
file_transfer: boolean
|
||||
heartbeat_interval: number
|
||||
}
|
||||
|
||||
// system/disconnect
|
||||
|
||||
Reference in New Issue
Block a user