websocket

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Echo

func Echo(conn *websocket.Conn, ctx context.Context)

Echo is a built-in handler that reads messages and echoes them back.

func Upgrader

func Upgrader(handler Handler, opts *Options) gin.HandlerFunc

Upgrader returns a gin.HandlerFunc that upgrades the HTTP connection to WebSocket and delegates to the provided handler. If opts is nil, all origins are accepted (InsecureSkipVerify = true).

Types

type Client

type Client struct {
	ID   string
	Conn *websocket.Conn
	// contains filtered or unexported fields
}

Client represents a single WebSocket connection registered with a Hub.

type Handler

type Handler func(conn *websocket.Conn, ctx context.Context)

Handler is a callback for handling a WebSocket connection.

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

func NewHubWithConfig(cfg HubConfig) *Hub

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

func (h *Hub) Clients(room string) []*Client

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

func (h *Hub) Join(client *Client, room string)

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

func (h *Hub) Leave(client *Client, room string)

Leave removes the client from the named room. The room is deleted if it becomes empty.

func (*Hub) OnJoin

func (h *Hub) OnJoin(fn func(client *Client, room string))

OnJoin registers a callback that fires when a client joins a room.

func (*Hub) OnLeave

func (h *Hub) OnLeave(fn func(client *Client, room string))

OnLeave registers a callback that fires when a client leaves a room (including when removed on disconnect).

func (*Hub) Remove

func (h *Hub) Remove(client *Client)

Remove removes the client from all rooms and from the Hub. Fires onLeaveFn for each room the client was in.

func (*Hub) Rooms

func (h *Hub) Rooms() []string

Rooms returns a snapshot of all active room names.

func (*Hub) Send

func (h *Hub) Send(clientID string, msgType websocket.MessageType, data []byte) error

Send delivers a message to a single client by ID. Returns an error if the client is not found.

type HubConfig

type HubConfig struct {
	PingInterval time.Duration // How often to send Ping frames. 0 disables heartbeat. Default: 30s.
	PongTimeout  time.Duration // How long to wait for Pong after Ping. Default: 10s.
}

HubConfig controls Hub behavior.

type Options

type Options struct {
	OriginPatterns     []string // Allowed origin patterns for CORS
	InsecureSkipVerify bool     // Skip origin verification (dev only)
}

Options configures the WebSocket upgrade.

Jump to

Keyboard shortcuts

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