eventsource

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Eventsource package provides the utilities to connect and process events from an HTML5 Server-Sent Events endpoint.

Index

Constants

View Source
const (
	ContentType = "text/event-stream"
)

Variables

View Source
var (
	// ErrContentType means the content-type header of the server is not the
	// expected one for an event-source. EventSource always expects
	// `text/event-stream`. content-type.
	ErrContentType = errors.New("eventsource: the content type of the stream is not allowed")

	// ErrUnauthorized means the server responded with an authorization error
	// status code.
	ErrUnauthorized = errors.New("eventsource: connection is unauthorized")
)

Functions

This section is empty.

Types

type EventSource

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

EventSource connects and processes events from an HTTP server-sent events stream.

func New

func New(url string, requestModifiers ...RequestModifier) (*EventSource, error)

New EventSource, it accepts requests modifiers which allow to modify the underlying HTTP request, see RequestModifier.

func (*EventSource) Close

func (es *EventSource) Close()

Close the event source. Once it has been closed, the event source cannot be re-used again.

func (*EventSource) MessageEvents

func (es *EventSource) MessageEvents() <-chan *base.MessageEvent

MessageEvents returns a receive-only channel where events are received.

func (*EventSource) ReadyState

func (es *EventSource) ReadyState() <-chan Status

ReadyState exposes a channel with updates on the ready state of the EventSource. It must be consumed together with MessageEvents.

func (*EventSource) URL

func (es *EventSource) URL() string

URL of the EventSource.

type ReadyState

type ReadyState uint16

ReadyState indicates the state of the EventSource.

const (
	// Connecting while trying to establish connection with the stream.
	Connecting ReadyState = iota
	// Open after connection is established with the server.
	Open
	// Closed after the connection is closed.
	Closed
)

func (ReadyState) String

func (i ReadyState) String() string

type RequestModifier

type RequestModifier func(r *http.Request)

RequestModifier function for modifying the HTTP connection request.

func WithBasicAuth

func WithBasicAuth(username, password string) RequestModifier

WithBasicAuth adds basic authentication to the HTTP request

func WithBearerTokenAuth

func WithBearerTokenAuth(token string) RequestModifier

WithBearerTokenAuth adds bearer token header to the HTTP request

type Status

type Status struct {
	Err        error
	ReadyState ReadyState
}

Status groups together a ready state and possible error associated with it. Useful to notify changes and why they happened.

func (*Status) Error

func (s *Status) Error() string

Error returns the error that accompanies the ready-state change, if any

Jump to

Keyboard shortcuts

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