sse

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultBufferSize = 1024

DefaultBufferSize size of the queue that holds the streams messages.

Variables

This section is empty.

Functions

func SetDefault

func SetDefault(v *Server)

func WithContext

func WithContext(ctx context.Context, v *Server) context.Context

Types

type Event

type Event struct {

	// 事件 ID,会成为当前 EventSource 对象的内部属性“最后一个事件 ID”的属性值。
	ID []byte
	// 消息的数据字段。
	// 当 EventSource 接收到多个以 data: 开头的连续行时,会将它们连接起来,在它们之间插入一个换行符。
	// 末尾的换行符会被删除。
	Data []byte
	// 一个用于标识事件类型的字符串。
	// 如果指定了这个字符串,浏览器会将具有指定事件名称的事件分派给相应的监听器;
	// 网站源代码应该使用 addEventListener() 来监听指定的事件。
	// 如果一个消息没有指定事件名称,那么 onmessage 处理程序就会被调用。
	Event []byte
	// 重新连接的时间。
	// 如果与服务器的连接丢失,浏览器将等待指定的时间,然后尝试重新连接。
	// 这必须是一个整数,以毫秒为单位指定重新连接的时间。
	// 如果指定了一个非整数值,该字段将被忽略。
	Retry   []byte
	Comment []byte
	// contains filtered or unexported fields
}

Event holds all of the event source fields

type EventLog

type EventLog []*Event

EventLog holds all of previous events

func (*EventLog) Add

func (e *EventLog) Add(ev *Event)

Add event to eventlog

func (*EventLog) Clear

func (e *EventLog) Clear()

Clear events from eventlog

func (*EventLog) Replay

func (e *EventLog) Replay(s *Subscriber)

Replay events to a subscriber

type EventStreamReader

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

EventStreamReader scans an io.Reader looking for EventStream messages.

func NewEventStreamReader

func NewEventStreamReader(eventStream io.Reader, maxBufferSize int) *EventStreamReader

NewEventStreamReader creates an instance of EventStreamReader.

func (*EventStreamReader) ReadEvent

func (e *EventStreamReader) ReadEvent() ([]byte, error)

ReadEvent scans the EventStream for events.

type Server

type Server struct {
	// Extra headers adding to the HTTP response to each client
	Headers map[string]string
	// Sets a ttl that prevents old events from being transmitted
	EventTTL time.Duration
	// Specifies the size of the message buffer for each stream
	BufferSize int
	// Encodes all data as base64
	EncodeBase64 bool
	// Splits an events data into multiple data: entries
	SplitData bool
	// Enables creation of a stream when a client connects
	AutoStream bool
	// Enables automatic replay for each new subscriber that connects
	AutoReplay bool

	// Specifies the function to run when client subscribe or un-subscribe
	OnSubscribe   func(streamID string, sub *Subscriber)
	OnUnsubscribe func(streamID string, sub *Subscriber)
	// contains filtered or unexported fields
}

Server Is our main struct

func Default

func Default() *Server

func FromContext

func FromContext(ctx context.Context) *Server

func New

func New() *Server

New will create a server and setup defaults

func NewWithCallback

func NewWithCallback(onSubscribe, onUnsubscribe func(streamID string, sub *Subscriber)) *Server

NewWithCallback will create a server and setup defaults with callback function

func (*Server) Close

func (s *Server) Close()

Close shuts down the server, closes all of the streams and connections

func (*Server) CreateStream

func (s *Server) CreateStream(id string) *Stream

CreateStream will create a new stream and register it

func (*Server) Publish

func (s *Server) Publish(id string, event *Event)

Publish sends a mesage to every client in a streamID. If the stream's buffer is full, it blocks until the message is sent out to all subscribers (but not necessarily arrived the clients), or when the stream is closed.

func (*Server) RemoveStream

func (s *Server) RemoveStream(id string)

RemoveStream will remove a stream

func (*Server) ServeHTTP

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

ServeHTTP serves new connections with events for a given stream ...

func (*Server) StreamExists

func (s *Server) StreamExists(id string) bool

StreamExists checks whether a stream by a given id exists

func (*Server) TryPublish

func (s *Server) TryPublish(id string, event *Event) bool

TryPublish is the same as Publish except that when the operation would cause the call to be blocked, it simply drops the message and returns false. Together with a small BufferSize, it can be useful when publishing the latest message ASAP is more important than reliable delivery.

type Stream

type Stream struct {
	ID string

	Eventlog EventLog

	// Enables replaying of eventlog to newly added subscribers
	AutoReplay bool

	// Specifies the function to run when client subscribe or un-subscribe
	OnSubscribe   func(streamID string, sub *Subscriber)
	OnUnsubscribe func(streamID string, sub *Subscriber)
	// contains filtered or unexported fields
}

Stream ...

type Subscriber

type Subscriber struct {
	URL *url.URL
	// contains filtered or unexported fields
}

Subscriber ...

Jump to

Keyboard shortcuts

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