-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
293 lines (249 loc) · 6.87 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Package pusher provides a client library for Pusher. It connects to the WebSocket
// interface, allows subscribing to channels, and receiving events.
package pusher
import (
"encoding/json"
"log"
s "strings"
"time"
)
const (
// Default WebSocket endpoint
defaultScheme = "wss"
defaultHost = "ws.pusherapp.com"
defaultPort = "443"
)
// Client responsibilities:
//
// * Connecting (via Connection)
// * Reconnecting on disconnect
// * Decoding and encoding events
// * Managing channel subscriptions
//
type Client struct {
ClientConfig
bindings chanbindings
globalBindings map[*func(string, string, interface{})]struct{}
*connection
// Internal channels
_subscribe chan *Channel
_unsubscribe chan string
_disconnect chan bool
Connected bool
Channels []*Channel
UserData Member
Debug bool
}
type ClientConfig struct {
Scheme string
Host string
Port string
Key string
Secret string
AuthEndpoint string
}
type Event struct {
Name string `json:"event"`
Channel string `json:"channel"`
Data string `json:"data"`
}
type evBind map[string]chan (interface{})
type chanbindings map[string]evBind
// New creates a new Pusher client with given Pusher application key
func New(key string) *Client {
config := ClientConfig{
Scheme: defaultScheme,
Host: defaultHost,
Port: defaultPort,
Key: key,
}
return NewWithConfig(config)
}
// NewWithConfig allows creating a new Pusher client which connects to a custom endpoint
func NewWithConfig(c ClientConfig) *Client {
client := &Client{
ClientConfig: c,
bindings: make(chanbindings),
globalBindings: map[*func(string, string, interface{})]struct{}{},
_subscribe: make(chan *Channel),
_unsubscribe: make(chan string),
_disconnect: make(chan bool),
Channels: make([]*Channel, 0),
}
go client.runLoop()
return client
}
func (self *Client) Disconnect() {
self._disconnect <- true
}
// Subscribe subscribes the client to the channel
func (self *Client) Subscribe(channel string) (ch *Channel) {
for _, ch := range self.Channels {
if ch.Name == channel {
self._subscribe <- ch
return ch
}
}
ch = &Channel{Name: channel, bindings: &self.bindings}
self._subscribe <- ch
return
}
// UnSubscribe unsubscribes the client from the channel
func (self *Client) Unsubscribe(channel string) {
self._unsubscribe <- channel
}
func (self *Client) runLoop() {
onMessage := make(chan string)
onClose := make(chan bool)
onDisconnect := make(chan bool)
callbacks := &connCallbacks{
onMessage: onMessage,
onClose: onClose,
onDisconnect: onDisconnect,
}
// Connect when this timer fires - initially fire immediately
connectTimer := time.NewTimer(0 * time.Second)
for {
select {
case <-connectTimer.C:
// Connect to Pusher
if c, err := dial(self.ClientConfig, callbacks); err != nil {
if Debug {
log.Print("Failed to connect: ", err)
}
connectTimer.Reset(1 * time.Second)
} else {
if Debug {
log.Print("Connection opened")
}
self.connection = c
}
case c := <-self._subscribe:
if self.Connected {
self.subscribe(c)
}
self.Channels = append(self.Channels, c)
case c := <-self._unsubscribe:
for _, ch := range self.Channels {
if ch.Name == c {
if self.connection != nil {
self.unsubscribe(ch)
}
}
}
case message := <-onMessage:
event, _ := decode([]byte(message))
if Debug {
log.Printf("Received: channel=%v event=%v data=%v", event.Channel, event.Name, event.Data)
}
switch event.Name {
case "pusher:connection_established":
connectionEstablishedData := make(map[string]string)
json.Unmarshal([]byte(event.Data), &connectionEstablishedData)
self.connection.socketID = connectionEstablishedData["socket_id"]
self.Connected = true
for _, ch := range self.Channels {
if !ch.Subscribed {
self.subscribe(ch)
}
}
case "pusher_internal:subscription_succeeded":
for _, ch := range self.Channels {
if ch.Name == event.Channel {
ch.Subscribed = true
ch.connection = self.connection
if ch.isPresence() {
members, _ := unmarshalledMembers(event.Data, self.UserData.UserId)
self.triggerEventCallback(event.Channel, "pusher:subscription_succeeded", members)
}
}
}
case "pusher_internal:member_added":
member, _ := unmarshalledMember(event.Data)
self.triggerEventCallback(event.Channel, "pusher:member_added", member)
case "pusher_internal:member_removed":
member, _ := unmarshalledMember(event.Data)
self.triggerEventCallback(event.Channel, "pusher:member_removed", member)
default:
self.triggerEventCallback(event.Channel, event.Name, event.Data)
}
case <-self._disconnect:
for _, ch := range self.Channels {
ch.Subscribed = false
}
self.connection.ws.Close()
self.connection = nil
connectTimer.Stop()
onDisconnect <- true
return
case <-onClose:
if Debug {
log.Print("Connection closed, will reconnect in 1s")
}
for _, ch := range self.Channels {
ch.Subscribed = false
}
self.connection = nil
connectTimer.Reset(1 * time.Second)
}
}
}
func (self *Client) triggerEventCallback(channel, event string, data interface{}) {
if self.bindings[channel] != nil {
if binding := self.bindings[channel][event]; binding != nil {
binding <- data
}
}
for handler, _ := range self.globalBindings {
(*handler)(channel, event, data)
}
}
func encode(event string, data interface{}, channel *string) (message []byte, err error) {
payload := map[string]interface{}{
"event": event,
"data": data,
}
if channel != nil {
payload["channel"] = channel
}
message, err = json.Marshal(payload)
return
}
func decode(message []byte) (event Event, err error) {
err = json.Unmarshal(message, &event)
return
}
func (self *Client) subscribe(channel *Channel) {
payload := map[string]string{
"channel": channel.Name,
}
isPrivate := channel.isPrivate()
isPresence := channel.isPresence()
if isPrivate || isPresence {
stringToSign := (s.Join([]string{self.connection.socketID, channel.Name}, ":"))
if isPresence {
var _userData []byte
_userData, err := json.Marshal(self.UserData)
if err != nil {
panic(err)
}
userData := string(_userData)
payload["channel_data"] = userData
stringToSign = s.Join([]string{stringToSign, userData}, ":")
}
authString := createAuthString(self.Key, self.ClientConfig.Secret, stringToSign)
payload["auth"] = authString
}
message, _ := encode("pusher:subscribe", payload, nil)
self.connection.send(message)
}
func (self *Client) unsubscribe(channel *Channel) {
message, _ := encode("pusher:unsubscribe", map[string]string{
"channel": channel.Name,
}, nil)
self.connection.send(message)
channel.Subscribed = false
}
func (self *Client) BindGlobal(callback func(string, string, interface{})) {
self.globalBindings[&callback] = struct{}{}
}