diff --git a/main.go b/main.go index 6af2299..05b374e 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "container/ring" "net/http" - "strings" + "time" "github.com/gin-gonic/gin" melody "gopkg.in/olahol/melody.v1" @@ -16,6 +16,7 @@ func main() { m := melody.New() history := ring.New(10) + pub := ring.New(10) r.Static("/assets", "./assets") r.StaticFile("/", "./index.html") @@ -31,35 +32,42 @@ func main() { "status": "ok", }) - go func() { - if metadata == "" { + if metadata == "" { + return + } + + for i := 0; i < history.Len(); i++ { + history = history.Next() + + if history.Value == metadata { return } + } - if strings.HasPrefix(metadata, "/srv/audio/") { - metadata = strings.Replace(metadata, "/srv/audio/", "", 1) - } + history.Value = metadata + history = history.Next() - for i := 0; i < history.Len(); i++ { - history = history.Next() - if history.Value == metadata { + go func() { + time.Sleep(time.Second * 10) + for i := 0; i < pub.Len(); i++ { + pub = pub.Next() + + if pub.Value == metadata { return } } - - history.Value = metadata - history = history.Next() - + pub.Value = metadata + pub = pub.Next() m.Broadcast([]byte(metadata)) }() }) m.HandleConnect(func(s *melody.Session) { - for i := 0; i < history.Len(); i++ { - if history.Value != nil { - s.Write([]byte(history.Value.(string))) + for i := 0; i < pub.Len(); i++ { + if pub.Value != nil { + s.Write([]byte(pub.Value.(string))) } - history = history.Next() + pub = pub.Next() } })