websocket

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 6 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 initialized maps.

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.

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

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

Remove removes the client from all rooms and from the Hub.

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