Let's see the example below:
Server side:
package main
import (
"fmt"
"net"
"net/http"
"time"
"github.com/donovanhide/eventsource"
)
type TimeEvent time.Time
func (t TimeEvent) Id() string { return fmt.Sprint(time.Time(t).UnixNano()) }
func (t TimeEvent) Event() string { return "Tick" }
func (t TimeEvent) Data() string { return time.Time(t).String() }
const (
TICK_COUNT = 5
)
func TimePublisher(srv *eventsource.Server) {
start := time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC)
ticker := time.NewTicker(time.Second)
for {
select {
case <- ticker.C:
}
srv.Publish([]string{"time"}, TimeEvent(start))
start = start.Add(time.Second)
}
}
func main() {
srv := eventsource.NewServer()
srv.Gzip = true
defer srv.Close()
l, err := net.Listen("tcp", "127.0.0.1:8099")
if err != nil {
return
}
defer l.Close()
http.HandleFunc("/time", srv.Handler("time"))
go http.Serve(l, nil)
go TimePublisher(srv)
fmt.Println("event source started.")
select {}
}
Client side:
package main
import (
"fmt"
"github.com/donovanhide/eventsource"
)
func main() {
stream, err := eventsource.Subscribe("http://127.0.0.1:8099/time", "")
if err != nil {
return
}
for ev := range stream.Events{
fmt.Println(ev.Id(), ev.Event(), ev.Data())
}
}
You'll find that after the connection has been extablished, and the client side has print logs like this:
1356998406000000000 Tick 2013-01-01 00:00:06 +0000 UTC
1356998407000000000 Tick 2013-01-01 00:00:07 +0000 UTC
1356998408000000000 Tick 2013-01-01 00:00:08 +0000 UTC
1356998409000000000 Tick 2013-01-01 00:00:09 +0000 UTC
1356998410000000000 Tick 2013-01-01 00:00:10 +0000 UTC
1356998411000000000 Tick 2013-01-01 00:00:11 +0000 UTC
But, if you kill the server side process now, you can find that client just hang there, and no errors occurs.
And there is no retry actions.
How to solve this, guys ?
Let's see the example below:
Server side:
Client side:
You'll find that after the connection has been extablished, and the client side has print logs like this:
But, if you kill the server side process now, you can find that client just hang there, and no errors occurs.
And there is no retry actions.
How to solve this, guys ?