-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
81 lines (75 loc) · 1.74 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
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { //always anser request no matter what the origin is
return true //always return true, so that every request is allowed
},
}
var websockets = newWsStore()
func handleWebsocket(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
websockets.addConn(c)
}
func handleInputEvent(event map[string]*json.RawMessage) {
var slotInt int
var slot uint
if err := json.Unmarshal(*event["slot"], &slotInt); err == nil {
slot = uint(slotInt)
} else {
return
}
//get the player for the slot
player := slotMap.getPlayer(slot)
if player == nil {
//player is nil and the slot number must be invalid
return
}
//get the "player" object from the json object
playEvent := make(map[string]*json.RawMessage)
err := json.Unmarshal(*event["player"], &playEvent)
if err != nil {
//no player object provided
return
}
//stop
if stop := unmarshalBool(playEvent, "stop"); stop != nil && *stop {
player.stop()
}
//playing
if playing := unmarshalBool(playEvent, "playing"); playing != nil {
if *playing {
player.resume()
} else {
player.pause()
}
}
//loop
if loop := unmarshalBool(playEvent, "loop"); loop != nil {
player.loop = *loop
}
//volume
var volume float64
if playEvent["volume"] != nil {
if err = json.Unmarshal(*playEvent["volume"], &volume); err == nil {
player.setVolume(volume)
}
}
}
func unmarshalBool(data map[string]*json.RawMessage, key string) *bool {
result := false
if data[key] == nil {
return nil
}
err := json.Unmarshal(*data[key], &result)
if err != nil {
return nil
}
return &result
}