-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsockets.go
186 lines (172 loc) · 5.44 KB
/
websockets.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
package main
import (
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/gorilla/websocket"
)
var (
clients = make(map[*websocket.Conn]string)
addrToNameMap = make(map[string]string)
mapMutex = sync.Mutex{}
connMutex = sync.Mutex{}
playing = make(map[string]bool)
)
func generateRandomName() string {
// Generate two random words
adjective := randomdata.Adjective()
noun := randomdata.Noun()
return fmt.Sprintf("%s-%s", adjective, noun)
}
func getClientName(remoteAddr string) string {
mapMutex.Lock()
defer mapMutex.Unlock()
// Check if the name already exists
name, exists := addrToNameMap[remoteAddr]
if !exists {
// Generate a new name if not exist
name = generateRandomName()
addrToNameMap[remoteAddr] = name
}
return name
}
func clearChannelRequests(channel string) {
defer func() {
if r := recover(); r != nil {
requests = nil
logger("Recovered from panic in clearChannelRequests: "+fmt.Sprintf("%v", r), logError, channel)
}
}()
for i := len(requests) - 1; i >= 0; i-- {
if i >= len(requests) {
continue
}
request := requests[i]
if request.Channel == channel {
requests = append(requests[:i], requests[i+1:]...)
}
}
}
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
channel := strings.ToLower(r.URL.Query().Get("channel"))
upgrader := websocket.Upgrader{}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logger("Error upgrading to WebSocket: "+err.Error(), logError, channel)
return
}
hash := r.URL.Query().Get("v")
currentHash, err := ComputeMD5("static/index.html")
if err != nil {
logger("Error computing hash for index.html: "+err.Error(), logError, channel)
return
}
clientName := getClientName(fmt.Sprintf("%p", conn))
if hash != currentHash {
logger(clientName+" connected with outdated version: "+hash+" (current: "+currentHash+")", logInfo, channel)
logger("Sending update message to "+clientName+": "+currentHash, logInfo, channel)
err := conn.WriteMessage(websocket.TextMessage, []byte("update "+currentHash))
if err != nil {
logger("Error sending update message to client: "+err.Error(), logError, channel)
}
conn.Close()
return
}
logger("Client "+clientName+" connected", logInfo, channel)
connMutex.Lock()
clients[conn] = channel
connMutex.Unlock()
// Read messages from the client
go func(clientName string, channel string, conn *websocket.Conn) {
clientPingTicker := time.NewTicker(120 * time.Second)
// check for client ping messages and reset the ticker, otherwise close the connection if no ping is received after 60 seconds
go func() {
for {
select {
case <-clientPingTicker.C:
logger("Ping not received, closing connection for client "+clientName, logInfo, channel)
clearChannelRequests(channel)
conn.Close()
connMutex.Lock()
delete(clients, conn)
connMutex.Unlock()
//remove clientname from map
mapMutex.Lock()
delete(addrToNameMap, fmt.Sprintf("%p", conn))
mapMutex.Unlock()
return
}
}
}()
defer clientPingTicker.Stop()
for {
messageType, messageBytes, err := conn.ReadMessage()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
logger("Client "+clientName+" disconnected", logInfo, channel)
} else {
logger("Error reading message from client "+clientName+": "+err.Error(), logError, channel)
}
conn.Close()
connMutex.Lock()
delete(clients, conn)
connMutex.Unlock()
//remove clientname from map
mapMutex.Lock()
delete(addrToNameMap, fmt.Sprintf("%p", conn))
mapMutex.Unlock()
return
}
if messageType == websocket.TextMessage {
message := string(messageBytes)
if message == "ping" {
logger("Received ping from "+clientName, logFountain, channel)
clientPingTicker.Reset(60 * time.Second)
} else if message == "close" {
logger("Client "+clientName+" closed the connection", logInfo, channel)
clearChannelRequests(channel)
conn.Close()
connMutex.Lock()
delete(clients, conn)
connMutex.Unlock()
//remove clientname from map
mapMutex.Lock()
delete(addrToNameMap, fmt.Sprintf("%p", conn))
mapMutex.Unlock()
return
} else if strings.Contains(message, "confirm") {
// split the message by spaces and get the last element
// this is the timestamp of the audio that the client is confirming
timestamp := strings.Split(message, " ")[1]
requestName := getAudioDataName(timestamp)
logger("Client "+clientName+" confirmed playing audio for "+requestName, logInfo, channel)
// remove timestamp from playing map
delete(playing, timestamp)
} else {
logger("Unknown message from "+clientName+": "+message, logDebug, channel)
}
} else if messageType == websocket.BinaryMessage {
logger("Received binary message from "+clientName, logDebug, channel)
}
}
}(clientName, channel, conn)
}
func sendTextMessage(channel string, message string) {
for client, clientChannel := range clients {
if clientChannel == channel {
clientName := getClientName(fmt.Sprintf("%p", client))
err := client.WriteMessage(websocket.TextMessage, []byte(message))
if err != nil {
logger("Error sending text message to "+clientName+": "+err.Error(), logError, channel)
client.Close()
connMutex.Lock()
delete(clients, client)
connMutex.Unlock()
}
logger("Text message sent to "+clientName, logInfo, channel)
}
}
}