websocket

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	sync.RWMutex
	UID        int32
	Conn       *websocket.Conn
	Send       chan Response
	OnClose    func(*Conn)          // 关闭回调函数
	OnMessage  func(*Conn, Request) // 消息回调函数
	IsAlive    bool                 // 连接是否活跃
	LastActive time.Time            // 最后活跃时间
	// contains filtered or unexported fields
}

func NewConn

func NewConn(w http.ResponseWriter, r *http.Request, uid int32, heartbeat bool) (*Conn, error)

func (*Conn) Close

func (c *Conn) Close()

Close 主动关闭连接

func (*Conn) DirectSend

func (c *Conn) DirectSend(msg Response) error

DirectSend 直接发送消息(绕过通道,用于紧急消息)

func (*Conn) GetLastActive

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

GetLastActive 获取最后活跃时间

func (*Conn) IsConnected

func (c *Conn) IsConnected() bool

IsConnected 检查连接是否活跃

func (*Conn) SafeSend

func (c *Conn) SafeSend(msg Response) bool

SafeSend 安全发送消息,避免通道阻塞

type Request

type Request struct {
	Type string          `json:"type"`
	Data json.RawMessage `json:"data"`
}

type Response

type Response struct {
	Type string `json:"type"`
	Data any    `json:"data"`
}

type SendError

type SendError struct {
	UID     int32
	Conn    *Conn
	Message string
}

func (*SendError) Error

func (e *SendError) Error() string

type WebSocket

type WebSocket struct {
	sync.RWMutex
	Groups        map[int32][]*Conn
	Error         error
	Stats         *WebSocketStats
	HeartbeatTime time.Duration
}

func GetSocket

func GetSocket() *WebSocket

func (*WebSocket) Add

func (ws *WebSocket) Add(w http.ResponseWriter, r *http.Request, uid int32, heartbeat bool, handler func(*Conn, Request) (Response, error)) (*Conn, error)

Add 添加新的WebSocket连接

func (*WebSocket) Broadcast

func (ws *WebSocket) Broadcast(resp Response) error

Broadcast 广播消息给所有用户

func (*WebSocket) Cleanup

func (ws *WebSocket) Cleanup()

Cleanup 清理所有连接

func (*WebSocket) GetActiveUsers

func (ws *WebSocket) GetActiveUsers() int

GetActiveUsers 获取活跃用户数

func (*WebSocket) GetAllConnections

func (ws *WebSocket) GetAllConnections() int

GetAllConnections 获取所有活跃连接数

func (*WebSocket) GetStats

func (ws *WebSocket) GetStats() WebSocketStats

GetStats 获取WebSocket统计信息

func (*WebSocket) GetUserConnections

func (ws *WebSocket) GetUserConnections(uid int32) int

GetUserConnections 获取指定用户的连接数

func (*WebSocket) Remove

func (ws *WebSocket) Remove(uid int32, conn *Conn) error

Remove 移除关闭的连接

func (*WebSocket) SetMessage

func (ws *WebSocket) SetMessage(uid int32, resp Response) error

SetMessage 向指定用户发送消息

type WebSocketStats

type WebSocketStats struct {
	sync.RWMutex
	TotalConnections  int64
	ActiveConnections int64
	MessagesSent      int64
	MessagesReceived  int64
}

Jump to

Keyboard shortcuts

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