Documentation
¶
Index ¶
- Variables
- func GetMeta[T any](c *Conn) (T, bool)
- type BatchConfig
- type Conn
- func (c *Conn) Close()
- func (c *Conn) EnableBatch(config BatchConfig)
- func (c *Conn) GetMeta() any
- func (c *Conn) Send(data []byte)
- func (c *Conn) SendBatchWithType(messageType MessageType, data []byte)
- func (c *Conn) SendWithType(messageType MessageType, data []byte)
- func (c *Conn) SetMeta(meta any)
- type Manager
- type MessageType
- type Shard
- type WS
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultManager = NewManager()
Functions ¶
func GetMeta ¶
GetMeta retrieves the metadata of the connection in a type-safe manner
@param c *Conn the websocket connection @return T the metadata value @return bool whether the metadata exists and is of type T
e.g:
// 在 OnMessage 回调中获取
ws.OnMessage(func(conn *websocket.Conn, messageType int, data []byte) {
if userID, ok := websocket.GetMeta[string](conn); ok {
// 处理消息
}
})
Types ¶
type BatchConfig ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
func (*Conn) EnableBatch ¶
func (c *Conn) EnableBatch(config BatchConfig)
func (*Conn) SendBatchWithType ¶
func (c *Conn) SendBatchWithType(messageType MessageType, data []byte)
func (*Conn) SendWithType ¶
func (c *Conn) SendWithType(messageType MessageType, data []byte)
func (*Conn) SetMeta ¶
SetMeta sets the metadata for the connection
@param meta any
e.g:
// 在 OnConnect 回调中设置 meta
ws.OnConnect(func(conn *websocket.Conn) {
// 可以从 request 中获取用户信息,这里需要修改 Handler 传递用户信息
meta := &UserMeta{
UserID: "123",
Username: "john",
}
conn.SetMeta(meta)
})
// 在 OnMessage 回调中获取
ws.OnMessage(func(conn *websocket.Conn, messageType int, data []byte) {
if meta, ok := conn.GetMeta().(*UserMeta); ok {
userID := meta.UserID
// 处理消息
}
})
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func NewManager ¶
func NewManager() *Manager
func (*Manager) BroadcastWithType ¶
func (m *Manager) BroadcastWithType(messageType MessageType, msg []byte)
type MessageType ¶
type MessageType int
const ( TextMessage MessageType = websocket.TextMessage BinaryMessage MessageType = websocket.BinaryMessage )
Click to show internal directories.
Click to hide internal directories.