send heartbeat with ping. (#16)

This commit is contained in:
Miroslav Šedivý 2022-11-11 17:58:54 +01:00 committed by GitHub
parent 482476489e
commit 2cb64d15a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 2 deletions

View File

@ -23,6 +23,16 @@ const pingPeriod = 10 * time.Second
// period for sending inactive cursor messages
const inactiveCursorsPeriod = 750 * time.Millisecond
// events that are not logged in debug mode
var nologEvents = []string{
// don't log twice
event.SYSTEM_LOGS,
// don't log heartbeat
event.SYSTEM_HEARTBEAT,
// don't log every cursor update
event.SESSION_CURSORS,
}
func New(
sessions types.SessionManager,
desktop types.DesktopManager,
@ -301,7 +311,8 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, peer type
break
}
if data.Event != event.SYSTEM_LOGS {
// log events if not ignored
if ok, _ := utils.ArrayIn(data.Event, nologEvents); !ok {
logger.Debug().
Str("address", connection.RemoteAddr().String()).
Str("event", data.Event).

View File

@ -75,6 +75,13 @@ func (peer *WebSocketPeerCtx) Ping() error {
return errors.New("peer connection not found")
}
// application level heartbeat
if err := peer.connection.WriteJSON(types.WebSocketMessage{
Event: event.SYSTEM_HEARTBEAT,
}); err != nil {
return err
}
return peer.connection.WriteMessage(websocket.PingMessage, nil)
}

View File

@ -6,6 +6,7 @@ const (
SYSTEM_SETTINGS = "system/settings"
SYSTEM_LOGS = "system/logs"
SYSTEM_DISCONNECT = "system/disconnect"
SYSTEM_HEARTBEAT = "system/heartbeat"
)
const (

View File

@ -7,7 +7,7 @@ import (
type WebSocketMessage struct {
Event string `json:"event"`
Payload json.RawMessage `json:"payload"`
Payload json.RawMessage `json:"payload,omitempty"`
}
type WebSocketHandler func(Session, WebSocketMessage) bool