Add 10 second ping message
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2019-08-18 11:52:03 +02:00
parent 2b7b63ba83
commit 449e6321c6
Signed by: peter
GPG Key ID: 374322D56E5209E8

View File

@ -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