2020-11-26 06:36:33 +13:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-09-25 01:15:59 +12:00
|
|
|
"errors"
|
2020-11-26 06:36:33 +13:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2021-08-30 04:23:58 +12:00
|
|
|
"github.com/rs/zerolog"
|
2021-09-03 06:30:50 +12:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-11-26 06:36:33 +13:00
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/types"
|
|
|
|
"github.com/demodesk/neko/pkg/types/event"
|
|
|
|
"github.com/demodesk/neko/pkg/types/message"
|
2023-03-31 23:02:19 +13:00
|
|
|
"github.com/demodesk/neko/pkg/utils"
|
2020-11-26 06:36:33 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebSocketPeerCtx struct {
|
2021-02-15 05:11:21 +13:00
|
|
|
mu sync.Mutex
|
2021-08-30 04:23:58 +12:00
|
|
|
logger zerolog.Logger
|
2020-11-26 06:36:33 +13:00
|
|
|
connection *websocket.Conn
|
|
|
|
}
|
|
|
|
|
2021-09-03 06:30:50 +12:00
|
|
|
func newPeer(connection *websocket.Conn) *WebSocketPeerCtx {
|
|
|
|
logger := log.With().
|
|
|
|
Str("module", "websocket").
|
|
|
|
Str("submodule", "peer").
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
return &WebSocketPeerCtx{
|
|
|
|
logger: logger,
|
|
|
|
connection: connection,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (peer *WebSocketPeerCtx) setSessionID(sessionId string) {
|
|
|
|
peer.logger = peer.logger.With().Str("session_id", sessionId).Logger()
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:20:20 +12:00
|
|
|
func (peer *WebSocketPeerCtx) Send(event string, payload any) {
|
2021-02-15 05:11:21 +13:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
2020-11-26 06:36:33 +13:00
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
if peer.connection == nil {
|
2021-09-02 07:11:07 +12:00
|
|
|
return
|
2020-11-26 06:36:33 +13:00
|
|
|
}
|
|
|
|
|
2021-09-02 07:11:07 +12:00
|
|
|
raw, err := json.Marshal(payload)
|
2020-11-26 06:36:33 +13:00
|
|
|
if err != nil {
|
2021-09-02 09:10:06 +12:00
|
|
|
peer.logger.Err(err).Str("event", event).Msg("message marshalling has failed")
|
2021-09-02 07:11:07 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = peer.connection.WriteJSON(types.WebSocketMessage{
|
|
|
|
Event: event,
|
|
|
|
Payload: raw,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-02 09:10:06 +12:00
|
|
|
peer.logger.Err(err).Str("event", event).Msg("send message error")
|
2021-09-02 07:11:07 +12:00
|
|
|
return
|
2020-11-26 06:36:33 +13:00
|
|
|
}
|
|
|
|
|
2023-03-31 23:02:19 +13: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-26 06:36:33 +13:00
|
|
|
}
|
|
|
|
|
2021-09-25 01:15:59 +12:00
|
|
|
func (peer *WebSocketPeerCtx) Ping() error {
|
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
|
|
|
|
|
|
|
if peer.connection == nil {
|
|
|
|
return errors.New("peer connection not found")
|
|
|
|
}
|
|
|
|
|
2022-11-12 05:58:54 +13:00
|
|
|
// application level heartbeat
|
|
|
|
if err := peer.connection.WriteJSON(types.WebSocketMessage{
|
|
|
|
Event: event.SYSTEM_HEARTBEAT,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-25 01:15:59 +12:00
|
|
|
return peer.connection.WriteMessage(websocket.PingMessage, nil)
|
|
|
|
}
|
|
|
|
|
2021-09-03 07:52:23 +12:00
|
|
|
func (peer *WebSocketPeerCtx) Destroy(reason string) {
|
2021-09-02 07:11:07 +12:00
|
|
|
peer.Send(
|
|
|
|
event.SYSTEM_DISCONNECT,
|
2021-03-26 02:08:26 +13:00
|
|
|
message.SystemDisconnect{
|
2021-09-03 07:52:23 +12:00
|
|
|
Message: reason,
|
2021-09-02 07:11:07 +12:00
|
|
|
})
|
2021-03-26 02:08:26 +13:00
|
|
|
|
2021-09-18 10:56:03 +12:00
|
|
|
peer.mu.Lock()
|
|
|
|
defer peer.mu.Unlock()
|
2021-03-26 02:08:26 +13:00
|
|
|
|
2021-09-18 10:56:03 +12:00
|
|
|
if peer.connection != nil {
|
|
|
|
err := peer.connection.Close()
|
|
|
|
peer.logger.Err(err).Msg("peer connection destroyed")
|
|
|
|
peer.connection = nil
|
|
|
|
}
|
2020-11-26 06:36:33 +13:00
|
|
|
}
|