-
Notifications
You must be signed in to change notification settings - Fork 87
/
websocket.go
109 lines (101 loc) · 2.76 KB
/
websocket.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package turnpike
import (
"crypto/tls"
"fmt"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
type websocketPeer struct {
conn *websocket.Conn
serializer Serializer
messages chan Message
payloadType int
closed bool
sendMutex sync.Mutex
}
func NewWebsocketPeer(serialization Serialization, url string, requestHeader http.Header, tlscfg *tls.Config, dial DialFunc) (Peer, error) {
switch serialization {
case JSON:
return newWebsocketPeer(url, requestHeader, jsonWebsocketProtocol,
new(JSONSerializer), websocket.TextMessage, tlscfg, dial,
)
case MSGPACK:
return newWebsocketPeer(url, requestHeader, msgpackWebsocketProtocol,
new(MessagePackSerializer), websocket.BinaryMessage, tlscfg, dial,
)
default:
return nil, fmt.Errorf("Unsupported serialization: %v", serialization)
}
}
func newWebsocketPeer(url string, reqHeader http.Header, protocol string, serializer Serializer, payloadType int, tlscfg *tls.Config, dial DialFunc) (Peer, error) {
dialer := websocket.Dialer{
Subprotocols: []string{protocol},
TLSClientConfig: tlscfg,
Proxy: http.ProxyFromEnvironment,
NetDial: dial,
}
conn, _, err := dialer.Dial(url, reqHeader)
if err != nil {
return nil, err
}
ep := &websocketPeer{
conn: conn,
messages: make(chan Message, 10),
serializer: serializer,
payloadType: payloadType,
}
go ep.run()
return ep, nil
}
// TODO: make this just add the message to a channel so we don't block
func (ep *websocketPeer) Send(msg Message) error {
b, err := ep.serializer.Serialize(msg)
if err != nil {
return err
}
ep.sendMutex.Lock()
defer ep.sendMutex.Unlock()
return ep.conn.WriteMessage(ep.payloadType, b)
}
func (ep *websocketPeer) Receive() <-chan Message {
return ep.messages
}
func (ep *websocketPeer) Close() error {
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "goodbye")
err := ep.conn.WriteControl(websocket.CloseMessage, closeMsg, time.Now().Add(5*time.Second))
if err != nil {
log.Println("error sending close message:", err)
}
ep.closed = true
return ep.conn.Close()
}
func (ep *websocketPeer) run() {
for {
// TODO: use conn.NextMessage() and stream
// TODO: do something different based on binary/text frames
if msgType, b, err := ep.conn.ReadMessage(); err != nil {
if ep.closed {
log.Println("peer connection closed")
} else {
log.Println("error reading from peer:", err)
ep.conn.Close()
}
close(ep.messages)
break
} else if msgType == websocket.CloseMessage {
ep.conn.Close()
close(ep.messages)
break
} else {
msg, err := ep.serializer.Deserialize(b)
if err != nil {
log.Println("error deserializing peer message:", err)
// TODO: handle error
} else {
ep.messages <- msg
}
}
}
}