Documentation ¶
Overview ¶
Package sse implements Server-Sent Events(SSE) This extension is compliant with any net/http implementation, and is not limited to WebGo.
Index ¶
- func DefaultBeforeSend(ctx context.Context, client *Client)
- func DefaultCreateHook(ctx context.Context, client *Client, count int)
- func DefaultOnSend(ctx context.Context, client *Client, err error)
- func DefaultRemoveHook(ctx context.Context, clientID string, count int)
- func DefaultUnsupportedMessageHandler(w http.ResponseWriter, r *http.Request) error
- type Client
- type ClientManager
- type Clients
- func (cs *Clients) Active() int
- func (cs *Clients) Client(clientID string) *Client
- func (cs *Clients) Clients() []*Client
- func (cs *Clients) New(ctx context.Context, w http.ResponseWriter, clientID string) (*Client, int)
- func (cs *Clients) Range(f func(cli *Client))
- func (cs *Clients) Remove(clientID string) int
- type Message
- type SSE
- func (sse *SSE) ActiveClients() int
- func (sse *SSE) Broadcast(msg Message)
- func (sse *SSE) Client(id string) *Client
- func (sse *SSE) Handler(w http.ResponseWriter, r *http.Request) error
- func (sse *SSE) HandlerFunc(w http.ResponseWriter, r *http.Request)
- func (sse *SSE) NewClient(ctx context.Context, w http.ResponseWriter, clientID string) *Client
- func (sse *SSE) RemoveClient(ctx context.Context, clientID string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultBeforeSend ¶
func DefaultUnsupportedMessageHandler ¶
func DefaultUnsupportedMessageHandler(w http.ResponseWriter, r *http.Request) error
Types ¶
type ClientManager ¶
type ClientManager interface { // New should return a new client, and the total number of active clients after adding this new one New(ctx context.Context, w http.ResponseWriter, clientID string) (*Client, int) // Range should iterate through all the active clients Range(func(*Client)) // Remove should remove the active client given a clientID, and close the connection Remove(clientID string) int // Active returns the number of active clients Active() int // Clients returns a list of all active clients Clients() []*Client // Client returns *Client if clientID is active Client(clientID string) *Client }
func NewClientManager ¶
func NewClientManager() ClientManager
type Clients ¶
type Clients struct { MsgBuffer int // contains filtered or unexported fields }
func (*Clients) Clients ¶
MessageChannels returns a slice of message channels of all clients which you can then use to send message concurrently
type Message ¶
type Message struct { // Event is a string identifying the type of event described. If this is specified, an event will be dispatched on the browser to the listener for the specified event name; the website source code should use addEventListener() to listen for named events. The onmessage handler is called if no event name is specified for a message. Event string // Data field for the message. When the EventSource receives multiple consecutive lines that begin with data:, it concatenates them, inserting a newline character between each one. Trailing newlines are removed. Data string // ID to set the EventSource object's last event ID value. ID string // Retry is the reconnection time. If the connection to the server is lost, the browser will wait for the specified time before attempting to reconnect. This must be an integer, specifying the reconnection time in milliseconds. If a non-integer value is specified, the field is ignored. Retry time.Duration }
Message represents a valid SSE message ref: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
type SSE ¶
type SSE struct { // ClientIDHeader is the HTTP request header in which the client ID is set. Default is `sse-clientid` ClientIDHeader string // UnsupportedMessage is used to send the error response to client if the // server doesn't support SSE UnsupportedMessage func(http.ResponseWriter, *http.Request) error // OnCreateClient is a hook, for when a client is added to the active clients. count is the number // of active clients after adding the latest client OnCreateClient func(ctx context.Context, client *Client, count int) // OnRemoveClient is a hook, for when a client is removed from the active clients. count is the number // of active clients after removing a client OnRemoveClient func(ctx context.Context, clientID string, count int) // OnSend is a hook, which is called *after* a message is sent to a client OnSend func(ctx context.Context, client *Client, err error) // BeforeSend is a hook, which is called right before a message is sent to a client BeforeSend func(ctx context.Context, client *Client) Clients ClientManager }
func (*SSE) ActiveClients ¶
func (*SSE) Handler ¶
Handler returns an error rather than being directly used as an http.HandlerFunc, to let the user handle error. e.g. if the error has to be logged
func (*SSE) HandlerFunc ¶
func (sse *SSE) HandlerFunc(w http.ResponseWriter, r *http.Request)
HandlerFunc is a convenience function which can be directly used with net/http implementations. Important: You cannot handle any error returned by the Handler