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"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
@ -25,6 +26,10 @@ type Message struct {
|
||||||
Object interface{}
|
Object interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type pingMessage struct {
|
||||||
|
PingCount int `json:"ping"`
|
||||||
|
}
|
||||||
|
|
||||||
// Broker holds open client connections,
|
// Broker holds open client connections,
|
||||||
// listens for incoming events on its Notifier channel
|
// listens for incoming events on its Notifier channel
|
||||||
// and broadcast event data to all registered connections
|
// and broadcast event data to all registered connections
|
||||||
|
@ -44,8 +49,16 @@ type Broker struct {
|
||||||
|
|
||||||
// Listen on different channels and act accordingly
|
// Listen on different channels and act accordingly
|
||||||
func (broker *Broker) listen() {
|
func (broker *Broker) listen() {
|
||||||
|
ticker := time.NewTicker(10 * time.Second)
|
||||||
|
pingCount := 0
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
broker.Notifier <- Message{
|
||||||
|
Event: "ping",
|
||||||
|
Object: pingMessage{PingCount: pingCount},
|
||||||
|
}
|
||||||
|
pingCount++
|
||||||
case s := <-broker.newClients:
|
case s := <-broker.newClients:
|
||||||
// A new client has connected.
|
// A new client has connected.
|
||||||
// Register their message channel
|
// Register their message channel
|
||||||
|
|
Loading…
Reference in New Issue
Block a user