-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgobwas_test.go
More file actions
55 lines (41 loc) · 1.32 KB
/
gobwas_test.go
File metadata and controls
55 lines (41 loc) · 1.32 KB
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
package wszero_test
import (
"context"
"net"
"net/http"
"time"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
)
type GobwasConn struct {
nc net.Conn
state ws.State
}
func (c *GobwasConn) ReadMessage() (int, []byte, error) {
data, op, err := wsutil.ReadData(c.nc, c.state)
return int(op), data, err
}
func (c *GobwasConn) WriteMessage(t int, d []byte) error {
return wsutil.WriteMessage(c.nc, c.state, ws.OpCode(t), d)
}
func (c *GobwasConn) WriteControl(t int, d []byte, _ time.Time) error { return c.WriteMessage(t, d) }
func (c *GobwasConn) NetConn() net.Conn { return c.nc }
func (c *GobwasConn) SetReadLimit(int64) {}
func (c *GobwasConn) Close() error { return c.nc.Close() }
type GobwasUpgrader struct{}
func (u *GobwasUpgrader) Upgrade(w http.ResponseWriter, r *http.Request, _ http.Header) (*GobwasConn, error) {
nc, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
return nil, err
}
return &GobwasConn{nc, ws.StateServerSide}, nil
}
type GobwasDialer ws.Dialer
var GobwasDefaultDialer = (*GobwasDialer)(&ws.DefaultDialer)
func (d *GobwasDialer) DialContext(ctx context.Context, urlStr string, _ http.Header) (*GobwasConn, *http.Response, error) {
nc, _, _, err := (*ws.Dialer)(d).Dial(ctx, urlStr)
if err != nil {
return nil, nil, err
}
return &GobwasConn{nc, ws.StateClientSide}, nil, nil
}