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
}
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPeer, connected bool) {
if websocketPeer != session.websocketPeer {
return
}
session.state.IsConnected = connected
if connected {

View File

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

View File

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