This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
293 lines (238 loc) · 5.98 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
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 eventide
import (
"bytes"
"compress/zlib"
"encoding/json"
"errors"
"fmt"
"io"
"time"
"github.com/gorilla/websocket"
"github.com/thefakequake/eventide/discord"
)
// Establishes a WebSocket connection with Discord
func (c *Client) Connect() error {
var err error
if err != nil {
return errors.New("websocket connection is already open")
}
if c.gateway == "" {
c.gateway, err = c.GetGateway()
if err != nil {
return fmt.Errorf("error fetching gateway url: %s", err)
}
c.log(LogInfo, "fetched gateway url")
}
defer func() {
if err != nil {
c.Disconnect()
}
}()
c.Lock()
defer c.Unlock()
c.ws, _, err = websocket.DefaultDialer.Dial(c.gateway, nil)
if err != nil {
return err
}
c.log(LogInfo, "established connection with gateway")
var payload discord.GatewayPayload[discord.Hello]
if err = c.ws.ReadJSON(&payload); err != nil {
return err
}
if payload.Op != 10 {
c.log(LogWarn, "expected opcode 10 hello, instead received opcode %d", payload.Op)
} else {
c.log(LogInfo, "received opcode 10 hello")
}
if c.sessionID == "" {
if err = c.identify(); err != nil {
return fmt.Errorf("error sending identify payload: %s", err)
}
c.log(LogInfo, "sent identify payload")
} else {
resumePayload := discord.GatewayPayload[discord.Resume]{
Op: 6,
Data: discord.Resume{
Token: c.token,
SessionID: c.sessionID,
Seq: c.lastSequence,
},
}
if err = c.ws.WriteJSON(&resumePayload); err != nil {
return fmt.Errorf("error sending resume payload: %s", err)
}
c.log(LogInfo, "sent resume payload")
}
t, dat, err := c.ws.ReadMessage()
if err != nil {
return err
}
firstEvent, err := c.parsePayload(t, dat)
if err != nil {
return err
}
if firstEvent.Type != "READY" && firstEvent.Type != "RESUMED" {
c.log(LogWarn, "expected READY or RESUMED packet, instead received: %s", firstEvent.Type)
}
go c.runHandlers(firstEvent)
go c.heartbeatLoop(payload.Data.HeartbeatInterval)
go c.listenEvent()
return nil
}
func (c *Client) identify() error {
payload := discord.GatewayPayload[discord.Identify]{
Op: 2,
Data: discord.Identify{
Token: c.token,
Intents: c.intents,
Properties: c.identifyProperties,
Compress: c.compress,
},
}
c.wsLock.Lock()
err := c.ws.WriteJSON(&payload)
c.wsLock.Unlock()
return err
}
func (c *Client) listenClose() chan int {
closeListener := make(chan int, 1)
c.listenerLock.Lock()
c.closeListeners = append(c.closeListeners, closeListener)
c.listenerLock.Unlock()
return closeListener
}
func (c *Client) sendHeartbeat() error {
c.RLock()
seq := &c.lastSequence
c.RUnlock()
heartbeat := discord.GatewayPayload[*int64]{
Op: 1,
Data: seq,
}
c.wsLock.Lock()
err := c.ws.WriteJSON(&heartbeat)
c.wsLock.Unlock()
c.log(LogDebug, "sent heartbeat")
return err
}
func (c *Client) heartbeatLoop(interval time.Duration) {
c.log(LogInfo, "started heartbeat goroutine")
listening := c.listenClose()
ticker := time.NewTicker(interval * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
case <-listening:
return
}
if err := c.sendHeartbeat(); err != nil {
c.log(LogError, "error sending heartbeat to gateway: %s", err)
}
}
}
func (c *Client) listenEvent() {
ws := c.ws
listening := c.listenClose()
c.log(LogInfo, "started event listening goroutine")
for {
t, dat, err := ws.ReadMessage()
if err != nil {
// check if connection wasn't closed manually by checking if connection is the same
c.RLock()
sameConn := ws == c.ws
c.RUnlock()
if sameConn {
c.log(LogWarn, "error reading websocket message: %s", err)
c.Reconnect()
}
return
}
payload, err := c.parsePayload(t, dat)
if err != nil {
c.log(LogError, "%s", err)
continue
}
c.log(LogDebug, "op: %d seq: %d t: %s d: %s\n", payload.Op, payload.Sequence, payload.Type, payload.Data)
switch payload.Op {
case 0:
c.Lock()
c.lastSequence = payload.Sequence
c.Unlock()
go c.runHandlers(payload)
case 1:
c.log(LogInfo, "sending heartbeat in response to ping")
if err := c.sendHeartbeat(); err != nil {
c.log(LogError, "error sending heartbeat: %s", err)
}
case 9:
c.log(LogInfo, "sending identify payload in response to invalid session")
if err := c.identify(); err != nil {
c.log(LogError, "error sending identify payload: %s", err)
}
case 11:
c.log(LogDebug, "received heartbeat ack")
}
select {
case <-listening:
return
default:
continue
}
}
}
func (c *Client) parsePayload(messageType int, dat []byte) (discord.GatewayPayload[json.RawMessage], error) {
var reader io.Reader
var payload discord.GatewayPayload[json.RawMessage]
reader = bytes.NewBuffer(dat)
if messageType == websocket.BinaryMessage {
res, err := zlib.NewReader(reader)
if err != nil {
return payload, fmt.Errorf("error decompressing gateway event: %s", err)
}
defer func() {
if err := res.Close(); err != nil {
c.log(LogError, "error closing zlib: %s", err)
}
}()
reader = res
}
if err := json.NewDecoder(reader).Decode(&payload); err != nil {
return payload, fmt.Errorf("error decoding event JSON: %s", err)
}
return payload, nil
}
func (c *Client) Disconnect() error {
return c.closeWebsocket(websocket.CloseNormalClosure)
}
func (c *Client) Reconnect() error {
if err := c.closeWebsocket(websocket.CloseServiceRestart); err != nil {
return err
}
return c.Connect()
}
func (c *Client) closeWebsocket(code int) error {
var err error
c.Lock()
defer c.Unlock()
c.listenerLock.Lock()
closeListeners := c.closeListeners
c.closeListeners = []chan int{}
c.listenerLock.Unlock()
for _, l := range closeListeners {
l <- code
}
if c.ws != nil {
c.log(LogInfo, "sending closing frame")
c.wsLock.Lock()
c.ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(code, ""))
c.wsLock.Unlock()
c.log(LogInfo, "closing gateway websocket")
err = c.ws.Close()
if err != nil {
c.log(LogInfo, "error closing websocket: %s", err)
}
c.ws = nil
}
return err
}