diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-05-21 16:08:23 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-05-21 16:14:02 +0200 |
| commit | eb846695264d871edffb0e09b6d57a1a85793f48 (patch) | |
| tree | 54ccd99fbcedabc0947cd1b658dae40ca677e9a0 /wrms.go | |
| parent | d4dbb47ac776471eb8a9f91a97da40b9b6621a74 (diff) | |
| download | WRMS-eb846695264d871edffb0e09b6d57a1a85793f48.tar.gz WRMS-eb846695264d871edffb0e09b6d57a1a85793f48.zip | |
implement incremental search results
TODO: ensure late search results do not show up for future searches
Diffstat (limited to 'wrms.go')
| -rw-r--r-- | wrms.go | 32 |
1 files changed, 25 insertions, 7 deletions
@@ -44,12 +44,20 @@ type Event struct { Songs []Song `json:"songs"` } +func newNotification(notification string) Event { + return Event{notification, nil} +} + type Connection struct { Id uuid.UUID Events chan Event C *websocket.Conn } +func (c *Connection) Send(ev Event) { + c.Events <- ev +} + func (c *Connection) Close() { llog.Info("Closing connection %s", c.Id) close(c.Events) @@ -57,7 +65,7 @@ func (c *Connection) Close() { } type Wrms struct { - Connections []Connection + Connections map[uuid.UUID]*Connection Songs []Song queue Playlist CurrentSong Song @@ -69,10 +77,20 @@ type Wrms struct { func NewWrms(config Config) *Wrms { wrms := Wrms{} wrms.Config = config + wrms.Connections = make(map[uuid.UUID]*Connection) wrms.Player = NewPlayer(&wrms, strings.Split(config.Backends, " ")) return &wrms } +func (wrms *Wrms) GetConn(connId string) *Connection { + id, err := uuid.Parse(connId) + if err != nil { + llog.Fatal("Failed to parse connId %s: %v", connId, err) + } + + return wrms.Connections[id] +} + func (wrms *Wrms) Close() { llog.Info("Closing WRMS") for _, conn := range wrms.Connections { @@ -80,10 +98,10 @@ func (wrms *Wrms) Close() { } } -func (wrms *Wrms) Broadcast(cmd Event) { - llog.Info("Broadcasting %v", cmd) - for i := 0; i < len(wrms.Connections); i++ { - wrms.Connections[i].Events <- cmd +func (wrms *Wrms) Broadcast(ev Event) { + llog.Info("Broadcasting %v", ev) + for _, conn := range wrms.Connections { + conn.Send(ev) } } @@ -122,14 +140,14 @@ func (wrms *Wrms) PlayPause() { wrms.Next() if wrms.CurrentSong.Uri == "" { llog.Info("No song left to play") - wrms.Broadcast(Event{"stop", []Song{}}) + wrms.Broadcast(newNotification("stop")) return } } ev = Event{"play", []Song{wrms.CurrentSong}} wrms.Player.Play(&wrms.CurrentSong) } else { - ev = Event{"pause", []Song{}} + ev = newNotification("pause") wrms.Player.Pause() } |
