websocket

package
v3.1.19 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultManager = NewManager()

Functions

func GetMeta

func GetMeta[T any](c *Conn) (T, bool)

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 BatchConfig struct {
	Enabled    bool          // 是否启用批量
	MaxSize    int           // 最大批量大小
	MaxDelay   time.Duration // 最大延迟
	InitialCap int           // 初始容量
}

type Conn

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

func (*Conn) Close

func (c *Conn) Close()

func (*Conn) EnableBatch

func (c *Conn) EnableBatch(config BatchConfig)

func (*Conn) GetMeta

func (c *Conn) GetMeta() any

func (*Conn) Send

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

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

func (c *Conn) SetMeta(meta any)

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

func (m *Manager) Add(c *Conn)

func (*Manager) Broadcast

func (m *Manager) Broadcast(msg []byte)

func (*Manager) BroadcastWithType

func (m *Manager) BroadcastWithType(messageType MessageType, msg []byte)

func (*Manager) Remove

func (m *Manager) Remove(c *Conn)

type MessageType

type MessageType int
const (
	TextMessage   MessageType = websocket.TextMessage
	BinaryMessage MessageType = websocket.BinaryMessage
)

type Shard

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

type WS

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

func New

func New() *WS

func (*WS) Handler

func (w *WS) Handler() func(core.Ctx) error

func (*WS) OnClose

func (w *WS) OnClose(f func(*Conn)) *WS

func (*WS) OnConnect

func (w *WS) OnConnect(f func(*Conn)) *WS

func (*WS) OnMessage

func (w *WS) OnMessage(f func(*Conn, MessageType, []byte)) *WS

Jump to

Keyboard shortcuts

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