-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
145 lines (114 loc) · 3.05 KB
/
client.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package networkwebsockets
import (
"errors"
"net/http"
"time"
"github.com/richtr/websocket"
)
type ClientMessageHandler struct {
client *Client
}
func (handler *ClientMessageHandler) Read(buf []byte) error {
client := handler.client
if client == nil {
return errors.New("ClientMessageHandler requires an attached Client object")
}
message, err := decodeWireMessage(buf)
if err != nil {
return err
}
switch message.Action {
case "connect":
client.Connect <- message
case "disconnect":
client.Disconnect <- message
case "status":
client.Status <- message
case "broadcast":
client.Broadcast <- message
case "message":
client.Message <- message
}
return nil
}
func (handler *ClientMessageHandler) Write(buf []byte) error {
client := handler.client
if client == nil {
return errors.New("ClientMessageHandler requires an attached Client object")
}
if !client.transport.open {
return errors.New("Client is not active")
}
client.transport.conn.SetWriteDeadline(time.Now().Add(writeWait))
client.transport.conn.WriteMessage(websocket.TextMessage, buf)
return nil
}
func Dial(urlStr string, handler MessageHandler) (*Client, *http.Response, error) {
d := websocket.Dialer{
HandshakeTimeout: 10 * time.Second,
ReadBufferSize: 8192,
WriteBufferSize: 8192,
}
wsConn, httpResp, err := d.Dial(urlStr, nil)
if err != nil {
return nil, nil, err
}
transport := NewTransport(wsConn, handler)
client := NewClient(transport)
// Setup default client message handler if one has not been provided
if client.transport.handler == nil {
client.transport.handler = &ClientMessageHandler{client}
}
// Start read/write pumps
client.Start()
return client, httpResp, nil
}
// Client interface
type Client struct {
// Underlying transport object
transport *Transport
// incoming message channels
Status chan WireMessage
Connect chan WireMessage
Disconnect chan WireMessage
Message chan WireMessage
Broadcast chan WireMessage
}
func NewClient(transport *Transport) *Client {
client := &Client{
transport: transport,
Status: make(chan WireMessage, 255),
Connect: make(chan WireMessage, 255),
Disconnect: make(chan WireMessage, 255),
Message: make(chan WireMessage, 255),
Broadcast: make(chan WireMessage, 255),
}
return client
}
func (client *Client) Start() {
// Start read/write pumps
client.transport.Start()
}
func (client *Client) Stop() {
// Stop read/write pumps
client.transport.Stop()
}
// Default Client Message Handler Helper functions
func (client *Client) SendBroadcastData(data string) {
if wireData, err := encodeWireMessage("broadcast", "", "", data); err == nil {
client.transport.Write(wireData)
}
}
func (client *Client) SendMessageData(data string, targetId string) {
if targetId == "" {
return
}
if wireData, err := encodeWireMessage("message", "", targetId, data); err == nil {
client.transport.Write(wireData)
}
}
func (client *Client) SendStatusRequest() {
if wireData, err := encodeWireMessage("status", "", "", ""); err == nil {
client.transport.Write(wireData)
}
}