Add default ticker
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2018-09-08 20:07:05 +02:00
parent f0bdae1855
commit 58b7027259
Signed by: peter
GPG Key ID: 374322D56E5209E8

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"time"
"p83.nl/go/ekster/pkg/microsub"
)
@ -22,13 +23,21 @@ func newConsumer(conn net.Conn) *Consumer {
fmt.Fprint(conn, "\r\n")
go func() {
for msg := range cons.output {
fmt.Fprint(conn, `event: ping`)
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, `data:`)
json.NewEncoder(conn).Encode(msg)
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, "\r\n")
for {
select {
case <-time.Tick(10 * time.Second):
fmt.Fprint(conn, `event: ping`)
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, "\r\n")
case msg := <-cons.output:
fmt.Fprint(conn, `event: message`)
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, `data:`)
json.NewEncoder(conn).Encode(msg)
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, "\r\n")
}
}
conn.Close()
}()