mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -04:00
86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
// Copyright 2014 Team 254. All Rights Reserved.
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
//
|
|
// Web handlers for the pit rankings display.
|
|
|
|
package main
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"text/template"
|
|
)
|
|
|
|
// Renders the pit display which shows scrolling rankings.
|
|
func PitDisplayHandler(w http.ResponseWriter, r *http.Request) {
|
|
if !UserIsReader(w, r) {
|
|
return
|
|
}
|
|
|
|
template, err := template.ParseFiles("templates/pit_display.html")
|
|
if err != nil {
|
|
handleWebErr(w, err)
|
|
return
|
|
}
|
|
data := struct {
|
|
*EventSettings
|
|
}{eventSettings}
|
|
err = template.Execute(w, data)
|
|
if err != nil {
|
|
handleWebErr(w, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// The websocket endpoint for the pit display, used only to force reloads remotely.
|
|
func PitDisplayWebsocketHandler(w http.ResponseWriter, r *http.Request) {
|
|
if !UserIsReader(w, r) {
|
|
return
|
|
}
|
|
|
|
websocket, err := NewWebsocket(w, r)
|
|
if err != nil {
|
|
handleWebErr(w, err)
|
|
return
|
|
}
|
|
defer websocket.Close()
|
|
|
|
reloadDisplaysListener := mainArena.reloadDisplaysNotifier.Listen()
|
|
defer close(reloadDisplaysListener)
|
|
|
|
// Spin off a goroutine to listen for notifications and pass them on through the websocket.
|
|
go func() {
|
|
for {
|
|
var messageType string
|
|
var message interface{}
|
|
select {
|
|
case _, ok := <-reloadDisplaysListener:
|
|
if !ok {
|
|
return
|
|
}
|
|
messageType = "reload"
|
|
message = nil
|
|
}
|
|
err = websocket.Write(messageType, message)
|
|
if err != nil {
|
|
// The client has probably closed the connection; nothing to do here.
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Loop, waiting for commands and responding to them, until the client closes the connection.
|
|
for {
|
|
_, _, err := websocket.Read()
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
// Client has closed the connection; nothing to do here.
|
|
return
|
|
}
|
|
log.Printf("Websocket error: %s", err)
|
|
return
|
|
}
|
|
}
|
|
}
|