-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
252 lines (234 loc) · 6.4 KB
/
server.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"flag"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/fhs/gompd/v2/mpd"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{}
var server Server
var jukebox Jukebox
// Server : State of connections
type Server struct {
connections map[*websocket.Conn]bool
mux sync.Mutex
}
func initServer() Server {
return Server{connections: make(map[*websocket.Conn]bool)}
}
func (server *Server) removeConnection(c *websocket.Conn) {
server.mux.Lock()
delete(server.connections, c)
server.mux.Unlock()
}
func (server *Server) addConnection(c *websocket.Conn) {
server.mux.Lock()
server.connections[c] = true
server.mux.Unlock()
}
// Sends error back.
func handleError(c *websocket.Conn, errText string, err error) {
log.Println(errText, err)
c.WriteMessage(websocket.TextMessage, []byte("error "+errText+" "+err.Error()))
}
func socketinit(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Websocket upgrade error:", err)
}
server.addConnection(c)
defer func() {
server.removeConnection(c)
c.Close()
}()
for {
mt, message, err := c.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
}
return
}
switch messageTokens := strings.SplitN(string(message), " ", 2); messageTokens[0] {
case "ping":
c.WriteMessage(mt, []byte("pong"))
continue
case "name":
if len(messageTokens) == 2 && validateName([]byte(messageTokens[1])) {
c.WriteMessage(mt, []byte("ok"))
defer socketHandle(c, []byte(messageTokens[1]))
return
}
c.WriteMessage(mt, []byte("Invalid name."))
continue
default:
c.WriteMessage(mt, []byte("Unknown message."))
continue
}
}
}
func socketHandle(c *websocket.Conn, name []byte) {
sendState()
for {
mt, message, err := c.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("error: %v", err)
}
return
}
switch messageTokens := strings.Split(string(message), " "); messageTokens[0] {
case "ping":
c.WriteMessage(mt, []byte("pong"))
continue
case "skip":
if err := jukebox.SkipSong(); err != nil {
handleError(c, "Skip error:", err)
}
case "volume":
volume, err := strconv.Atoi(messageTokens[1])
if err != nil {
handleError(c, "Error parsing volume:", err)
} else if err := jukebox.SetVolume(volume); err != nil {
handleError(c, "Error setting volume:", err)
}
case "remove":
songPosition, err := strconv.Atoi(messageTokens[1])
if err != nil {
handleError(c, "Error parsing remove:", err)
}
jukebox.RemoveSong(string(name), songPosition)
case "queue":
songurl := messageTokens[1]
err := jukebox.AddSongURL(string(name), songurl)
if err != nil {
handleError(c, "Error adding song:", err)
} else {
c.WriteMessage(mt, []byte("ok"))
}
case "pause":
err := jukebox.Pause()
if err != nil {
handleError(c, "Error pausing:", err)
}
case "resume":
err := jukebox.Resume()
if err != nil {
handleError(c, "Error resuming:", err)
}
default:
log.Println("Illegal command:", messageTokens[0])
c.WriteMessage(mt, []byte("Illegal command: "+messageTokens[0]))
}
sendState()
}
}
func sendState() {
bytestate := []byte(jukebox.GetState())
server.mux.Lock()
defer server.mux.Unlock()
for c := range server.connections {
c.WriteMessage(1, bytestate)
}
}
func validateName(name []byte) bool {
if len(name) <= 20 && len(name) > 0 {
match, _ := regexp.Match("^[a-zA-Z0-9]*$", name)
return match
}
return false
}
func main() {
// Command line flags
var host string
flag.StringVar(&host, "host", "localhost", "Server host ip")
var port string
flag.StringVar(&port, "port", "8080", "Server port number")
var mpdhost string
flag.StringVar(&mpdhost, "mpdhost", "localhost", "MPD host ip")
var mpdport string
flag.StringVar(&mpdport, "mpdport", "6600", "MPD port number")
flag.Parse()
log.Println("Attempting MPD Client connection on " + mpdhost + ":" + mpdport)
conn, err := mpd.Dial("tcp", mpdhost+":"+mpdport)
if err != nil {
log.Fatalln(err)
}
defer conn.Close()
if err := conn.Consume(true); err != nil { // Remove song when finished playing
log.Fatalln(err)
}
if err := conn.Clear(); err != nil { // Clear mpd on startup
log.Fatalln(err)
}
log.Println("Successfully connected to MPD")
// Keep MPD Client connection alive
go func() {
for {
time.Sleep(5 * time.Second)
if err := conn.Ping(); err != nil {
log.Println("Ping error:", err)
}
}
}()
// Manages state and player
jukebox = NewJukebox(conn)
log.Println("Attempting MPD Player Watcher connection")
w, err := mpd.NewWatcher("tcp", mpdhost+":"+mpdport, "", "player")
if err != nil {
log.Fatalln(err)
}
log.Println("Successfully watching MPD")
defer w.Close()
go func() {
for range w.Event {
status, err := conn.Status()
if err != nil {
log.Println("Status fetch error:", err)
}
switch status["state"] {
case "play":
case "stop": // When music stops, move onto the next song
// Extremely annoying, but mpd won't know a stream URL won't decode correctly
// until it after it responded that it played successfully. Therefore, only
// here can we handle a broken youtube-dl streamURL.
if err, ok := status["error"]; ok {
log.Println("Playback error:", err)
}
if err := jukebox.CycleSong(); err != nil {
log.Println("Cycle song error:", err)
}
sendState()
case "pause":
}
}
}()
// Log errors.
log.Println("Attempting MPD Error Watcher connection")
ew, err := mpd.NewWatcher("tcp", mpdhost+":"+mpdport, "")
if err != nil {
log.Fatalln(err)
}
log.Println("Successfully watching MPD errors")
defer ew.Close()
go func() {
for err := range ew.Error {
log.Println("MPD Internal Error:", err)
}
}()
// Basic test #1 https://www.youtube.com/watch?v=otdOnrgtyfI
// Basic test #2 https://soundcloud.com/futureisnow/perkys-calling-prod-by-southside
// Playlist test https://www.youtube.com/watch?v=W3J9-OvxNpo&list=PLmIf0JO7SvbKxGuse9T19m_mHBm4oNG7y
server = initServer()
fs := http.FileServer(http.Dir("./dist"))
http.Handle("/", fs)
http.HandleFunc("/ws", socketinit)
log.Println("Starting up server on " + host + ":" + port)
log.Fatalln(http.ListenAndServe(host+":"+port, nil))
}