forked from AbiosGaming/go-sdk-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
192 lines (158 loc) · 5.38 KB
/
push.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
package abios
import (
"encoding/json"
"fmt"
"log"
"net/url"
"time"
"github.com/gobuffalo/uuid"
"github.com/gorilla/websocket"
. "github.com/PatronGG/abios-go-sdk/structs"
)
// Custom status codes sent by the server for the 'close' command.
// The websocket standard (RFC6455) allocates the
// 4000-4999 range to application specific status codes.
const (
CloseMissingAccessToken = 4000 // Missing access token in ws setup request
CloseInvalidAccessToken = 4001 // Invalid access token in ws setup request
CloseNotAuthorized = 4002 // Client account does not have access to the push API
CloseMaxNumSubscribers = 4003 // Max number of concurrent subscribers connected for client id
CloseMaxNumSubscriptions = 4004 // Max number of registered subscriptions exist for client id
CloseInvalidReconnectToken = 4005 // Invalid reconnect token in ws setup request
CloseMissingSubscriptionID = 4006 // Missing subscription id in ws setup request
CloseUnknownSubscriptionID = 4007 // The supplied subscriber id in ws setup request does not exist in server
CloseInternalError = 4500 // Unspecified error due to problem in server
)
/*
func (a *client) UpdateSubscription(id int, sub Subscription) (Subscription, error) {
}
func (a *client) PushServiceConfig() ([]byte, error) {
}
*/
func (a *client) PushServiceConnect(subscriptionID uuid.UUID) error {
params := make(Parameters)
params.Set("access_token", a.oauth.AccessToken)
params.Set("subscription_id", subscriptionID.String())
if a.reconnectToken != uuid.Nil {
params.Set("reconnect_token", a.reconnectToken.String())
}
u, err := url.Parse(wsBaseUrl)
if err != nil {
return err
}
u.RawQuery = params.encode()
log.Printf("[INFO] Dialing to socket '%v'\n", u.String())
var dialer *websocket.Dialer
conn, res, err := dialer.Dial(u.String(), nil)
if err == websocket.ErrBadHandshake {
log.Printf("[ERROR]: Failed to connect to WS url. Handshake status='%d'\n", res.StatusCode)
return err
} else if err != nil {
log.Printf("[ERROR]: Failed to connect to WS url. Error='%s'\n", err.Error())
return err
}
a.wsConn = conn
return nil
}
func (a *client) PushServiceInit(subscriptionID uuid.UUID) (chan SeriesMessage, chan error) {
errors := make(chan error, 1)
series := make(chan SeriesMessage, 1)
if err := a.PushServiceConnect(subscriptionID); err != nil {
errors <- err
return series, errors
}
initMsg, err := a.handleInitMessage(subscriptionID)
if err != nil {
errors <- err
return series, errors
}
a.reconnectToken = initMsg.ReconnectToken
go a.keepAliveLoop(errors)
go a.messageReadLoop(subscriptionID, series, errors)
return series, errors
}
func (a *client) handleInitMessage(subscriptionID uuid.UUID) (InitResponseMessage, error) {
var m InitResponseMessage
_, message, err := a.wsConn.ReadMessage()
if closeErr, ok := err.(*websocket.CloseError); ok {
var errMsg string
switch closeErr.Code {
case CloseUnknownSubscriptionID:
errMsg = fmt.Sprintf("Subscription ID '%s' is not registered on server", subscriptionID)
case CloseMissingSubscriptionID:
errMsg = "Missing subscription ID or name in setup request"
case CloseMaxNumSubscribers:
errMsg = "The max number of concurrent subscribers for the account has been exceeded"
case CloseMaxNumSubscriptions:
errMsg = "The max number of registered subscriptions for the account has been exceeded"
case CloseInternalError:
errMsg = "Unknown server error"
default:
errMsg = fmt.Sprintf("Server sent unrecognized error code %d", closeErr.Code)
}
log.Printf("[ERROR]: Server closed connection: %s\n", errMsg)
return m, err
} else if err != nil {
// Websocket read encountered some other error, we won't try to recover
log.Printf("[ERROR]: Failed to read `init' message. Error='%s'\n", err.Error())
return m, err
}
json.Unmarshal(message, &m)
return m, nil
}
func (a *client) messageReadLoop(subscriptionID uuid.UUID, series chan<- SeriesMessage, errors chan<- error) {
for {
_, message, err := a.wsConn.ReadMessage()
if closeErr, ok := err.(*websocket.CloseError); ok {
log.Printf("[INFO]: Websocket was closed, starting reconnect loop. Reason='%s'\n", closeErr.Error())
authErr := a.authenticate()
if authErr != nil {
errors <- fmt.Errorf("%v", authErr)
return
}
err = a.PushServiceConnect(subscriptionID)
if err != nil {
errors <- err
return
}
continue
} else if err != nil {
log.Printf("[ERROR]: Failed to read message. Error='%s'\n", err.Error())
errors <- err
return
}
// sanity check
var m PushMessage
err = json.Unmarshal(message, &m)
if err != nil {
log.Printf("[ERROR]: Failed to unmarshal to message struct. Error='%s', Message='%s'\n", err.Error(), message)
errors <- err
continue
}
switch m.Channel {
case "series":
var s SeriesMessage
err = json.Unmarshal(message, &s)
if err != nil {
log.Printf("[ERROR]: Failed to unmarshal to message struct. Error='%s', Message='%s'\n", err.Error(), message)
errors <- err
continue
}
s.Raw = message
series <- s
}
}
}
func (a *client) keepAliveLoop(errors chan<- error) {
for {
time.Sleep(time.Second * 30)
if a.wsConn != nil {
err := a.wsConn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(3*time.Second))
if err != nil {
log.Printf("[ERROR]: Failed to send Ping message. Error='%s'\n", err.Error())
errors <- err
continue
}
}
}
}