sse

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyStarted = errors.New("sse stream already started")
)

Functions

func AsSSE

func AsSSE(handler func(*Stream) error) httpx.RespOption

AsSSE 设置为 SSE 流式响应

func AsSSEEvent

func AsSSEEvent(events ...Event) httpx.RespOption

AsSSEEvent 发送单个或多个 SSE 事件

func AsSSEWithHeartbeat

func AsSSEWithHeartbeat(heartbeat time.Duration, handler func(*Stream) error) httpx.RespOption

AsSSEWithHeartbeat 带心跳的 SSE

Types

type Event

type Event struct {
	Event string      `json:"event,omitempty"`
	ID    string      `json:"id,omitempty"`
	Data  interface{} `json:"data"`
	Retry int         `json:"retry,omitempty"`
}

Event represents a single SSE event.

type Stream

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

Stream is a Server-Sent Events stream controller.

func NewStream

func NewStream(ctx *gin.Context) *Stream

NewStream creates a new SSE stream for the given context.

func (*Stream) Close

func (s *Stream) Close() error

Close closes the stream and stops the heartbeat.

func (*Stream) Send

func (s *Stream) Send(event Event) error

Send writes an event to the stream.

func (*Stream) SendEvent

func (s *Stream) SendEvent(eventType string, data interface{}) error

SendEvent sends a typed event.

func (*Stream) SendJSON

func (s *Stream) SendJSON(data interface{}) error

SendJSON sends data as a JSON SSE event of type "message".

func (*Stream) Start

func (s *Stream) Start() error

Start marks the stream as started. Returns ErrAlreadyStarted if called more than once.

func (*Stream) StartHeartbeat

func (s *Stream) StartHeartbeat(interval time.Duration)

StartHeartbeat starts a background goroutine that sends keep-alive comments.

Jump to

Keyboard shortcuts

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