sse

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package sse implements server-sent events as described by the HTML Standard. See https://html.spec.whatwg.org/multipage/server-sent-events.html for more details.

Currently, only a server implementation is provided.

Index

Constants

View Source
const (
	// EOL is the end-of-line character for fields and events.
	EOL byte = '\n'
)

Variables

This section is empty.

Functions

func H2CTransport added in v0.4.0

func H2CTransport() http.RoundTripper

H2CTransport creates a transport that supports HTTP2 cleartext.

func H2CWrap added in v0.4.0

func H2CWrap(h http.Handler) http.Handler

H2CWrap wraps a HTTP handler to support HTTP2 cleartext. Note that net/http.Server by itself enables HTTP2 automatically if using a TLS certificate.

Types

type Error added in v0.4.0

type Error struct {
	Request *http.Request
	// contains filtered or unexported fields
}

Error represents an SSE-specific error.

func (*Error) Error added in v0.4.0

func (e *Error) Error() string

Error returns an error string with the client's IP address.

type Event added in v0.4.0

type Event struct {
	// Comment is a piece of text ignored by the client.
	// It may be used to send a heartbeat to keep the SSE connection alive.
	Comment string

	// Type is the kind of event.
	// Listeners for this specific type can be dispatched client side to process the event.
	Type string

	// Data is the event's content.
	Data []byte

	// ID is a unique identifier for the event.
	ID string

	// Retry sets the client-side interval for reconnecting to the server.
	Retry time.Duration

	// If Raw is true, newlines in the event's data are not escaped.
	// Escaping prevents accidental truncation of text content.
	//
	// However, if it has already been encoded i.e. with JSON, Raw can be set to true.
	Raw bool
}

Event represents a server-sent event. The zero value of an event can be used as-is.

Reference: https://html.spec.whatwg.org/multipage/server-sent-events.html

func (*Event) Marshal added in v0.4.0

func (e *Event) Marshal(w io.Writer)

Marshal writes the stream respresentation of the event to the writer.

type Server added in v0.4.0

type Server struct {
	// contains filtered or unexported fields
}

Server is a stream multiplexer, allowing clients to receive events from a specific stream.

func NewServer added in v0.4.0

func NewServer(defaultStream string) *Server

NewServer creates a new server.

If defaultStream is not empty, a default stream is created for clients who do not specify a stream to connect to.

func (*Server) Add added in v0.4.0

func (s *Server) Add(name string) *Stream

Add creates a new stream in the server. If the stream already exists, the existing one is returned.

func (*Server) Close added in v0.4.0

func (s *Server) Close()

Close closes all server streams.

func (*Server) Get added in v0.4.0

func (s *Server) Get(name string) *Stream

Get returns an existing stream by name in the server. If the stream does not exist, nil is returned.

func (*Server) Remove added in v0.4.0

func (s *Server) Remove(name string)

Remove deletes the stream by name from the server and closes it. If the stream does not exist, this is a no-op.

func (*Server) Send added in v0.4.0

func (s *Server) Send(name string, e *Event)

Send is a convenience function for Get(name).Send(event). The stream must have already been added beforehand; if Get(name) is nil, a panic occurs.

func (*Server) ServeHTTP added in v0.4.0

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP connects a client to a stream in the server depending on the 'stream' query parameter.

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

Stream represents an event stream with several clients. Any errors encountered are sent over the Errors channel.

func NewStream

func NewStream() *Stream

NewStream creates a new stream.

func (*Stream) Close

func (s *Stream) Close()

Close closes the stream, dropping all events.

func (*Stream) Send

func (s *Stream) Send(e *Event)

Send sends an event to all clients connected to the stream.

func (*Stream) ServeHTTP added in v0.4.0

func (s *Stream) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP connects a client to the stream and waits for sent events. If the client does not support SSE, a 500 status code will be sent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL