mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
move websocket Ping to peer.
This commit is contained in:
parent
41d8fa7e22
commit
d91c0a9f7f
@ -16,6 +16,7 @@ type CheckOrigin func(r *http.Request) bool
|
|||||||
|
|
||||||
type WebSocketPeer interface {
|
type WebSocketPeer interface {
|
||||||
Send(event string, payload interface{})
|
Send(event string, payload interface{})
|
||||||
|
Ping() error
|
||||||
Destroy(reason string)
|
Destroy(reason string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,10 +215,10 @@ func (manager *WebSocketManagerCtx) connect(connection *websocket.Conn, r *http.
|
|||||||
session.SetWebSocketConnected(peer, false)
|
session.SetWebSocketConnected(peer, false)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
manager.handle(connection, session)
|
manager.handle(connection, peer, session)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.Session) {
|
func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, peer types.WebSocketPeer, session types.Session) {
|
||||||
// add session id to logger context
|
// add session id to logger context
|
||||||
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
|
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
|
||||||
|
|
||||||
@ -281,11 +281,10 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, session t
|
|||||||
case <-cancel:
|
case <-cancel:
|
||||||
return
|
return
|
||||||
case <-manager.shutdown:
|
case <-manager.shutdown:
|
||||||
err := connection.Close()
|
peer.Destroy("connection shutdown")
|
||||||
manager.logger.Err(err).Msg("connection shutdown")
|
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if err := connection.WriteMessage(websocket.PingMessage, nil); err != nil {
|
if err := peer.Ping(); err != nil {
|
||||||
logger.Err(err).Msg("ping message has failed")
|
logger.Err(err).Msg("ping message has failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package websocket
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@ -66,6 +67,17 @@ func (peer *WebSocketPeerCtx) Send(event string, payload interface{}) {
|
|||||||
Msg("sending message to client")
|
Msg("sending message to client")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (peer *WebSocketPeerCtx) Ping() error {
|
||||||
|
peer.mu.Lock()
|
||||||
|
defer peer.mu.Unlock()
|
||||||
|
|
||||||
|
if peer.connection == nil {
|
||||||
|
return errors.New("peer connection not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return peer.connection.WriteMessage(websocket.PingMessage, nil)
|
||||||
|
}
|
||||||
|
|
||||||
func (peer *WebSocketPeerCtx) Destroy(reason string) {
|
func (peer *WebSocketPeerCtx) Destroy(reason string) {
|
||||||
peer.Send(
|
peer.Send(
|
||||||
event.SYSTEM_DISCONNECT,
|
event.SYSTEM_DISCONNECT,
|
||||||
|
Loading…
Reference in New Issue
Block a user