go serverside events curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"strconv" | |
"time" | |
"gopkg.in/antage/eventsource.v1" | |
) | |
func main() { | |
es := eventsource.New(nil, nil) | |
defer es.Close() | |
http.Handle("/events", es) | |
go func() { | |
id := 1 | |
for { | |
es.SendEventMessage("tick", "tick-event", strconv.Itoa(id)) | |
id++ | |
time.Sleep(2 * time.Second) | |
} | |
}() | |
log.Fatal(http.ListenAndServe(":8089", nil)) | |
} | |
// ❯ curl -N --http2 -H "Accept:text/event-stream" localhost:8089/events | |
/* | |
id: 4 | |
event: tick-event | |
data: tick | |
id: 5 | |
event: tick-event | |
data: tick | |
id: 6 | |
event: tick-event | |
data: tick | |
*/ | |
/* | |
❯ curl -v -N --http2 -H "Accept:text/event-stream" localhost:8089/events | |
* Trying 127.0.0.1:8089... | |
* Connected to localhost (127.0.0.1) port 8089 (#0) | |
> GET /events HTTP/1.1 | |
> Host: localhost:8089 | |
> User-Agent: curl/7.79.1 | |
> Connection: Upgrade, HTTP2-Settings | |
> Upgrade: h2c | |
> HTTP2-Settings: AAMAAABkAAQCAAAAAAIAAAAA | |
> Accept:text/event-stream | |
> | |
* Mark bundle as not supporting multiuse | |
< HTTP/1.1 200 OK | |
< Content-Type: text/event-stream | |
< Vary: Accept-Encoding | |
* no chunk, no close, no size. Assume close to signal end | |
< | |
id: 3 | |
event: tick-event | |
data: tick | |
id: 4 | |
event: tick-event | |
data: tick | |
id: 5 | |
event: tick-event | |
data: tick | |
^C | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment