Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(general): add heartbeat to websocket control #460

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ bin

# Environment files
*.env

# Code Editors
.idea
6 changes: 6 additions & 0 deletions client/src/neko/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = () => {}
Expand Down
5 changes: 5 additions & 0 deletions client/src/neko/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const EVENT = {
DISCONNECT: 'system/disconnect',
ERROR: 'system/error',
},
CLIENT: {
HEARTBEAT: 'client/heartbeat'
},
SIGNAL: {
OFFER: 'signal/offer',
ANSWER: 'signal/answer',
Expand Down Expand Up @@ -69,6 +72,7 @@ export type Events = typeof EVENT

export type WebSocketEvents =
| SystemEvents
| ClientEvents
| ControlEvents
| MemberEvents
| SignalEvents
Expand All @@ -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 =
Expand Down
7 changes: 6 additions & 1 deletion client/src/neko/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions client/src/neko/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface SystemInitPayload {
implicit_hosting: boolean
locks: Record<string, string>
file_transfer: boolean
heartbeat_interval: number
}

// system/disconnect
Expand Down
9 changes: 9 additions & 0 deletions server/internal/config/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type WebSocket struct {

ControlProtection bool

HeartbeatInterval int

FileTransferEnabled bool
FileTransferPath string
}
Expand All @@ -39,6 +41,11 @@ func (WebSocket) Init(cmd *cobra.Command) error {
return err
}

cmd.PersistentFlags().Int("heartbeat_interval", 120, "heartbeat interval in seconds")
if err := viper.BindPFlag("heartbeat_interval", cmd.PersistentFlags().Lookup("heartbeat_interval")); err != nil {
return err
}

// File transfer

cmd.PersistentFlags().Bool("file_transfer_enabled", false, "enable file transfer feature")
Expand All @@ -61,6 +68,8 @@ func (s *WebSocket) Set() {

s.ControlProtection = viper.GetBool("control_protection")

s.HeartbeatInterval = viper.GetInt("heartbeat_interval")

s.FileTransferEnabled = viper.GetBool("file_transfer_enabled")
s.FileTransferPath = viper.GetString("file_transfer_path")
s.FileTransferPath = filepath.Clean(s.FileTransferPath)
Expand Down
4 changes: 4 additions & 0 deletions server/internal/types/event/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const (
SYSTEM_ERROR = "system/error"
)

const (
CLIENT_HEARTBEAT = "client/heartbeat"
)

const (
SIGNAL_OFFER = "signal/offer"
SIGNAL_ANSWER = "signal/answer"
Expand Down
9 changes: 5 additions & 4 deletions server/internal/types/message/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ type Message struct {
}

type SystemInit struct {
Event string `json:"event"`
ImplicitHosting bool `json:"implicit_hosting"`
Locks map[string]string `json:"locks"`
FileTransfer bool `json:"file_transfer"`
Event string `json:"event"`
ImplicitHosting bool `json:"implicit_hosting"`
Locks map[string]string `json:"locks"`
FileTransfer bool `json:"file_transfer"`
HeartbeatInterval int `json:"heartbeat_interval"`
}

type SystemMessage struct {
Expand Down
5 changes: 5 additions & 0 deletions server/internal/websocket/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (h *MessageHandler) Message(id string, raw []byte) error {
}

switch header.Event {
// Client Events
case event.CLIENT_HEARTBEAT:
// do nothing
return nil

// Signal Events
case event.SIGNAL_OFFER:
payload := &message.SignalOffer{}
Expand Down
11 changes: 6 additions & 5 deletions server/internal/websocket/handler/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"m1k1o/neko/internal/types/message"
)

func (h *MessageHandler) SessionCreated(id string, session types.Session) error {
func (h *MessageHandler) SessionCreated(id string, heartbeatInterval int, session types.Session) error {
// send sdp and id over to client
if err := h.signalProvide(id, session); err != nil {
return err
}

// send initialization information
if err := session.Send(message.SystemInit{
Event: event.SYSTEM_INIT,
ImplicitHosting: h.webrtc.ImplicitControl(),
Locks: h.state.AllLocked(),
FileTransfer: h.state.FileTransferEnabled(),
Event: event.SYSTEM_INIT,
ImplicitHosting: h.webrtc.ImplicitControl(),
Locks: h.state.AllLocked(),
FileTransfer: h.state.FileTransferEnabled(),
HeartbeatInterval: heartbeatInterval,
}); err != nil {
h.logger.Warn().Str("id", id).Err(err).Msgf("sending event %s has failed", event.SYSTEM_INIT)
return err
Expand Down
2 changes: 1 addition & 1 deletion server/internal/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (ws *WebSocketHandler) Start() {

switch e.Type {
case types.SESSION_CREATED:
if err := ws.handler.SessionCreated(e.Id, e.Session); err != nil {
if err := ws.handler.SessionCreated(e.Id, ws.conf.HeartbeatInterval, e.Session); err != nil {
ws.logger.Warn().Str("id", e.Id).Err(err).Msg("session created with and error")
} else {
ws.logger.Debug().Str("id", e.Id).Msg("session created")
Expand Down