/
connection.go
99 lines (77 loc) · 1.96 KB
/
connection.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
package connection
import (
"encoding/json"
"log"
"github.com/bpaksi/MusicMage/server/tools/messagebus"
"github.com/gorilla/websocket"
)
type clientPayload struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload,omitempty"`
}
// Connection ...
type Connection struct {
id int64
writeChannel chan clientPayload
socket *websocket.Conn
}
func newConnection(id int64, socket *websocket.Conn) *Connection {
client := Connection{
id: id,
socket: socket,
writeChannel: make(chan clientPayload),
}
stop := make(chan bool, 0)
go client.writeData(stop)
go client.readData(stop)
log.Printf("Connection opened: %d", id)
return &client
}
// Send ...
func (client *Connection) Send(msgType string, payload interface{}) {
raw, err := json.Marshal(payload)
if err != nil {
log.Println("Error: " + err.Error())
return
}
msg := clientPayload{
Type: msgType,
Payload: raw,
}
client.writeChannel <- msg
}
func (client *Connection) readData(disconnected chan<- bool) {
// defer log.Println("Connection.readData exited")
for {
var payload clientPayload
err := client.socket.ReadJSON(&payload)
if err != nil {
log.Println("Connection.readData error: " + err.Error())
disconnected <- true
return
}
// log.Printf("Connection.readData %s, %s", payload.Type, payload.Payload)
if !MessageWhitelist.Contains(payload.Type) {
log.Printf("Non whitelisted message from Client: %d, %s\n", client.id, payload.Type)
continue
}
msg := messagebus.Message{
Type: payload.Type,
Payload: payload.Payload,
ClientID: client.id,
}
messagebus.PublishVerbose(msg)
}
}
func (client *Connection) writeData(disconnected <-chan bool) {
// defer log.Println("Connection.writeData exited")
for {
select {
case msg := <-client.writeChannel:
client.socket.WriteJSON(msg)
case <-disconnected:
messagebus.Publish("CLIENT_CONNECTION_CLOSED", client.id)
return
}
}
}