Documentation
¶
Index ¶
- func Echo(conn *websocket.Conn, ctx context.Context)
- func Upgrader(handler Handler, opts *Options) gin.HandlerFunc
- type Client
- type Handler
- type Hub
- func (h *Hub) Broadcast(room string, msgType websocket.MessageType, data []byte)
- func (h *Hub) BroadcastOthers(room string, sender *Client, msgType websocket.MessageType, data []byte)
- func (h *Hub) Clients(room string) []*Client
- func (h *Hub) Handler(onConnect func(*Client)) gin.HandlerFunc
- func (h *Hub) Join(client *Client, room string)
- func (h *Hub) Leave(client *Client, room string)
- func (h *Hub) OnJoin(fn func(client *Client, room string))
- func (h *Hub) OnLeave(fn func(client *Client, room string))
- func (h *Hub) Remove(client *Client)
- func (h *Hub) Rooms() []string
- func (h *Hub) Send(clientID string, msgType websocket.MessageType, data []byte) error
- type HubConfig
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub manages WebSocket clients and rooms.
func NewHub ¶
func NewHub() *Hub
NewHub creates an empty Hub with default config (30s ping, 10s pong timeout).
func NewHubWithConfig ¶ added in v2.1.0
NewHubWithConfig creates an empty Hub with the given configuration.
func (*Hub) Broadcast ¶
func (h *Hub) Broadcast(room string, msgType websocket.MessageType, data []byte)
Broadcast sends a message to all clients in the named room.
func (*Hub) BroadcastOthers ¶
func (h *Hub) BroadcastOthers(room string, sender *Client, msgType websocket.MessageType, data []byte)
BroadcastOthers sends a message to all clients in the named room except the sender.
func (*Hub) Clients ¶
Clients returns a snapshot of all clients in the named room. Returns nil if the room does not exist.
func (*Hub) Handler ¶
func (h *Hub) Handler(onConnect func(*Client)) gin.HandlerFunc
Handler returns a gin.HandlerFunc that upgrades the HTTP connection to WebSocket, creates a Client, registers it with the Hub, and calls onConnect. The client is automatically removed when onConnect returns. If PingInterval > 0, a heartbeat goroutine detects dead connections.
func (*Hub) Join ¶
Join adds the client to the named room. The room is created if it does not exist. Joining the same room twice is a no-op.
func (*Hub) Leave ¶
Leave removes the client from the named room. The room is deleted if it becomes empty.
func (*Hub) OnJoin ¶ added in v2.1.0
OnJoin registers a callback that fires when a client joins a room.
func (*Hub) OnLeave ¶ added in v2.1.0
OnLeave registers a callback that fires when a client leaves a room (including when removed on disconnect).
func (*Hub) Remove ¶
Remove removes the client from all rooms and from the Hub. Fires onLeaveFn for each room the client was in.