Allow transports to define their own ping mechanism

This commit is contained in:
Wichert Akkerman
2019-10-15 20:56:11 +02:00
committed by Mickaël Rémond
parent d0f2b492ac
commit 36e153f981
4 changed files with 25 additions and 2 deletions

View File

@@ -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)
}