Documentation
¶
Overview ¶
Package confws 提供可配置的 WebSocket 服务端:
- 服务配置: Endpoint / Option(监听、超时、TLS、容量等)
- 客户端管理: Client / ClientManager(登记、摘表、踢除、关闭)
- 回调接口(用法与 error 语义见各 Handler 类型注释):
- Connection: Upgrade 前门禁;error → HTTP 401、不建 WS
- Establish: 登记后业务握手;error → Close Client、不进消息循环
- Message 及读写失败、断开钩子
Index ¶
- Variables
- type Client
- type ClientManager
- type ClientOptionApplier
- func WithClientDisconnectionHandler(h DisconnectionHandler) ClientOptionApplier
- func WithClientEstablishHandler(h EstablishHandler) ClientOptionApplier
- func WithClientMessageHandler(h MessageHandler) ClientOptionApplier
- func WithClientReadFailureHandler(h ReadFailureHandler) ClientOptionApplier
- func WithClientWriteFailureHandler(h WriteFailureHandler) ClientOptionApplier
- func WithIdleTimeout(d time.Duration) ClientOptionApplier
- func WithWriteTimeout(d time.Duration) ClientOptionApplier
- type ConnectionHandler
- type DisconnectionHandler
- type Endpoint
- func (e *Endpoint) Client(id string) Client
- func (e *Endpoint) Close(ctx context.Context) error
- func (e *Endpoint) Init(ctx context.Context) (err error)
- func (e *Endpoint) Run(ctx context.Context) (err error)
- func (e *Endpoint) Session(id string) Client
- func (e *Endpoint) SetDefault()
- func (e *Endpoint) String() string
- func (e *Endpoint) WithContext(ctx context.Context) context.Context
- type Error
- type EstablishHandler
- type MessageHandler
- type Option
- type ReadFailureHandler
- type Session
- type WriteFailureHandler
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
// Close 关闭连接并从管理器 Detach;可重复调用.
Close(ctx context.Context) error
// Done 关闭完成信号.
Done() <-chan struct{}
// Err 关闭原因;未关闭时为 nil.
Err() error
// Read 拉模式读一帧;Push 模式返回 ERROR__READ_ON_NON_PULL_CLIENT.
Read(ctx context.Context) (t int, data []byte, err error)
// WriteBinary 写二进制帧;失败将关闭连接.
WriteBinary(ctx context.Context, data []byte) error
// WriteText 写文本帧;失败将关闭连接.
WriteText(ctx context.Context, data []byte) error
// LastActivity 最近读写时间.
LastActivity() time.Time
// ID 连接标识(ULID,由框架生成).
ID() string
// Underlying 返回原始http请求
Underlying() *http.Request
}
Client 已升级的 WebSocket 会话.
Push(有 MessageHandler):框架读循环回调,业务勿调用 Read. Pull(无 MessageHandler):业务自行 Read;写均经 WriteText/WriteBinary.
type ClientManager ¶
type ClientManager interface {
// Add 登记客户端;同 id 已存在则关闭旧连接并替换.
Add(ctx context.Context, c Client) error
// Detach 仅当 map 中仍是该实例时摘表.
Detach(c Client)
// Remove 关闭指定客户端.
Remove(ctx context.Context, id string) error
// Get 返回指定客户端;不存在返回 nil.
Get(id string) Client
// Activities 当前登记连接数.
Activities() int
// Close 关闭所有登记连接.
Close(ctx context.Context) error
}
ClientManager 在线连接登记表.
Detach: 仅摘表(不关连接;Client.Close 内部调用) Remove: 关闭指定连接(经 Client.Close → Detach) Close: 关闭全部连接
type ClientOptionApplier ¶
type ClientOptionApplier func(*clientOption)
ClientOptionApplier 覆盖单连接 clientOption(由 ConnectionHandler 返回).
func WithClientDisconnectionHandler ¶
func WithClientDisconnectionHandler(h DisconnectionHandler) ClientOptionApplier
WithClientDisconnectionHandler 设置本连接断开钩子.
func WithClientEstablishHandler ¶
func WithClientEstablishHandler(h EstablishHandler) ClientOptionApplier
WithClientEstablishHandler 覆盖本连接 Establish 钩子.
func WithClientMessageHandler ¶
func WithClientMessageHandler(h MessageHandler) ClientOptionApplier
WithClientMessageHandler 覆盖本连接 Message 钩子.
func WithClientReadFailureHandler ¶
func WithClientReadFailureHandler(h ReadFailureHandler) ClientOptionApplier
WithClientReadFailureHandler 设置本连接读失败钩子.
func WithClientWriteFailureHandler ¶
func WithClientWriteFailureHandler(h WriteFailureHandler) ClientOptionApplier
WithClientWriteFailureHandler 设置本连接写失败钩子.
func WithIdleTimeout ¶
func WithIdleTimeout(d time.Duration) ClientOptionApplier
WithIdleTimeout 覆盖本连接空闲超时;<=0 禁用.
func WithWriteTimeout ¶
func WithWriteTimeout(d time.Duration) ClientOptionApplier
WithWriteTimeout 覆盖本连接写超时.
type ConnectionHandler ¶
type ConnectionHandler func(ctx context.Context, r *http.Request) (context.Context, []ClientOptionApplier, error)
ConnectionHandler 在 Upgrade 之前调用(尚无 Client).
推荐: 读 Header/Query 做门禁或粗鉴权、可回写会话信息等 按需返回本连接 ClientOptionApplier;细鉴权/欢迎帧放到 Establish.
返回 error:框架不 Upgrade,以 HTTP 401 写回 err 文案并结束本次请求. 返回的 ctx 为 nil 时沿用入参 ctx.
type DisconnectionHandler ¶
DisconnectionHandler 连接断开回调(仅触发一次;无 Client,避免误 Close).
type Endpoint ¶
type Endpoint struct {
Option
// contains filtered or unexported fields
}
Endpoint WebSocket 服务入口:配置、监听、连接生命周期.
生命周期: Init(资源) → Run(监听) → upgrade(...) → Close.
type Error ¶
type Error int8
Error confws 错误码. +genx:code @code domain=CONFWS
const ( ERROR_UNDEFINED Error = iota ERROR__ENDPOINT_NOT_INITIALIZED ERROR__ENDPOINT_INITIALIZED ERROR__SERVER_CLOSED ERROR__MISSING_REQUIRED_HOOKS ERROR__TOO_MANY_CONNECTION ERROR__CONNECTION_CALLBACK_FAILED ERROR__FAILED_TO_LISTENING ERROR__FAILED_TO_REGISTER_CLIENT ERROR__FAILED_TO_UPGRADE ERROR__CLIENT_CLOSED ERROR__CLIENT_IDLE_TIMEOUT ERROR__READ_ON_NON_PULL_CLIENT )
type EstablishHandler ¶
EstablishHandler 在 Client 已登记之后、消息循环之前调用(已有稳定 WS).
推荐: 业务握手(二次校验、欢迎帧、订阅/拉模式启动)、可回写上下文(如鉴权结果等) 供后续 Message / Pull 使用;需要拒连时先经 cli 下发业务错误帧再返回 error.
返回 error:框架关闭该 Client 并摘表,不进入消息循环. 返回的 ctx 为 nil 时沿用入参 ctx.
type MessageHandler ¶
MessageHandler Push 模式收消息回调;业务需主动 Close 时自行调用 cli.Close.
type Option ¶
type Option struct {
// ListenAddr 监听地址,例如 0.0.0.0:10165.
ListenAddr string `url:",default=80"`
// Path HTTP upgrade 路径.
Path string `url:",default=/"`
// HandshakeTimeout 握手超时.
HandshakeTimeout types.Duration `url:",default=5s"`
// WriteTimeout 单次写超时.
WriteTimeout types.Duration `url:",default=5s"`
// IdleTimeout 空闲回收超时;<=0 禁用(SetDefault 后默认为 5m).
IdleTimeout types.Duration `url:",default=5m"`
// CheckOriginAllowAll 为 true 时升级握手放行任意 Origin.
CheckOriginAllowAll bool `url:",default=true"`
// MaxMessageSize 单帧/消息最大字节.
MaxMessageSize int `url:",default=32768"`
// MaxConnection 最大同时在线连接数.
MaxConnection int `url:",default=65536"`
// Cert TLS 证书;零值表示明文.
Cert conftls.X509KeyPair
// contains filtered or unexported fields
}
Option Endpoint 静态配置与默认钩子.
func (*Option) SetConnectionHandler ¶
func (o *Option) SetConnectionHandler(h ConnectionHandler)
SetConnectionHandler 设置默认 Connection 钩子.
func (*Option) SetEstablishHandler ¶
func (o *Option) SetEstablishHandler(h EstablishHandler)
SetEstablishHandler 设置默认 Establish 钩子.
func (*Option) SetMessageHandler ¶
func (o *Option) SetMessageHandler(h MessageHandler)
SetMessageHandler 设置默认 Message 钩子(Push 模式).
func (*Option) ValidateHooks ¶
ValidateHooks 至少配置 Message(Push) 或 Establish(可在其中自管 Pull).
type ReadFailureHandler ¶
ReadFailureHandler 读失败回调(cid + 原因);随后连接会关闭.