mirror of
https://github.com/FluuxIO/go-xmpp.git
synced 2025-11-01 08:53:42 -07:00
Allow transports to define their own ping mechanism
This commit is contained in:
committed by
Mickaël Rémond
parent
d0f2b492ac
commit
36e153f981
@@ -9,6 +9,8 @@ import (
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
const pingTimeout = time.Duration(5) * time.Second
|
||||
|
||||
type WebsocketTransport struct {
|
||||
Config TransportConfiguration
|
||||
wsConn *websocket.Conn
|
||||
@@ -46,6 +48,14 @@ func (t WebsocketTransport) IsSecure() bool {
|
||||
return strings.HasPrefix(t.Config.Address, "wss:")
|
||||
}
|
||||
|
||||
func (t WebsocketTransport) Ping() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), pingTimeout)
|
||||
defer cancel()
|
||||
// Note that we do not use wsConn.Ping(), because not all websocket servers
|
||||
// (ejabberd for example) implement ping frames
|
||||
return t.wsConn.Write(ctx, websocket.MessageText, []byte(" "))
|
||||
}
|
||||
|
||||
func (t WebsocketTransport) Read(p []byte) (n int, err error) {
|
||||
return t.netConn.Read(p)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user