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
//go:build !js
package wsconn
import (
"context"
"fmt"
"time"
"github.com/gorilla/websocket"
)
type Conn struct {
conn *websocket.Conn
}
func DialConfig(ctx context.Context, target string, cfg Config) (*Conn, error) {
dialer := &websocket.Dialer{
HandshakeTimeout: cfg.handshakeTimeout(),
TLSClientConfig: cfg.tlsConfig(),
}
if cfg.Dial != nil {
dialer.NetDialContext = cfg.Dial
}
conn, response, err := dialer.DialContext(ctx, target, nil)
if err != nil {
if response != nil {
return nil, &HandshakeError{StatusCode: response.StatusCode}
}
return nil, fmt.Errorf("wsconn: websocket dial: %w", err)
}
conn.SetReadLimit(MaxFrameBytes)
conn.SetPingHandler(func(data string) error {
_ = conn.SetReadDeadline(time.Now().Add(ReadTimeout))
return conn.WriteControl(websocket.PongMessage, []byte(data), time.Now().Add(PongWriteTimeout))
})
_ = conn.SetReadDeadline(time.Now().Add(ReadTimeout))
return &Conn{conn: conn}, nil
}
func (c *Conn) Read(ctx context.Context) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
_ = c.conn.SetReadDeadline(time.Now().Add(ReadTimeout))
stop := context.AfterFunc(ctx, func() { _ = c.conn.SetReadDeadline(time.Now()) })
defer stop()
_, data, err := c.conn.ReadMessage()
if err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return nil, ctxErr
}
return nil, err
}
return data, nil
}
func (c *Conn) Close() error {
return c.conn.Close()
}