Documentation
¶
Index ¶
- Constants
- Variables
- func SinkEvents(w http.ResponseWriter, code int, feed EventFeed, options ...ServerOption) error
- func SinkJSONEvents(w http.ResponseWriter, code int, feed AnyEventFeed) error
- func SplitFunc() func([]byte, bool) (int, []byte, error)
- type AnyEventFeed
- type ClientOption
- type Event
- type EventFeed
- type EventSink
- type KeepAliveTime
- type ReadyState
- type ReconnectTime
- type SSEClient
- func (ssec *SSEClient) Close()
- func (ssec *SSEClient) Errors() <-chan error
- func (ssec *SSEClient) GetStream(uri string) error
- func (ssec *SSEClient) Messages() <-chan *Event
- func (ssec *SSEClient) Opens() <-chan bool
- func (ssec *SSEClient) Reopen()
- func (ssec *SSEClient) SetContext(ctx context.Context)
- func (ssec *SSEClient) URL() *url.URL
- type ServerOption
- type SinkEvent
- type StdLogger
- type WantFlag
Constants ¶
const ( // bitwise flags for selecting which events to pass through WantErrors WantFlag = 1 WantOpenClose WantFlag = 2 WantMessages WantFlag = 4 // ReadyState is an enum representing the current status of the connection Connecting ReadyState = 0 Open ReadyState = 1 Closed ReadyState = 2 )
const ( MessageType = "message" ErrorType = "error" )
message types/names
Variables ¶
var ( IDHeader = []byte("id") EventHeader = []byte("event") DataHeader = []byte("data") RetryHeader = []byte("retry") )
protocol header constants
Functions ¶
func SinkEvents ¶
func SinkEvents(w http.ResponseWriter, code int, feed EventFeed, options ...ServerOption) error
SinkEvents is an more-or-less drop-in replacement for a responder in a net/http response handler. It handles all the SSE protocol for you - just feed it events.
func SinkJSONEvents ¶
func SinkJSONEvents(w http.ResponseWriter, code int, feed AnyEventFeed) error
SinkJSONEvents is a wrapped-up handler for responding with anything that encoding/json can marshal, that you can happily `return` to in a net/http handler.
Types ¶
type AnyEventFeed ¶
type AnyEventFeed interface {
GetEventChan(clientCloseChan <-chan struct{}) <-chan interface{}
}
AnyEventFeed represents an event feed which does not return events which have their own byte marshall method. Instead, encoding/json is used to marshal the events.
type ClientOption ¶
type Event ¶
type Event struct { // EventID is the ID of the event, or a previous event EventID string // Type is variously called "event type" and "event name" in the // TR. Defaults to "message". You must listen for specific named // event types to receive them. Type string // Error contains a go error if the error came from the client // (eg, connection problems) Error error // Data is the body of the event, and always terminated with a // line feed. "Simple" events have this empty. Returned as a // []byte as go decoders generally use that, but can't be binary! Data []byte // the RFC "Origin" field Origin string }
Event is a structure holding SSE-compliant events
func (*Event) GetData ¶
GetData implements the SinkEvent interface, so that sse.Event can be used for sinks and sources.
func (*Event) Reader ¶
func (ev *Event) Reader() io.ReadSeeker
Reader returns a reader for convenient passing to decoder functions.
type EventFeed ¶
type EventFeed interface {
GetEventChan(clientCloseChan <-chan struct{}) <-chan SinkEvent
}
EventFeed is a type for something that can return events in a form this API can write them to the write
func NewJSONEncoderFeed ¶
func NewJSONEncoderFeed(anyEventFeed AnyEventFeed) EventFeed
NewJSONEncoderFeed converts an 'AnyEventFeed' to an EventFeed by marshalling each of the events returned via the EventFeed via encoding/json.
type EventSink ¶
type EventSink struct {
// contains filtered or unexported fields
}
EventSink is a structure used by the event sink writer
func NewEventSink ¶
func NewEventSink(w http.ResponseWriter, feed EventFeed, options ...ServerOption) (*EventSink, error)
NewEventSink returns an Event
type KeepAliveTime ¶
func (KeepAliveTime) Apply ¶
func (kat KeepAliveTime) Apply(sink *EventSink) error
type ReadyState ¶
type ReadyState int32
type ReconnectTime ¶
func (ReconnectTime) Apply ¶
func (rt ReconnectTime) Apply(ssec *SSEClient) error
type SSEClient ¶
type SSEClient struct { http.Client // all state changes are guarded by this sync.Mutex // contains filtered or unexported fields }
SSEClient is a wrapper for an http Client which can also hold one SSE session open, and implements EventSource. You can use this API directly, but the EventSource API will steer you towards message protocols and behavior which also work from browsers.
func NewSSEClient ¶
func NewSSEClient(options ...ClientOption) *SSEClient
NewSSEClient creates a new client which can make a single SSE call.
func (*SSEClient) Errors ¶
Errors returns a channel from which errors will be returned. As an error indicates that the SSE channel is closed, there will only be one error returned before you reset the client (via Reopen())
func (*SSEClient) GetStream ¶
GetStream makes a GET request and returns a channel for *all* events read
func (*SSEClient) Reopen ¶
func (ssec *SSEClient) Reopen()
Reopen allows a connection which was closed to be re-opened again.
func (*SSEClient) SetContext ¶
SetContext allows the context to be specified - this affects cancelation and timeouts. Affects active client on reconnection only.
type ServerOption ¶
type SinkEvent ¶
SinkEvent is a generic type for things which can be marshalled to bytes. They might also implement any of the below interfaces to control behavior.