From a5f489691dd7b7fc6f9c59a03c6a8e22a0b119c7 Mon Sep 17 00:00:00 2001 From: ron Date: Fri, 12 Apr 2019 21:11:11 +0200 Subject: [PATCH] serve assets, tweaks --- index.html | 30 ++++++++++++++++++++++++++---- main.go | 44 +++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/index.html b/index.html index 003b436..99e5222 100644 --- a/index.html +++ b/index.html @@ -34,10 +34,7 @@ var player = document.getElementById('player'); var rng = Math.floor(Math.random() * 10000); player.src = player.src + '?nocache=' + rng; -var url = 'wss://radio.anarchyplanet.org/ws'; -var ws = new WebSocket(url); - -ws.onmessage = function(msg){ +function update(msg) { document.title = msg.data + " - APR"; var hist = document.getElementById("history"); @@ -47,8 +44,33 @@ ws.onmessage = function(msg){ if(hist.childElementCount >= 10) { hist.removeChild(hist.lastChild); } +}; + +function connect() { + console.log('connecting'); + + var url = 'wss://radio.anarchyplanet.org/ws'; + var ws = new WebSocket(url); + + ws.onmessage = function(e) { + update(e); + }; + + ws.onclose = function(e) { + setTimeout(connect, 1000); + }; + + ws.onerror = function(e) { + ws.close(); + }; + + console.log(ws); } + +connect(); + + diff --git a/main.go b/main.go index 43af570..2501d46 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,8 @@ func main() { history := ring.New(10) - r.GET("/", func(c *gin.Context) { - http.ServeFile(c.Writer, c.Request, "index.html") - }) + r.Static("/assets", "./assets") + r.StaticFile("/", "./index.html") r.GET("/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) @@ -28,32 +27,31 @@ func main() { r.POST("/update", func(c *gin.Context) { metadata := c.PostForm("metadata") - if strings.HasPrefix(metadata, "/srv/audio/") { - metadata = strings.Replace(metadata, "/srv/audio/", "", 1) - } - - if metadata == "" { - c.JSON(http.StatusBadRequest, gin.H{ - "status": "failure", - }) - return - } - c.JSON(http.StatusOK, gin.H{ - "status": "success", + "status": "ok", }) - for j := 0; j < 10; j++ { - if history.Value == metadata { - history = history.Next() + go func() { + if metadata == "" { return } - history = history.Next() - } - m.Broadcast([]byte(metadata)) - history.Value = metadata - history = history.Next() + if strings.HasPrefix(metadata, "/srv/audio/") { + metadata = strings.Replace(metadata, "/srv/audio/", "", 1) + } + + for i := 0; i < history.Len(); i++ { + history = history.Next() + if history.Value == metadata { + return + } + } + + m.Broadcast([]byte(metadata)) + + history.Value = metadata + history = history.Next() + }() }) m.HandleConnect(func(s *melody.Session) {