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
/
gateway.go
204 lines (170 loc) · 4.02 KB
/
gateway.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
package eventide
import (
"encoding/json"
"errors"
"time"
"github.com/gorilla/websocket"
)
type Op[T any] struct {
Code int `json:"op"`
Sequence int64 `json:"s,omitempty"`
Type string `json:"t,omitempty"`
Data T `json:"d,omitempty"`
}
type OpHello struct {
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
}
type OpIdentify struct {
Token string `json:"token"`
Intents Intents `json:"intents"`
Properties IdentifyProperties `json:"properties"`
}
type IdentifyProperties struct {
Os string `json:"$os"`
Browser string `json:"$browser"`
Device string `json:"$device"`
}
// 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")
}
c.log(LogInfo, "fetching gateway url")
url, err := c.GetGatewayURL()
if err != nil {
return err
}
c.Lock()
defer c.Unlock()
c.log(LogInfo, "connecting to the gateway")
c.ws, _, err = websocket.DefaultDialer.Dial(url, nil)
if err != nil {
return err
}
defer func() {
if err != nil {
c.Disconnect()
}
}()
var op Op[OpHello]
if err = c.ws.ReadJSON(&op); err != nil {
return err
}
if op.Code != 10 {
c.log(LogWarn, "expected opcode 10 hello, instead received opcode %d", op.Code)
} else {
c.log(LogInfo, "received opcode 10 hello")
}
c.log(LogInfo, "sending identify payload")
if err = c.identify(); err != nil {
return err
}
c.listening = make(chan any)
go c.heartbeatLoop(op.Data.HeartbeatInterval)
go c.listenEvent()
return nil
}
func (c *Client) identify() error {
payload := Op[OpIdentify]{
Code: 2,
Data: OpIdentify{
Token: c.token,
Intents: IntentsAll,
Properties: c.Identify,
},
}
err := c.ws.WriteJSON(&payload)
return err
}
func (c *Client) sendHeartbeat() error {
c.RLock()
seq := &c.lastSequence
c.RUnlock()
heartbeat := Op[*int64]{
Code: 1,
Data: seq,
}
c.wsLock.Lock()
err := c.ws.WriteJSON(&heartbeat)
c.wsLock.Unlock()
return err
}
func (c *Client) heartbeatLoop(interval time.Duration) {
c.log(LogInfo, "started heartbeat goroutine")
for {
time.Sleep(interval * time.Millisecond)
if err := c.sendHeartbeat(); err != nil {
c.log(LogError, "error sending heartbeat to gateway: %s", err)
}
}
}
func (c *Client) listenEvent() {
c.log(LogInfo, "started event listening goroutine")
for {
var op Op[json.RawMessage]
if err := c.ws.ReadJSON(&op); err != nil {
c.log(LogWarn, "error reading websocket message: %s", err)
c.RLock()
if c.ws != nil {
c.Disconnect()
}
c.RUnlock()
return
}
c.log(LogDebug, "op: %d seq: %d t: %s d: %s", op.Code, op.Sequence, op.Type, op.Data)
switch op.Code {
case 0:
c.Lock()
c.lastSequence = op.Sequence
c.Unlock()
go c.runHandlers(op)
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 <-c.listening:
return
default:
continue
}
}
}
func (c *Client) Disconnect() error {
return c.closeWebsocket(websocket.CloseNormalClosure)
}
func (c *Client) closeWebsocket(code int) error {
var err error
if c.listening != nil {
c.log(LogInfo, "closing listening channel")
close(c.listening)
c.listening = nil
}
c.Lock()
defer c.Unlock()
if c.ws != nil {
c.log(LogInfo, "sending closing frame")
c.wsLock.Lock()
err = c.ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(code, ""))
c.wsLock.Unlock()
if err != nil {
c.log(LogInfo, "error closing websocket: %s", err)
}
c.log(LogInfo, "closing gateway websocket")
err = c.ws.Close()
if err != nil {
c.log(LogInfo, "error closing websocket: %s", err)
}
}
return err
}