Add 10 second ping message
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
2b7b63ba83
commit
449e6321c6
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user