server

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSendFull = errors.New("send channel full")

ErrSendFull 发送通道满(背压)

Functions

This section is empty.

Types

type Conn

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

Conn WebSocket 连接(服务端侧)

func (*Conn) Close

func (c *Conn) Close() error

Close 关闭连接

func (*Conn) CodecName

func (c *Conn) CodecName() string

CodecName 获取当前连接使用的编码器名称

func (*Conn) Context

func (c *Conn) Context() context.Context

Context 获取连接上下文(取消时连接关闭)

func (*Conn) GetMeta

func (c *Conn) GetMeta(key string) (any, bool)

GetMeta 获取元数据(线程安全)

func (*Conn) ID

func (c *Conn) ID() string

ID 获取连接唯一标识

func (*Conn) LastActiveTime

func (c *Conn) LastActiveTime() time.Time

LastActiveTime 获取最后活跃时间

func (*Conn) SendResp

func (c *Conn) SendResp(resp *types.Response) error

SendResp 发送响应(非阻塞,缓冲区满返回 ErrSendFull)

func (*Conn) SetMeta

func (c *Conn) SetMeta(key string, val any)

SetMeta 设置元数据(线程安全)

func (*Conn) Subscribe

func (c *Conn) Subscribe(topics ...string)

Subscribe 订阅 topic(幂等,重复订阅不报错)

func (*Conn) Subscriptions

func (c *Conn) Subscriptions() []string

Subscriptions 获取当前连接已订阅的所有 topic

func (*Conn) Unsubscribe

func (c *Conn) Unsubscribe(topics ...string)

Unsubscribe 取消订阅 topic

type ConnManager

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

func (*ConnManager) Count

func (cm *ConnManager) Count() int64

Count 当前连接总数

func (*ConnManager) Get

func (cm *ConnManager) Get(id string) *Conn

Get 根据连接 ID 获取连接

func (*ConnManager) Range

func (cm *ConnManager) Range(fn func(*Conn) bool)

Range 遍历所有连接,fn 返回 false 停止

type Handler

type Handler func(*Conn, *types.Request)

Handler 消息处理函数类型

type Middleware

type Middleware func(Handler) Handler

Middleware 中间件函数类型

type Server

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

Server WebSocket 服务端 实现 http.Handler 接口,可接入用户自建的 HTTP 服务器

func NewServer

func NewServer(opts ...ServerOption) *Server

NewServer 创建 WebSocket 服务端

func (*Server) Broadcast

func (s *Server) Broadcast(resp *types.Response)

Broadcast 向所有连接广播消息

func (*Server) BroadcastFilter

func (s *Server) BroadcastFilter(resp *types.Response, filter func(*Conn) bool)

BroadcastFilter 向满足条件的连接广播消息

func (*Server) Codec

func (s *Server) Codec() codec.Codec

Codec 获取编解码器

func (*Server) ConnManager

func (s *Server) ConnManager() *ConnManager

ConnManager 获取连接管理器

func (*Server) Handle

func (s *Server) Handle(action string, handler Handler)

Handle 注册消息处理器(线程安全,支持运行时注册)

func (*Server) OnConnect

func (s *Server) OnConnect(fn func(*Conn, *http.Request))

OnConnect 设置连接建立回调

func (*Server) OnDisconnect

func (s *Server) OnDisconnect(fn func(*Conn))

OnDisconnect 设置连接断开回调

func (*Server) Publish

func (s *Server) Publish(topic string, resp *types.Response)

Publish 向某 topic 的所有订阅者发布消息

func (*Server) PublishFilter

func (s *Server) PublishFilter(topic string, resp *types.Response, filter func(*Conn) bool)

PublishFilter 向某 topic 中满足条件的订阅者发布消息

func (*Server) ServeHTTP

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

ServeHTTP 实现 http.Handler 接口

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown 优雅关闭 标记关闭阻止新连接,遍历关闭所有已有连接

func (*Server) TopicCount

func (s *Server) TopicCount(topic string) int

TopicCount 获取某 topic 的订阅者数量

func (*Server) Topics

func (s *Server) Topics() []string

Topics 获取所有有订阅者的 topic 列表

func (*Server) Use

func (s *Server) Use(middleware ...Middleware)

Use 注册中间件

type ServerOption

type ServerOption func(*serverConfig)

ServerOption 服务端配置选项

func WithServerAllowedOrigins

func WithServerAllowedOrigins(fn func(origin string) bool) ServerOption

WithServerAllowedOrigins 设置允许的跨域来源验证函数

func WithServerCodec

func WithServerCodec(name string) ServerOption

WithServerCodec 设置编解码器,支持 "json"/"msgpack"/"protobuf",默认 "json"

func WithServerHeartbeat

func WithServerHeartbeat(d time.Duration) ServerOption

WithServerHeartbeat 设置空闲超时检测间隔,0 禁用

func WithServerMaxConns

func WithServerMaxConns(n int) ServerOption

WithServerMaxConns 设置最大连接数

func WithServerMaxMessageSize

func WithServerMaxMessageSize(size int) ServerOption

WithServerMaxMessageSize 设置最大消息大小(字节),0 不限制

func WithServerSendBufferSize

func WithServerSendBufferSize(n int) ServerOption

WithServerSendBufferSize 设置每连接发送缓冲区大小

Jump to

Keyboard shortcuts

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