Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Stream Server Project
Stream Server
Commits
207b0004
Commit
207b0004
authored
Aug 21, 2019
by
Simon Kirsten
Browse files
TV now serves the current state on a new SSE connection and fixes
parent
b631df01
Changes
1
Hide whitespace changes
Inline
Side-by-side
internal/tv/tv.go
View file @
207b0004
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/
upd
ate?large_channel=asdf&small_channel=null&volume=&small_scale=0.25&show_chat=true
// /tv/
st
ate?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
())
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment