-
Notifications
You must be signed in to change notification settings - Fork 82
/
conn_nhoory.go
60 lines (48 loc) · 1.5 KB
/
conn_nhoory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package stream
import (
"context"
"net/http"
"net/url"
"time"
"nhooyr.io/websocket"
)
type nhooyrWebsocketConn struct {
conn *websocket.Conn
msgType websocket.MessageType
}
// newNhooyrWebsocketConn creates a new nhooyr websocket connection
func newNhooyrWebsocketConn(ctx context.Context, u url.URL) (conn, error) {
ctxWithTimeout, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
conn, _, err := websocket.Dial(ctxWithTimeout, u.String(), &websocket.DialOptions{
CompressionMode: websocket.CompressionContextTakeover,
HTTPHeader: http.Header{
"Content-Type": []string{"application/msgpack"},
},
})
return &nhooyrWebsocketConn{
conn: conn,
msgType: websocket.MessageBinary,
}, err
}
// close closes the websocket connection
func (c *nhooyrWebsocketConn) close() error {
return c.conn.Close(websocket.StatusNormalClosure, "")
}
// ping sends a ping to the client
func (c *nhooyrWebsocketConn) ping(ctx context.Context) error {
pingCtx, cancel := context.WithTimeout(ctx, pongWait)
defer cancel()
return c.conn.Ping(pingCtx)
}
// readMessage blocks until it reads a single message
func (c *nhooyrWebsocketConn) readMessage(ctx context.Context) (data []byte, err error) {
_, data, err = c.conn.Read(ctx)
return data, err
}
// writeMessage writes a single message
func (c *nhooyrWebsocketConn) writeMessage(ctx context.Context, data []byte) error {
writeCtx, cancel := context.WithTimeout(ctx, writeWait)
defer cancel()
return c.conn.Write(writeCtx, c.msgType, data)
}