aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-06-30 19:13:59 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2023-06-30 19:13:59 +0200
commitdb21d2ec6980e31a2e9a0db60eb2fdad83b33671 (patch)
treef1c963ccb419b2d7ddff6cfe1a66aec1fb365c8b /main.go
parent6c81e0dcaf5905c6fac3fe4b62dffaf67027d785 (diff)
downloadWRMS-db21d2ec6980e31a2e9a0db60eb2fdad83b33671.tar.gz
WRMS-db21d2ec6980e31a2e9a0db60eb2fdad83b33671.zip
switch from websockets to SSE
Diffstat (limited to 'main.go')
-rw-r--r--main.go25
1 files changed, 11 insertions, 14 deletions
diff --git a/main.go b/main.go
index 0ea10dd..53abc2f 100644
--- a/main.go
+++ b/main.go
@@ -14,8 +14,6 @@ import (
"muhq.space/go/wrms/llog"
"github.com/google/uuid"
-
- "nhooyr.io/websocket"
)
var wrms *Wrms
@@ -188,30 +186,29 @@ func nextHandler(w http.ResponseWriter, r *http.Request) {
genericControlHandler(w, r, "next")
}
-func wsEndpoint(w http.ResponseWriter, r *http.Request) {
+func eventsEndpoint(w http.ResponseWriter, r *http.Request) {
connId, err := getConnId(w, r)
if err != nil {
return
}
- ws, err := websocket.Accept(w, r, nil)
- if err != nil {
- llog.Warning("Accepting the websocket failed with %s", err)
- return
- }
+ // prepare the header
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.Header().Set("Connection", "keep-alive")
+ w.Header().Set("Access-Control-Allow-Origin", "*")
ctx, cancel := context.WithCancel(r.Context())
- ctx = ws.CloseRead(ctx)
+
+ conn := newConnection(connId, w, ctx, cancel)
defer func() {
llog.Info("cancel context of connection %v", connId)
cancel()
+ conn.Close()
}()
- conn := newConnection(connId, ws, ctx, cancel)
- defer conn.Close()
-
- llog.Info("New websocket connection with id %v", connId)
+ llog.Info("New SSE connection with id %v", connId)
err = wrms.initConn(conn)
if err != nil {
return
@@ -230,7 +227,7 @@ func setupRoutes() {
http.HandleFunc("/delete", deleteHandler)
http.HandleFunc("/next", nextHandler)
http.HandleFunc("/playpause", playPauseHandler)
- http.HandleFunc("/ws", wsEndpoint)
+ http.HandleFunc("/events", eventsEndpoint)
}
func main() {