diff --git a/internal/session/manager.go b/internal/session/manager.go index 97c70b2b..b2f1790e 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -122,7 +122,7 @@ func (manager *SessionManagerCtx) Delete(id string) error { manager.sessionsMu.Unlock() if session.State().IsConnected { - session.GetWebSocketPeer().Destroy() + session.GetWebSocketPeer().Destroy("session deleted") } if session.State().IsWatching { diff --git a/internal/session/session.go b/internal/session/session.go index 41ac12ff..b4359d4e 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -35,7 +35,7 @@ func (session *SessionCtx) profileChanged() { } if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected { - session.websocketPeer.Destroy() + session.websocketPeer.Destroy("profile changed") } } @@ -53,7 +53,7 @@ func (session *SessionCtx) IsHost() bool { func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) { if session.websocketPeer != nil { - session.websocketPeer.Destroy() + session.websocketPeer.Destroy("connection replaced") } session.websocketPeer = websocketPeer diff --git a/internal/types/websocket.go b/internal/types/websocket.go index 6bdf0a3c..885a0c41 100644 --- a/internal/types/websocket.go +++ b/internal/types/websocket.go @@ -16,7 +16,7 @@ type CheckOrigin func(r *http.Request) bool type WebSocketPeer interface { Send(event string, payload interface{}) - Destroy() + Destroy(reason string) } type WebSocketManager interface { diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index b44e37e5..26bb9b99 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -162,14 +162,7 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque session, err := manager.sessions.Authenticate(r) if err != nil { manager.logger.Warn().Err(err).Msg("authentication failed") - - peer.Send( - event.SYSTEM_DISCONNECT, - message.SystemDisconnect{ - Message: err.Error(), - }) - - peer.Destroy() + peer.Destroy(err.Error()) return } @@ -179,14 +172,7 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque if !session.Profile().CanConnect { logger.Warn().Msg("connection disabled") - - peer.Send( - event.SYSTEM_DISCONNECT, - message.SystemDisconnect{ - Message: "connection disabled", - }) - - peer.Destroy() + peer.Destroy("connection disabled") return } @@ -194,13 +180,7 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque logger.Warn().Msg("already connected") if !manager.sessions.MercifulReconnect() { - peer.Send( - event.SYSTEM_DISCONNECT, - message.SystemDisconnect{ - Message: "already connected", - }) - - peer.Destroy() + peer.Destroy("already connected") return } diff --git a/internal/websocket/peer.go b/internal/websocket/peer.go index 7ac5790c..a449a601 100644 --- a/internal/websocket/peer.go +++ b/internal/websocket/peer.go @@ -66,7 +66,7 @@ func (peer *WebSocketPeerCtx) Send(event string, payload interface{}) { Msg("sending message to client") } -func (peer *WebSocketPeerCtx) Destroy() { +func (peer *WebSocketPeerCtx) Destroy(reason string) { peer.mu.Lock() defer peer.mu.Unlock() @@ -77,7 +77,7 @@ func (peer *WebSocketPeerCtx) Destroy() { peer.Send( event.SYSTEM_DISCONNECT, message.SystemDisconnect{ - Message: "connection destroyed", + Message: reason, }) if err := peer.connection.Close(); err != nil {