remove session from websocket peer.

This commit is contained in:
Miroslav Šedivý 2021-09-02 20:30:50 +02:00
parent f18ae2b972
commit 1dab0bd859
2 changed files with 27 additions and 28 deletions

View File

@ -156,43 +156,26 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
return return
} }
// create new peer
peer := newPeer(connection)
session, err := manager.sessions.Authenticate(r) session, err := manager.sessions.Authenticate(r)
if err != nil { if err != nil {
manager.logger.Warn().Err(err).Msg("authentication failed") manager.logger.Warn().Err(err).Msg("authentication failed")
// TODO: Better handling... peer.Send(
raw, err := json.Marshal(message.SystemDisconnect{ event.SYSTEM_DISCONNECT,
Message: err.Error(), message.SystemDisconnect{
}) Message: err.Error(),
if err != nil {
manager.logger.Err(err).Msg("failed to create disconnect event")
}
err = connection.WriteJSON(
types.WebSocketMessage{
Event: event.SYSTEM_DISCONNECT,
Payload: raw,
}) })
if err != nil { peer.Destroy()
manager.logger.Err(err).Msg("failed to send disconnect event")
}
err = connection.Close()
manager.logger.Err(err).Msg("connection closed")
return return
} }
// use session id with defeault logger context // add session id to all log messages
logger := manager.logger.With().Str("session_id", session.ID()).Logger() logger := manager.logger.With().Str("session_id", session.ID()).Logger()
peer.setSessionID(session.ID())
// create new peer
peer := &WebSocketPeerCtx{
logger: logger,
session: session,
connection: connection,
}
if !session.Profile().CanConnect { if !session.Profile().CanConnect {
logger.Warn().Msg("connection disabled") logger.Warn().Msg("connection disabled")

View File

@ -6,6 +6,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/event" "demodesk/neko/internal/types/event"
@ -15,10 +16,25 @@ import (
type WebSocketPeerCtx struct { type WebSocketPeerCtx struct {
mu sync.Mutex mu sync.Mutex
logger zerolog.Logger logger zerolog.Logger
session types.Session
connection *websocket.Conn connection *websocket.Conn
} }
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()
}
func (peer *WebSocketPeerCtx) Send(event string, payload interface{}) { func (peer *WebSocketPeerCtx) Send(event string, payload interface{}) {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()