-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
41 lines (32 loc) · 1 KB
/
message.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
package twitch
import (
"github.com/google/uuid"
"time"
)
const maxUUIDRetryCount = 1024 // This should never happen
// makeUniqueId generates a random UUID for message nonce
func makeUniqueId() string {
// uuid might fail sometimes so we try until we get something
// this can only happen if /dev/urandom returns a Try again
// that's why we wait one millis
msgId, err := uuid.NewUUID()
retries := 0
for err != nil && retries > maxUUIDRetryCount {
time.Sleep(time.Millisecond)
msgId, err = uuid.NewUUID()
retries++
}
if retries == maxUUIDRetryCount {
log.Error("ERROR: MAX UUID GENERATOR RETRY COUNT REACHED! SERVER IS BUGGY")
}
return msgId.String()
}
// makeMessage creates a twitch websocket message with the specified type and data
func makeMessage(msgType string, data map[string]interface{}) (msgId string, message map[string]interface{}) {
message = map[string]interface{}{
"type": msgType,
"nonce": makeUniqueId(),
"data": data,
}
return message["nonce"].(string), message
}