Improve SSE status and event auth handling
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user