-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.go
71 lines (56 loc) · 1.38 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
package lanyard
import (
"encoding/json"
"log"
"strings"
"time"
"github.com/sacOO7/gowebsocket"
)
const (
WS_URL = "wss://api.lanyard.rest/socket"
PING_PERIOD = 30 * time.Second
)
type WSClient struct {
socket gowebsocket.Socket
ticker *time.Ticker
}
func (client WSClient) Destroy() {
if client.ticker != nil {
client.ticker.Stop()
}
client.socket.Close()
}
func (client WSClient) ping() {
client.ticker = time.NewTicker(PING_PERIOD)
defer client.ticker.Stop()
for ; ; <-client.ticker.C {
client.socket.SendText("{\"op\":3}")
}
}
func CreateWS(userId string, presenceUpdate func(data *LanyardData)) WSClient {
client := WSClient {
socket: gowebsocket.New(WS_URL),
}
client.socket.OnConnected = func(socket gowebsocket.Socket) {
client.socket.SendText("{\"op\":2,\"d\":{\"subscribe_to_id\":\"" + userId + "\"}}")
go client.ping()
};
client.socket.OnConnectError = func(err error, socket gowebsocket.Socket) {
log.Println("An error occured while connecting to Lanyard websocket server", err)
client.Destroy()
};
client.socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
if strings.Contains(message, "heartbeat_interval") {
return
}
var data LanyardWSResponse
err := json.Unmarshal([]byte(message), &data)
if err != nil {
client.Destroy()
return
}
presenceUpdate(data.D)
};
client.socket.Connect()
return client
}