From 449e6321c678bb7b56e92c79f2476cb20eae3283 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 18 Aug 2019 11:52:03 +0200 Subject: [PATCH] Add 10 second ping message --- pkg/sse/events.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/sse/events.go b/pkg/sse/events.go index 3b9c886..42a1c17 100644 --- a/pkg/sse/events.go +++ b/pkg/sse/events.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "strings" + "time" "github.com/pkg/errors" ) @@ -25,6 +26,10 @@ type Message struct { Object interface{} } +type pingMessage struct { + PingCount int `json:"ping"` +} + // Broker holds open client connections, // listens for incoming events on its Notifier channel // and broadcast event data to all registered connections @@ -44,8 +49,16 @@ type Broker struct { // Listen on different channels and act accordingly func (broker *Broker) listen() { + ticker := time.NewTicker(10 * time.Second) + pingCount := 0 for { select { + case <-ticker.C: + broker.Notifier <- Message{ + Event: "ping", + Object: pingMessage{PingCount: pingCount}, + } + pingCount++ case s := <-broker.newClients: // A new client has connected. // Register their message channel