-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_transport.go
69 lines (56 loc) · 1.67 KB
/
websocket_transport.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
package connector
import (
"github.com/gorilla/websocket"
"net"
"time"
)
const (
TransportProtocolTypeWebsocket TransportProtocolType = "websocket"
)
// websocketTransport is a wrapper struct over websocket connection to fit Transport
// interface so Client will accept it.
type websocketTransport struct {
conn *websocket.Conn
encoding EncodingType
opts *Options
}
func newWebsocketTransport(conn *websocket.Conn, encoding EncodingType, opts *Options) *websocketTransport {
transport := &websocketTransport{
conn: conn,
encoding: encoding,
opts: opts,
}
return transport
}
// ProtocolType returns the protocol type of the transport.
func (t *websocketTransport) ProtocolType() TransportProtocolType {
return TransportProtocolTypeWebsocket
}
// NetConn returns the internal net.Conn of the connection.
func (t *websocketTransport) NetConn() net.Conn {
return t.conn.UnderlyingConn()
}
func (t *websocketTransport) Read() ([]byte, error) {
_, message, err := t.conn.ReadMessage()
return message, err
}
// Write data to websocket.Conn.
func (t *websocketTransport) Write(data []byte) error {
messageType := websocket.TextMessage
if t.encoding == EncodingTypeProtobuf {
messageType = websocket.BinaryMessage
}
// SetWriteDeadline should be set per WriteMessage call.
if t.opts.WriteTimeout > 0 {
_ = t.conn.SetWriteDeadline(time.Now().Add(t.opts.WriteTimeout))
}
if err := t.conn.WriteMessage(messageType, data); err != nil {
return err
}
return nil
}
// Close closes the underlying network connection.
// It can be called concurrently, and it's OK to call Close more than once.
func (t *websocketTransport) Close() error {
return t.conn.Close()
}