-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_ws.go
65 lines (51 loc) · 1.37 KB
/
connection_ws.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
61
62
63
64
65
package jsonrpc
import (
"context"
"net/http"
"github.com/gorilla/websocket"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
)
type webSocketConnection struct {
conn *websocket.Conn
}
func (w *webSocketConnection) Write(data []byte) error {
return w.conn.WriteMessage(websocket.TextMessage, data)
}
func (w *webSocketConnection) Read() ([]byte, error) {
msgType, bytes, err := w.conn.ReadMessage()
if err != nil {
log.WithError(err).Error("read failure")
switch err.(type) {
case *websocket.CloseError:
// re-map error
return nil, ErrClosed
default:
return nil, err
}
}
if msgType != websocket.TextMessage {
return nil, errors.Errorf("expected text message type, received a writer for %v", msgType)
}
if err != nil {
return nil, errors.Annotate(err, "failed to read message")
}
return bytes, nil
}
func (w *webSocketConnection) Close() error {
return w.conn.Close()
}
type WebSocketDialer struct {
Url string
RequestHeader http.Header
// TODO expose more of the underlying ws options
}
func (w WebSocketDialer) Dial() (Connection, error) {
return w.DialContext(context.Background())
}
func (w WebSocketDialer) DialContext(ctx context.Context) (Connection, error) {
dialer := websocket.Dialer{}
wsConn, _, err := dialer.DialContext(ctx, w.Url, w.RequestHeader)
conn := webSocketConnection{conn: wsConn}
return &conn, err
}