websocket connection change.

This commit is contained in:
Miroslav Šedivý 2021-03-23 15:38:37 +01:00
parent 89081d9887
commit da1d073846
3 changed files with 12 additions and 6 deletions

View File

@ -67,7 +67,11 @@ func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
session.websocketPeer = websocketPeer session.websocketPeer = websocketPeer
} }
func (session *SessionCtx) SetWebSocketConnected(connected bool) { func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPeer, connected bool) {
if websocketPeer != session.websocketPeer {
return
}
session.state.IsConnected = connected session.state.IsConnected = connected
if connected { if connected {

View File

@ -15,7 +15,7 @@ type Session interface {
// websocket // websocket
SetWebSocketPeer(websocketPeer WebSocketPeer) SetWebSocketPeer(websocketPeer WebSocketPeer)
SetWebSocketConnected(connected bool) SetWebSocketConnected(websocketPeer WebSocketPeer, connected bool)
Send(v interface{}) error Send(v interface{}) error
Disconnect(reason string) error Disconnect(reason string) error

View File

@ -207,11 +207,13 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
return return
} }
session.SetWebSocketPeer(&WebSocketPeerCtx{ peer := &WebSocketPeerCtx{
session: session, session: session,
manager: manager, manager: manager,
connection: connection, connection: connection,
}) }
session.SetWebSocketPeer(peer)
manager.logger. manager.logger.
Debug(). Debug().
@ -219,7 +221,7 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
Str("address", connection.RemoteAddr().String()). Str("address", connection.RemoteAddr().String()).
Msg("connection started") Msg("connection started")
session.SetWebSocketConnected(true) session.SetWebSocketConnected(peer, true)
defer func() { defer func() {
manager.logger. manager.logger.
@ -228,7 +230,7 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
Str("address", connection.RemoteAddr().String()). Str("address", connection.RemoteAddr().String()).
Msg("connection ended") Msg("connection ended")
session.SetWebSocketConnected(false) session.SetWebSocketConnected(peer, false)
}() }()
manager.handle(connection, session) manager.handle(connection, session)