sse

package
v0.84.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package sse SSE 的服务端实现

Index

Constants

View Source
const Mimetype = "text/event-stream"

Variables

This section is empty.

Functions

This section is empty.

Types

type SSE

type SSE[T comparable] struct {
	// contains filtered or unexported fields
}

SSE sse 事件管理

T 表示用于区分不同事件源的 ID,比如按用户区分, 那么该类型可能是 int64 类型的用户 ID 值。

func New

func New[T comparable](s *web.Server, status int, timeout, freq time.Duration) *SSE[T]

New 声明 SSE 对象

status 表示正常情况下 SSE 返回的状态码; timeout 链接的空闲时间,超过此值将被回收; freq 回收程序执行的频率;

func (*SSE[T]) Get

func (sse *SSE[T]) Get(id T) *Source

Get 返回指定 ID 的事件源

仅在 SSE.NewSource 执行之后,此函数才能返回非空值。

func (*SSE[T]) Len

func (sse *SSE[T]) Len() (size int)

Len 当前链接的数量

func (*SSE[T]) NewSource

func (sse *SSE[T]) NewSource(id T, ctx *web.Context) (s *Source, wait func())

NewSource 声明新的事件源

NOTE: 只有采用此方法声明之后,才有可能通过 SSE.Get 获取实例。 id 表示是事件源的唯一 ID,如果事件是根据用户进行区分的,那么该值应该是表示用户的 ID 值; wait 当前 s 退出时,wait 才会返回,可以在 web.Handler 中阻止路由退出。

type Source

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

func (*Source) Close

func (s *Source) Close()

关闭当前事件源

这将导致关联的 [Server.NewSource] 的 wait 直接返回。

func (*Source) Sent

func (s *Source) Sent(data []string, event, id string, retry uint)

Sent 发送消息

id、event 和 retry 都可以为空,表示不需要这些值;

Jump to

Keyboard shortcuts

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