ws

package
v1.56.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 14 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) Meta added in v1.52.0

func (c *Client) Meta() map[string]interface{}

Meta 返回客户端业务属性快照(线程安全)。

func (*Client) MetaValue added in v1.52.0

func (c *Client) MetaValue(key string) interface{}

MetaValue 读取单个业务属性;不存在返回 nil。

func (*Client) Send

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

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

func (*Client) SetMeta added in v1.52.0

func (c *Client) SetMeta(key string, value interface{})

SetMeta 设置客户端业务属性(线程安全)。

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) BroadcastRoom added in v1.52.0

func (h *Hub) BroadcastRoom(room string, data []byte)

BroadcastRoom 向房间内全部客户端广播(非阻塞,队列满丢弃)。

func (*Hub) Close

func (h *Hub) Close()

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

func (*Hub) Count

func (h *Hub) Count() int

Count 返回当前在线连接数。

func (*Hub) CountRoom added in v1.52.0

func (h *Hub) CountRoom(room string) int

CountRoom 返回房间在线连接数。

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) Join added in v1.52.0

func (h *Hub) Join(room string, client *Client)

Join 客户端加入房间(幂等,重复加入不重复计数)。 一个连接可同时属于多个房间(多屏/多会话场景)。

func (*Hub) Leave added in v1.52.0

func (h *Hub) Leave(room string, client *Client)

Leave 客户端离开房间(幂等)。

func (*Hub) OnJoin added in v1.52.0

func (h *Hub) OnJoin(fn RoomEvent) *Hub

OnJoin 注册客户端加入房间回调(人数统计/欢迎语/审计)。

func (*Hub) OnLeave added in v1.52.0

func (h *Hub) OnLeave(fn RoomEvent) *Hub

OnLeave 注册客户端离开房间回调(人数推送/资源回收)。

func (*Hub) Rooms added in v1.52.0

func (h *Hub) Rooms() []string

Rooms 返回当前非空房间列表(顺序不保证)。

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 设置心跳间隔。

func (*Hub) WithRedis added in v1.56.0

func (h *Hub) WithRedis(client *redis.Client, channel ...string) *Hub

WithRedis 启用跨节点广播:注册 Redis 客户端与 channel(默认 "gbx-ws-broadcast")。 必须在 Run 之前调用。

type RoomEvent added in v1.52.0

type RoomEvent func(h *Hub, c *Client, room string)

RoomEvent 是客户端进出房间的生命周期回调。 回调在房间表锁外执行,内部可安全调用 BroadcastRoom/CountRoom 等房间 API。

Jump to

Keyboard shortcuts

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