xwebsocket

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

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) AddHandlers

func (c *Client) AddHandlers(handlers ...Handler)

AddHandlers 添加消息处理器

func (*Client) AppID

func (c *Client) AppID() string

AppID 应用ID

func (*Client) Cancel

func (c *Client) Cancel() context.CancelFunc

Cancel 取消函数

func (*Client) Conn

func (c *Client) Conn() *websocket.Conn

Conn 连接

func (*Client) ConnID

func (c *Client) ConnID() string

ConnID 连接ID

func (*Client) ConnTime

func (c *Client) ConnTime() time.Time

ConnTime 连接时间

func (*Client) Ctx

func (c *Client) Ctx() context.Context

Ctx 上下文

func (*Client) IsAlive

func (c *Client) IsAlive() bool

IsAlive 是否存活

func (*Client) SendJSON

func (c *Client) SendJSON(v any) error

SendJSON 发送 json 消息

func (*Client) SendMsg

func (c *Client) SendMsg(msgData []byte, msgType ...int)

SendMsg 发送消息

func (*Client) SetHandlers

func (c *Client) SetHandlers(handlers ...Handler)

SetHandlers 设置消息处理器

func (*Client) UserID

func (c *Client) UserID() string

UserID 用户ID

type Handler

type Handler func(client *Client, msg *Msg)

Handler 消息处理器

type Manager

type Manager struct {
	Upgrader          *websocket.Upgrader // websocket 协议升级器
	ReadTimeout       time.Duration       // 读取超时时间,要大于 HeartbeatInterval
	WriteTimeout      time.Duration       // 写入超时时间
	HeartbeatInterval time.Duration       // 心跳间隔时间
	MaxMessageSize    int64               // 最大消息字节大小
	// contains filtered or unexported fields
}

Manager websocket 管理器

func NewManager

func NewManager() *Manager

NewManager 新建 websocket 管理器

func (*Manager) BroadcastMsg

func (m *Manager) BroadcastMsg(msgData []byte, msgType ...int)

BroadcastMsg 广播消息

func (*Manager) DelClient

func (m *Manager) DelClient(connID string)

DelClient 删除 websocket 客户端

func (*Manager) GetClient

func (m *Manager) GetClient(connID string) (*Client, bool)

GetClient 获取 websocket 客户端

func (*Manager) GetClients

func (m *Manager) GetClients() []*Client

GetClients 获取所有 websocket 客户端

func (*Manager) GetUserClient

func (m *Manager) GetUserClient(userID, appID string) (*Client, bool)

GetUserClient 获取用户 websocket 客户端

func (*Manager) GetUserClients

func (m *Manager) GetUserClients(userID string) ([]*Client, bool)

GetUserClients 获取用户 websocket 客户端列表

func (*Manager) SendMsgToClient

func (m *Manager) SendMsgToClient(connID string, msgData []byte, msgType ...int)

SendMsgToClient 发送消息至指定客户端

func (*Manager) SendMsgToUser

func (m *Manager) SendMsgToUser(userID string, msgData []byte, msgType ...int)

SendMsgToUser 发送消息至指定用户

func (*Manager) UpgradeClient

func (m *Manager) UpgradeClient(w http.ResponseWriter, r *http.Request, opts ...Option) (*Client, error)

UpgradeClient 将 http 连接升级为 websocket 客户端

type Msg

type Msg struct {
	Type int    // 类型
	Data []byte // 内容
}

Msg websocket 消息

type Option

type Option func(c *Client)

Option 可选配置

func WithAppID

func WithAppID(appID string) Option

WithAppID 配置应用ID,可用于区分该 websocket 连接来自哪个应用,常见如:"web", "android" 和 "ios"

func WithChanSize

func WithChanSize(chanSize int) Option

WithChanSize 配置通道大小

func WithConnTime

func WithConnTime(connTime time.Time) Option

WithConnTime 配置连接时间

func WithHandlers

func WithHandlers(handlers ...Handler) Option

WithHandlers 配置消息处理器,可用于集中处理从客户端读取到的消息

func WithUserID

func WithUserID(userID string) Option

WithUserID 配置用户ID,可用于区分该 websocket 连接属于哪个用户

Jump to

Keyboard shortcuts

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