Documentation ¶
Overview ¶
Package http provides server-sent events for net/http server.
Example:
package main import ( "gopkg.in/antage/eventsource.v1" "log" "net/http" "strconv" "time" ) 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(":8080", nil)) }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EventSource ¶
type EventSource interface { // it should implement ServerHTTP method http.Handler // send message to all consumers SendEventMessage(data, event, id string) // send retry message to all consumers SendRetryMessage(duration time.Duration) SendMessageToClient(clientID, data, event, id string) SendRetryMessageToClient(clientID string, duration time.Duration) // consumers count ConsumersCount() int // close and clear all consumers Close() }
EventSource interface provides methods for sending messages and closing all connections.
type Settings ¶
type Settings struct { // SetTimeout sets the write timeout for individual messages. The // default is 2 seconds. Timeout time.Duration // CloseOnTimeout sets whether a write timeout should close the // connection or just drop the message. // // If the connection gets closed on a timeout, it's the client's // responsibility to re-establish a connection. If the connection // doesn't get closed, messages might get sent to a potentially dead // client. // // The default is true. CloseOnTimeout bool // Sets the timeout for an idle connection. The default is 30 minutes. IdleTimeout time.Duration // Gzip sets whether to use gzip Content-Encoding for clients which // support it. // // The default is false. Gzip bool ClientIDFunc func(*http.Request) string }
func DefaultSettings ¶
func DefaultSettings() *Settings
Click to show internal directories.
Click to hide internal directories.