1
0
forked from rfa/radioweb

serve assets, tweaks

This commit is contained in:
ron 2019-04-12 21:11:11 +02:00
parent 2741a55e04
commit a5f489691d
2 changed files with 47 additions and 27 deletions

View File

@ -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();
</script>
<noscript>Please enable javascript to use the audio player.</noscript>
</body>

44
main.go
View File

@ -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) {