90 lines
1.9 KiB
Go
Raw Normal View History

2020-11-25 18:36:33 +01:00
package websocket
import (
"encoding/json"
"sync"
"github.com/gorilla/websocket"
2021-08-29 18:23:58 +02:00
"github.com/rs/zerolog"
2020-11-25 18:36:33 +01:00
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/utils"
2020-11-25 18:36:33 +01:00
)
type WebSocketPeerCtx struct {
2021-02-14 17:11:21 +01:00
mu sync.Mutex
2021-08-29 18:23:58 +02:00
logger zerolog.Logger
2020-11-25 18:36:33 +01:00
connection *websocket.Conn
}
func newPeer(logger zerolog.Logger, connection *websocket.Conn) *WebSocketPeerCtx {
2021-09-02 20:30:50 +02:00
return &WebSocketPeerCtx{
logger: logger.With().Str("submodule", "peer").Logger(),
2021-09-02 20:30:50 +02:00
connection: connection,
}
}
func (peer *WebSocketPeerCtx) Send(event string, payload any) {
2021-02-14 17:11:21 +01:00
peer.mu.Lock()
defer peer.mu.Unlock()
2020-11-25 18:36:33 +01:00
2021-09-01 21:11:07 +02:00
raw, err := json.Marshal(payload)
2020-11-25 18:36:33 +01:00
if err != nil {
2021-09-01 23:10:06 +02:00
peer.logger.Err(err).Str("event", event).Msg("message marshalling has failed")
2021-09-01 21:11:07 +02:00
return
}
err = peer.connection.WriteJSON(types.WebSocketMessage{
Event: event,
Payload: raw,
})
if err != nil {
2021-09-01 23:10:06 +02:00
peer.logger.Err(err).Str("event", event).Msg("send message error")
2021-09-01 21:11:07 +02:00
return
2020-11-25 18:36:33 +01:00
}
// log events if not ignored
if ok, _ := utils.ArrayIn(event, nologEvents); !ok {
if len(raw) > maxPayloadLogLength {
raw = []byte("<truncated>")
}
peer.logger.Debug().
Str("address", peer.connection.RemoteAddr().String()).
Str("event", event).
Str("payload", string(raw)).
Msg("sending message to client")
}
2020-11-25 18:36:33 +01:00
}
2021-09-24 15:15:59 +02:00
func (peer *WebSocketPeerCtx) Ping() error {
peer.mu.Lock()
defer peer.mu.Unlock()
2022-11-11 17:58:54 +01:00
// application level heartbeat
if err := peer.connection.WriteJSON(types.WebSocketMessage{
Event: event.SYSTEM_HEARTBEAT,
}); err != nil {
return err
}
2021-09-24 15:15:59 +02:00
return peer.connection.WriteMessage(websocket.PingMessage, nil)
}
2021-09-02 21:52:23 +02:00
func (peer *WebSocketPeerCtx) Destroy(reason string) {
2021-09-01 21:11:07 +02:00
peer.Send(
event.SYSTEM_DISCONNECT,
2021-03-25 14:08:26 +01:00
message.SystemDisconnect{
2021-09-02 21:52:23 +02:00
Message: reason,
2021-09-01 21:11:07 +02:00
})
2021-03-25 14:08:26 +01:00
2021-09-18 00:56:03 +02:00
peer.mu.Lock()
defer peer.mu.Unlock()
2021-03-25 14:08:26 +01:00
err := peer.connection.Close()
peer.logger.Err(err).Msg("peer connection destroyed")
2020-11-25 18:36:33 +01:00
}