Improve SSE status and event auth handling

This commit is contained in:
Brandon Zhang
2026-03-27 17:55:28 +08:00
parent f437f6939c
commit 65b29bcf03
6 changed files with 77 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ package api
import (
"net/http"
"time"
"github.com/local-mcp/local-mcp-go/internal/events"
)
@@ -24,6 +25,8 @@ func handleSSE(broker *events.Broker) http.HandlerFunc {
// Subscribe to event broker
ch := broker.Subscribe()
defer broker.Unsubscribe(ch)
heartbeat := time.NewTicker(15 * time.Second)
defer heartbeat.Stop()
// Send initial connection event
w.Write([]byte("data: {\"type\":\"connected\"}\n\n"))
@@ -38,6 +41,9 @@ func handleSSE(broker *events.Broker) http.HandlerFunc {
}
w.Write(msg)
flusher.Flush()
case <-heartbeat.C:
w.Write([]byte(": keepalive\n\n"))
flusher.Flush()
case <-r.Context().Done():
return // client disconnected
}