sse: github.com/longsleep/sse Index | Files

package sse

import "github.com/longsleep/sse"

Index

Package Files

client.go conn.go upgrade.go

Variables

var (
    ErrStreamingNotSupported = errors.New("streaming unsupported")
    ErrConnectionClosed      = errors.New("connection already closed")
)
var DefaultClient = &http.Client{}

Client is the default client used for requests.

var DefaultGetReq = func(method, uri string, body io.Reader) (*http.Request, error) {
    return http.NewRequest(method, uri, body)
}

DefaultGetReq is a function to return a single request. It will be used by notify to get a request and can be replaced if additional configuration is desired on the request. The "Accept" header will necessarily be overwritten.

var (
    // ErrNilChan will be returned by Notify if it is passed a nil channel
    ErrNilChan = fmt.Errorf("nil channel given")
)

func Notify Uses

func Notify(uri string, client *http.Client, getReq getReqFunc, evCh chan<- *Event) error

Notify takes the uri of an SSE stream, http.Client, request generator function and a channel, and will send an Event down the channel when recieved, until the stream is closed. This is blocking, and so you will likely want to call this

in a new goroutine (via `go Notify(..)`). If client or getReq ire nil, the

DefaultClient and DefaultGetReq values are used.

type Conn Uses

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

func Upgrade Uses

func Upgrade(w http.ResponseWriter, r *http.Request) (*Conn, error)

Global Upgrade for method for usage without a Upgrader instance. Refer to Upgrader.Upgrade for complete documentation.

func (*Conn) Close Uses

func (c *Conn) Close()

Forces the connection to close. The Conn object should not be used anymore.

func (*Conn) IsOpen Uses

func (c *Conn) IsOpen() bool

Returns whether the connection is still opened.

func (*Conn) Write Uses

func (c *Conn) Write(msg []byte) error

Sends a byte-slice to the connected client. Returns an error if the connection is already closed.

func (*Conn) WriteEvent Uses

func (c *Conn) WriteEvent(typ string, msg []byte) error

Sends a byte-slice to the connected client and triggers the specified event with the data. Returns an error if the connection is already closed.

func (*Conn) WriteEventWithID Uses

func (c *Conn) WriteEventWithID(id, typ string, msg []byte) error

Sends a byte-slice to the connected client and triggers the specified event with the data. Returns an error if the connection is already closed.

func (*Conn) WriteJson Uses

func (c *Conn) WriteJson(value interface{}) error

Sends a json-encoded struct to the connected client. Returns an error if the connection is already closed or if the encoding failed.

func (*Conn) WriteJsonEvent Uses

func (c *Conn) WriteJsonEvent(typ string, value interface{}) error

Sends a json-encoded struct to the connected client, targeting the specified event. Returns an error if the connection is already closed or if the encoding failed.

func (*Conn) WriteString Uses

func (c *Conn) WriteString(msg string) error

Sends a string to the connected client. Returns an error if the connection is already closed.

func (*Conn) WriteStringEvent Uses

func (c *Conn) WriteStringEvent(typ, msg string) error

Sends a string to the connected client, targeting the specified event. Returns an error if the connection is already closed.

func (*Conn) WriteXml Uses

func (c *Conn) WriteXml(value interface{}) error

Sends a xml-encoded struct to the connected client. Returns an error if the connection is already closed or if the encoding failed.

func (*Conn) WriteXmlEvent Uses

func (c *Conn) WriteXmlEvent(typ string, value interface{}) error

Sends a xml-encoded struct to the connected client, targeting the specified event. Returns an error if the connection is already closed or if the encoding failed.

type Event Uses

type Event struct {
    URI string

    Type string
    ID   string
    Data io.Reader
}

Event is a go representation of an http server-sent event

type Upgrader Uses

type Upgrader struct {
    // time between two connects from a client
    RetryTime time.Duration
}

func (Upgrader) Upgrade Uses

func (up Upgrader) Upgrade(w http.ResponseWriter, r *http.Request) (*Conn, error)

Takes over a HTTP-connection and returns a SSE-Connection, which can be used to send events. Returns an error, if the connection does not support streaming. Please note, that in this case the client will also be notified and the HTTP-connection should therefore not be used anymore.

Package sse imports 9 packages (graph) and is imported by 2 packages. Updated 2020-07-28. Refresh now. Tools for package owners.