-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
503 lines (434 loc) · 13 KB
/
main.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package fibersocket
import (
"context"
"errors"
"sync"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"github.com/google/uuid"
)
type Event struct {
// Message type
mType int
// Message data
data []byte
// Message send retries when error
retries int
}
type FiberSocket struct {
Mutex sync.RWMutex
ws *websocket.Conn
IsConnectionAlive bool
Done chan struct{}
EventQueue chan Event
EventAttributes map[string]interface{}
UUID uuid.UUID
Locals func(key string) interface{}
Params func(key string, defaultValue ...string) string
Query func(key string, defaultValue ...string) string
Cookies func(key string, defaultValue ...string) string
}
type Fiber interface {
IsAlive() bool
GetUUID() uuid.UUID
SetUUID(uuid uuid.UUID)
SetAttribute(key string, attribute interface{})
GetAttribute(key string) interface{}
GetIntAttribute(key string) int
GetStringAttribute(key string) string
EmitToMany(uuids []uuid.UUID, message []byte, mType ...int)
EmitTo(uuid uuid.UUID, message []byte, mType ...int) error
Broadcast(message []byte, except bool, mType ...int)
Fire(event string, data []byte)
Emit(message []byte, mType ...int)
Close()
pong(ctx context.Context)
write(messageType int, messageBytes []byte)
run()
read(ctx context.Context)
disconnected(err error)
createUUID() uuid.UUID
fireEvent(event string, data []byte, error error)
}
// EventPayload holds all information about an event
type EventPayload struct {
Fiber *FiberSocket
Name string
SocketUUID uuid.UUID
SocketAttributes map[string]interface{}
Error error
Data []byte
}
type eventCallback func(payload *EventPayload)
// Source @url:https://github.com/gorilla/websocket/blob/master/conn.go#L61
// The message types are defined in RFC 6455, section 11.8.
const (
// TextMessage denotes a text data message. The text message payload is
// interpreted as UTF-8 encoded text data.
TextMessage = 1
// BinaryMessage denotes a binary data message.
BinaryMessage = 2
// CloseMessage denotes a close control message. The optional message
// payload contains a numeric code and text. Use the FormatCloseMessage
// function to format a close message payload.
CloseMessage = 8
// PingMessage denotes a ping control message. The optional message payload
// is UTF-8 encoded text.
PingMessage = 9
// PongMessage denotes a pong control message. The optional message payload
// is UTF-8 encoded text.
PongMessage = 10
)
// Supported event list
const (
// EventMessage Fired when a Text/Binary message is received
EventMessage = "message"
// EventPing More details here:
// @url https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Pings_and_Pongs_The_Heartbeat_of_WebSockets
EventPing = "ping"
EventPong = "pong"
// EventDisconnect Fired on disconnection
// The error provided in disconnection event
// is defined in RFC 6455, section 11.7.
// @url https://github.com/gofiber/websocket/blob/cd4720c435de415b864d975a9ca23a47eaf081ef/websocket.go#L192
EventDisconnect = "disconnect"
// EventConnect Fired on first connection
EventConnect = "connect"
// EventClose Fired when the connection is actively closed from the server
EventClose = "close"
// EventError Fired when some error appears useful also for debugging websockets
EventError = "error"
)
// support event id
const (
EventMessageId = "1"
EventPingId = "2"
EventPongId = "3"
// EventDisconnect Fired on disconnection
// The error provided in disconnection event
// is defined in RFC 6455, section 11.7.
// @url https://github.com/gofiber/websocket/blob/cd4720c435de415b864d975a9ca23a47eaf081ef/websocket.go#L192
EventDisconnectId = "4"
// EventConnect Fired on first connection
EventConnectId = "5"
// EventClose Fired when the connection is actively closed from the server
EventCloseId = "6"
// EventError Fired when some error appears useful also for debugging websockets
EventErrorId = "7"
)
/****************************************
Variables
****************************************/
var (
// ErrorInvalidConnection The addressed ws connection is not available anymore
// error data is the uuid of that connection
ErrorInvalidConnection = errors.New("message cannot be delivered invalid/gone connection")
// ErrorUUIDDuplication The UUID already exists in the pool
ErrorUUIDDuplication = errors.New("UUID already exists in the available connections pool")
)
// Holds a map of all event callbacks that will be executed in the future safely
var listeners = SafeListeners{
list: make(map[string][]eventCallback),
}
// Pool with the active connections
var pool = safePool{
conn: make(map[uuid.UUID]Fiber),
}
var (
PongTimeout = 1 * time.Second
// RetrySendTimeout retry after 20 ms if there is an error
RetrySendTimeout = 20 * time.Millisecond
//MaxSendRetry define max retries if there are socket issues
MaxSendRetry = 5
// ReadTimeout Instead of reading in a for loop, try to avoid full CPU load taking some pause
ReadTimeout = 10 * time.Millisecond
)
// Creates a new instance of GoFiberWebSocket
func New(callback func(socket *FiberSocket)) func(*fiber.Ctx) error {
return websocket.New(func(c *websocket.Conn) {
socket := &FiberSocket{
ws: c,
EventQueue: make(chan Event, 100),
Done: make(chan struct{}, 1),
EventAttributes: make(map[string]interface{}),
IsConnectionAlive: true,
// GoFiber Context Support
Locals: func(key string) interface{} {
return c.Locals(key)
},
Params: func(key string, defaultValue ...string) string {
return c.Params(key, defaultValue...)
},
Query: func(key string, defaultValue ...string) string {
return c.Query(key, defaultValue...)
},
Cookies: func(key string, defaultValue ...string) string {
return c.Cookies(key, defaultValue...)
},
}
// Generate uuid
socket.UUID = socket.createUUID()
// register the connection into the pool
pool.New(socket)
// execute the callback of the socket initialization
callback(socket)
socket.fireEvent(EventConnect, nil, nil)
// Run the loop for the given connection
socket.run()
})
}
// Handles Internal Event Emission
func (fs *FiberSocket) fireEvent(event string, data []byte, error error) {
callbacks := listeners.get(event)
// run the callbacks for the specified event in the safe listeners
for _, callback := range callbacks {
callback(&EventPayload{
Fiber: fs,
Name: event,
SocketUUID: fs.UUID,
SocketAttributes: fs.EventAttributes,
Data: data,
Error: error,
})
}
}
// Creates a UUID for the ws conn using google/uuid based on RFC 4122 and DCE 1.1
func (socket *FiberSocket) createUUID() uuid.UUID {
return uuid.New()
}
func (socket *FiberSocket) SetUUID(uuid uuid.UUID) {
socket.Mutex.RLock()
defer socket.Mutex.RUnlock()
socket.UUID = uuid
}
// Get the UUID of the *FiberSocket safely
func (socket *FiberSocket) GetUUID() uuid.UUID {
socket.Mutex.Lock()
defer socket.Mutex.Unlock()
return socket.UUID
}
// Send out message queue
func (fs *FiberSocket) send(ctx context.Context) {
for {
select {
case message := <-fs.EventQueue:
if !fs.hasConn() {
if message.retries <= MaxSendRetry {
// retry without blocking the sending thread
go func() {
time.Sleep(RetrySendTimeout)
message.retries = message.retries + 1
fs.EventQueue <- message
}()
}
continue
}
fs.Mutex.RLock()
err := fs.ws.WriteMessage(message.mType, message.data)
fs.Mutex.RUnlock()
if err != nil {
fs.disconnected(err)
}
case <-ctx.Done():
return
}
}
}
// SetAttribute Set a specific attribute for the specific socket connection
func (fs *FiberSocket) SetAttribute(key string, attribute interface{}) {
fs.Mutex.Lock()
defer fs.Mutex.Unlock()
fs.EventAttributes[key] = attribute
}
// GetAttribute Get a specific attribute from the socket attributes
func (fs *FiberSocket) GetAttribute(key string) interface{} {
fs.Mutex.RLock()
defer fs.Mutex.RUnlock()
value, ok := fs.EventAttributes[key]
if ok {
return value
}
return nil
}
// GetIntAttribute Convenience method to retrieve an attribute as an int.
// Will panic if attribute is not an int.
func (fs *FiberSocket) GetIntAttribute(key string) int {
fs.Mutex.RLock()
defer fs.Mutex.RUnlock()
value, ok := fs.EventAttributes[key]
if ok {
return value.(int)
}
return 0
}
// GetStringAttribute Convenience method to retrieve an attribute as a string.
// Will panic if attribute is not an int.
func (fs *FiberSocket) GetStringAttribute(key string) string {
fs.Mutex.RLock()
defer fs.Mutex.RUnlock()
value, ok := fs.EventAttributes[key]
if ok {
return value.(string)
}
return ""
}
// Start Pong/Read/Write functions
// Needs to be blocking, otherwise the connection would close.
func (fs *FiberSocket) run() {
ctx, cancelFunc := context.WithCancel(context.Background())
go fs.pong(ctx)
go fs.read(ctx)
go fs.send(ctx)
<-fs.Done // block until one event is sent to the done channel
cancelFunc()
}
// pong writes a control message to the client
func (fs *FiberSocket) pong(ctx context.Context) {
timeoutTicker := time.Tick(PongTimeout)
for {
select {
case <-timeoutTicker:
fs.write(PongMessage, []byte{})
case <-ctx.Done():
return
}
}
}
func (fs *FiberSocket) hasConn() bool {
fs.Mutex.RLock()
defer fs.Mutex.RUnlock()
return fs.ws.Conn != nil
}
// Add in message queue
func (fs *FiberSocket) write(messageType int, messageBytes []byte) {
fs.EventQueue <- Event{
mType: messageType,
data: messageBytes,
retries: 0,
}
}
func (fs *FiberSocket) IsAlive() bool {
fs.Mutex.RLock()
defer fs.Mutex.RUnlock()
return fs.IsConnectionAlive
}
func (fs *FiberSocket) setAlive(alive bool) {
fs.Mutex.Lock()
defer fs.Mutex.Unlock()
fs.IsConnectionAlive = alive
}
// When the connection closes, disconnected method
func (fs *FiberSocket) disconnected(err error) {
fs.fireEvent(EventDisconnect, nil, err)
// may be called multiple times from different go routines
if fs.IsAlive() {
close(fs.Done)
}
fs.setAlive(false)
// Fire error event if the connection is
// disconnected by an error
if err != nil {
fs.fireEvent(EventError, nil, err)
}
// Remove the socket from the pool
pool.Delete(fs.UUID)
}
// Listen for incoming messages
// and filter by message type
func (fs *FiberSocket) read(ctx context.Context) {
timeoutTicker := time.Tick(ReadTimeout)
for {
select {
case <-timeoutTicker:
if !fs.hasConn() {
continue
}
fs.Mutex.RLock()
mtype, msg, err := fs.ws.ReadMessage()
fs.Mutex.RUnlock()
if mtype == PingMessage {
fs.fireEvent(EventPing, nil, nil)
continue
}
if mtype == PongMessage {
fs.fireEvent(EventPong, nil, nil)
continue
}
if mtype == CloseMessage {
fs.disconnected(nil)
return
}
if err != nil {
fs.disconnected(err)
return
}
// We have a message and we fire the message event
fs.fireEvent(EventMessage, msg, nil)
case <-ctx.Done():
return
}
}
}
// Handles event event emission to a specific connection socket
func (fs *FiberSocket) EmitTo(uuid uuid.UUID, message []byte, mType ...int) error {
if !pool.DoesContain(uuid) || !pool.Get(uuid).IsAlive() {
fs.fireEvent(EventError, uuid[:], ErrorInvalidConnection)
return ErrorInvalidConnection
}
pool.Get(uuid).Emit(message, mType...)
return nil
}
// EmitToList Emit the message to a specific socket uuids list
func (fs *FiberSocket) EmitToMany(uuids []uuid.UUID, message []byte, mType ...int) {
for _, uuid := range uuids {
err := fs.EmitTo(uuid, message, mType...)
if err != nil {
fs.fireEvent(EventError, message, err)
}
}
}
// Close Actively close the connection from the server
func (fs *FiberSocket) Close() {
fs.write(CloseMessage, []byte("Connection closed"))
fs.fireEvent(EventClose, nil, nil)
}
// Emits a message to the client connection
func (fs *FiberSocket) Emit(message []byte, mType ...int) {
t := TextMessage
if len(mType) > 0 {
t = mType[0]
}
fs.write(t, message)
}
// Adds listener callback to an event into the safe listeners list
func On(event string, callback eventCallback) {
listeners.set(event, callback)
}
// Broadcast message to all the active connections
// except avoid broadcasting the message to itself
func (fs *FiberSocket) Broadcast(message []byte, except bool, mType ...int) {
for uuid := range pool.GetAll() {
if except && fs.UUID == uuid {
continue
}
err := fs.EmitTo(uuid, message, mType...)
if err != nil {
fs.fireEvent(EventError, message, err)
}
}
}
// Fire custom event
func (fs *FiberSocket) Fire(event string, data []byte) {
fs.fireEvent(event, data, nil)
}
// Fires event on all connections.
func fireGlobalEvent(event string, eventId *string, data []byte, error error) {
for _, fs := range pool.GetAll() {
fs.fireEvent(event, data, error)
}
}
// Fire custom event on all connections
func Fire(event string, eventId *string, data []byte) {
fireGlobalEvent(event, eventId, data, nil)
}