Commit 207b0004 authored by Simon Kirsten's avatar Simon Kirsten
Browse files

TV now serves the current state on a new SSE connection and fixes

parent b631df01
package tv
import (
"encoding/json"
"log"
"net/http"
"strconv"
......@@ -26,7 +27,7 @@ var state = tvState{
LargeChannel: nil,
SmallChannel: nil,
Volume: 0.5,
SmallScale: 0.5,
SmallScale: 0.3,
ShowChat: false,
}
......@@ -37,7 +38,7 @@ var clients map[*sse.Client]bool = make(map[*sse.Client]bool)
// stateHandleFunc updated the state based on the query string.
// For example
// /tv/update?large_channel=asdf&small_channel=null&volume=&small_scale=0.25&show_chat=true
// /tv/state?large_channel=asdf&small_channel=null&volume=&small_scale=0.25&show_chat=true
// will
// - set large_channel to asdf
// - reset small_channel to nil/null
......@@ -98,9 +99,7 @@ func stateHandleFunc(w http.ResponseWriter, r *http.Request) {
}
}
// TODO: redo this error output
if errs != nil { // we had errors
http.Error(w, errs.Error(), http.StatusBadRequest)
log.Printf("Error(s) while parsing update query: %v\n", errs)
......@@ -146,6 +145,19 @@ func eventsHandleFunc(w http.ResponseWriter, r *http.Request) {
// remove client from broadcast set on exit
defer delete(clients, client)
body, err := json.Marshal(state)
if err != nil {
return
}
go func() {
// client.Send will block the channel that client.Run is listening on
// thats why we send the initial state in an goroutine that runs concurrently with client.Run
client.Send(sse.Msg{
Data: string(body),
})
}()
// run the in the context of the request
client.Run(r.Context())
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment