mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
remove session from websocket peer.
This commit is contained in:
parent
f18ae2b972
commit
1dab0bd859
@ -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")
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user