X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/8f9b19f7877e32c15a5f4c296d6738f449dfcc19..8320951221d45c5f5f3d387c5cb4b97d9fa2094c:/server/command.go diff --git a/server/command.go b/server/command.go new file mode 100644 index 0000000..cd7410b --- /dev/null +++ b/server/command.go @@ -0,0 +1,53 @@ +package main + +import ( + "code.octet-stream.net/broadcaster/protocol" + "encoding/json" + "golang.org/x/net/websocket" + "sync" +) + +type CommandRouter struct { + connsMutex sync.Mutex + conns map[int]*websocket.Conn +} + +var commandRouter CommandRouter + +func InitCommandRouter() { + commandRouter.conns = make(map[int]*websocket.Conn) +} + +func (c *CommandRouter) AddWebsocket(radioId int, ws *websocket.Conn) { + c.connsMutex.Lock() + defer c.connsMutex.Unlock() + c.conns[radioId] = ws +} + +func (c *CommandRouter) RemoveWebsocket(ws *websocket.Conn) { + c.connsMutex.Lock() + defer c.connsMutex.Unlock() + key := -1 + for k, v := range c.conns { + if v == ws { + key = k + } + } + if key != -1 { + delete(c.conns, key) + } + +} + +func (c *CommandRouter) Stop(radioId int) { + c.connsMutex.Lock() + defer c.connsMutex.Unlock() + ws := c.conns[radioId] + if ws != nil { + stop := protocol.StopMessage{ + T: protocol.StopType, + } + msg, _ := json.Marshal(stop) + ws.Write(msg) + } +}