sseclient

package
v0.55.2 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2024 License: BSD-3-Clause Imports: 12 Imported by: 1

Documentation

Overview

Package sseclient implement HTTP client for Server-Sent Events (SSE).

Notes on implementation

The SSE specification have inconsistent state when dispatching empty data. In the "9.2.6 Interpreting an event stream", if the data buffer is empty it would return; but in the third example it can dispatch an empty string. In this implement we ignore an empty string in server and client.

References,

Index

Constants

View Source
const (
	// EventTypeOpen is set when connection succesfully established.
	// The passed [Event.Data] is empty.
	EventTypeOpen = `open`

	// EventTypeMessage is set when client received message from server,
	// possibly with new ID.
	EventTypeMessage = `message`

	EventTypeError = `error`
)

List of system event type.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	C <-chan Event

	// Endpoint define the HTTP server URL to connect.
	Endpoint string

	// LastEventID define the last event ID to be sent during handshake.
	// Once the handshake success, this field will be reset and may set
	// with next ID from server.
	// This field is optional.
	LastEventID string

	// Timeout define the read and write timeout when reading and
	// writing from/to server.
	// This field is optional default to 10 seconds.
	Timeout time.Duration

	// Retry define how long, in milliseconds, the client should wait
	// before reconnecting back to server after disconnect.
	// Zero or negative value disable it.
	//
	// This field is optional, default to 0 (not retrying).
	Retry time.Duration

	// Insecure allow connect to HTTPS endpoint with invalid
	// certificate.
	Insecure bool
	// contains filtered or unexported fields
}

Client for SSE. Once the Client filled, user need only to call Connect to start receiving message from channel C.

func (*Client) Close

func (cl *Client) Close() (err error)

Close the connection and release all resources.

func (*Client) Connect

func (cl *Client) Connect(header http.Header) (err error)

Connect to server and start consume the message and propagate to each registered handlers.

The header parameter define custom, optional HTTP header to be sent during handshake. The following header cannot be set: Host, User-Agent, and Accept.

type Event

type Event struct {
	Type string
	Data string
	ID   string
}

Event contains SSE message from server or client status.

func (*Event) IDInt

func (ev *Event) IDInt() (id int64)

IDInt return the ID as int64. If the ID cannot be converted to integer it would return 0.

Jump to

Keyboard shortcuts

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