11 "code.octet-stream.net/broadcaster/internal/protocol"
12 "golang.org/x/net/websocket"
15 func WebSync(ws
*websocket
.Conn
) {
16 log
.Println("A web user connected with WebSocket")
17 buf
:= make([]byte, 16384)
20 isAuthenticated
:= false
23 // Ignore any massively oversize messages
24 n
, err
:= ws
.Read(buf
)
26 if user
.Username
!= "" {
27 log
.Println("Lost websocket to user:", user
)
29 log
.Println("Lost unauthenticated website websocket")
42 token
:= string(buf
[:n
])
43 u
, err
:= users
.GetUserForSession(token
)
45 log
.Println("Could not find user for offered token", token
, err
)
50 log
.Println("User authenticated:", user
)
51 isAuthenticated
= true
55 // send initial playlists message
56 err
= sendRadioStatusToWeb(ws
)
64 type WebStatusData
struct {
65 Radios
[]WebRadioStatus
68 type WebRadioStatus
struct {
82 func sendRadioStatusToWeb(ws
*websocket
.Conn
) error
{
83 webStatuses
:= make([]WebRadioStatus
, 0)
84 radioStatuses
:= status
.Statuses()
85 keys
:= make([]int, 0)
86 for i
:= range radioStatuses
{
87 keys
= append(keys
, i
)
90 for _
, i
:= range keys
{
92 radio
, err
:= db
.GetRadio(i
)
96 var channelClass
, channelState
string
104 channelClass
= "clear"
105 channelState
= "CLEAR"
107 var statusText
string
108 var disableCancel
bool
109 if v
.Status
== protocol
.StatusIdle
{
112 } else if v
.Status
== protocol
.StatusDelay
{
113 statusText
= fmt
.Sprintf("Performing delay before transmit: %ds remain", v
.DelaySecondsRemaining
)
114 disableCancel
= false
115 } else if v
.Status
== protocol
.StatusChannelInUse
{
116 statusText
= fmt
.Sprintf("Waiting for channel to clear: %ds", v
.WaitingForChannelSeconds
)
117 disableCancel
= false
118 } else if v
.Status
== protocol
.StatusPlaying
{
119 statusText
= fmt
.Sprintf("Playing: %d:%02d", v
.PlaybackSecondsElapsed
/60, v
.PlaybackSecondsElapsed%60
)
120 disableCancel
= false
122 playlist
:= v
.Playlist
126 filename
:= v
.Filename
130 webStatuses
= append(webStatuses
, WebRadioStatus
{
132 LocalTime
: v
.LocalTime
,
133 TimeZone
: v
.TimeZone
,
134 ChannelClass
: channelClass
,
135 ChannelState
: channelState
,
140 DisableCancel
: disableCancel
,
141 FilesInSync
: v
.FilesInSync
,
144 data
:= WebStatusData
{
147 buf
:= new(strings
.Builder
)
148 tmpl
:= template
.Must(template
.ParseFS(content
, "templates/radios.partial.html"))
149 tmpl
.Execute(buf
, data
)
150 _
, err
:= ws
.Write([]byte(buf
.String()))
154 func KeepWebUpdated(ws
*websocket
.Conn
) {
156 <-status
.ChangeChannel()
157 err
:= sendRadioStatusToWeb(ws
)