ws

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ws 提供 WebSocket 实时双向通信能力: 连接管理(Hub)、广播、业务消息回调与心跳保活。 标准 http.Handler 实现,可配合 iris.FromStd 或直接用于 net/http。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client 表示一个 WebSocket 连接。

func (*Client) ID

func (c *Client) ID() string

ID 返回客户端唯一标识。

func (*Client) Send

func (c *Client) Send(data []byte)

Send 向该客户端发送文本消息(非阻塞,队列满时丢弃)。

type Hub

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

Hub 管理全部 WebSocket 客户端。 消息处理通过 onMessage 回调注入;广播用 Broadcast。

func NewHub

func NewHub(onMessage func(client *Client, data []byte)) *Hub

NewHub 创建 WebSocket Hub;onMessage 为收到业务消息的回调(可 nil)。

func (*Hub) Broadcast

func (h *Hub) Broadcast(data []byte)

Broadcast 向全部连接广播文本消息。

func (*Hub) Close

func (h *Hub) Close()

Close 关闭全部连接并停止事件循环(幂等)。

func (*Hub) Count

func (h *Hub) Count() int

Count 返回当前在线连接数。

func (*Hub) Handle

func (h *Hub) Handle(w http.ResponseWriter, r *http.Request)

Handle 是标准 WebSocket 升级处理器。 用法:http.Handle("/ws", hub.Handle) 或 app.Get("/ws", iris.FromStd(hub.Handle))

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run 启动 Hub 事件循环(注册/注销/广播),ctx 取消时关闭全部连接。 应在 Handle 之前启动。

func (*Hub) WithPingInterval

func (h *Hub) WithPingInterval(interval time.Duration) *Hub

WithPingInterval 设置心跳间隔。

Jump to

Keyboard shortcuts

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