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
}
// create new peer
peer := newPeer(connection)
session, err := manager.sessions.Authenticate(r)
if err != nil {
manager.logger.Warn().Err(err).Msg("authentication failed")
// TODO: Better handling...
raw, err := json.Marshal(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,
peer.Send(
event.SYSTEM_DISCONNECT,
message.SystemDisconnect{
Message: err.Error(),
})
if err != nil {
manager.logger.Err(err).Msg("failed to send disconnect event")
}
err = connection.Close()
manager.logger.Err(err).Msg("connection closed")
peer.Destroy()
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()
// create new peer
peer := &WebSocketPeerCtx{
logger: logger,
session: session,
connection: connection,
}
peer.setSessionID(session.ID())
if !session.Profile().CanConnect {
logger.Warn().Msg("connection disabled")

View File

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