websocket

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewUpgrader

func NewUpgrader(config Config, checkOrigin OriginChecker) *websocket.Upgrader

NewUpgrader creates a new WebSocket upgrader

func Upgrade

func Upgrade(w http.ResponseWriter, r *http.Request, config Config, checkOrigin OriginChecker) (*websocket.Conn, error)

Upgrade upgrades an HTTP connection to a WebSocket connection

Types

type Client

type Client struct {
	// ID is the unique identifier for this client connection
	ID string
	// UserID is the user identifier
	UserID string
	// TenantID is the tenant identifier
	TenantID string
	// Token stores JWT token for authenticated API calls
	Token string
	// Metadata stores custom key-value data for application use
	Metadata map[string]interface{}
	// contains filtered or unexported fields
}

Client represents a WebSocket client connection

func NewClient

func NewClient(
	id string,
	userID string,
	tenantID string,
	conn *websocket.Conn,
	hub HubInterface,
	logger api.Logger,
	config Config,
	opts ...ClientOption,
) *Client

NewClient creates a new WebSocket client

func (*Client) Close

func (c *Client) Close()

Close closes the client connection

func (*Client) GetMetadata

func (c *Client) GetMetadata(key string) (interface{}, bool)

GetMetadata retrieves metadata value by key

func (*Client) ReadPump

func (c *Client) ReadPump()

ReadPump pumps messages from the WebSocket connection to the message handler This should be run in a goroutine

func (*Client) Send

func (c *Client) Send(message []byte) bool

Send sends a message to the client (non-blocking)

func (*Client) SendJSON

func (c *Client) SendJSON(v interface{}) error

SendJSON marshals and sends a JSON message to the client

func (*Client) SetMetadata

func (c *Client) SetMetadata(key string, value interface{})

SetMetadata sets a metadata value

func (*Client) Start

func (c *Client) Start()

Start begins the read and write pumps for this client This should be called after the client is registered with the hub

func (*Client) WritePump

func (c *Client) WritePump()

WritePump pumps messages from the send channel to the WebSocket connection This should be run in a goroutine

type ClientOption

type ClientOption func(*Client)

ClientOption is a functional option for configuring a Client

func WithDisconnectHandler

func WithDisconnectHandler(handler DisconnectHandler) ClientOption

WithDisconnectHandler sets the disconnect handler for the client

func WithMessageHandler

func WithMessageHandler(handler MessageHandler) ClientOption

WithMessageHandler sets the message handler for the client

func WithMetadata

func WithMetadata(key string, value interface{}) ClientOption

WithMetadata sets custom metadata for the client

func WithToken

func WithToken(token string) ClientOption

WithToken sets the JWT token for the client

type Config

type Config struct {
	// WriteWait is the time allowed to write a message to the peer
	WriteWait time.Duration
	// PongWait is the time allowed to read the next pong message from the peer
	PongWait time.Duration
	// PingPeriod is the period to send pings to the peer (must be less than PongWait)
	PingPeriod time.Duration
	// MaxMessageSize is the maximum message size allowed from peer
	MaxMessageSize int64
	// SendBufferSize is the size of the send channel buffer
	SendBufferSize int
	// ReadBufferSize is the WebSocket read buffer size
	ReadBufferSize int
	// WriteBufferSize is the WebSocket write buffer size
	WriteBufferSize int
}

Config holds WebSocket configuration

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns default WebSocket configuration

type DisconnectHandler

type DisconnectHandler func(client *Client)

DisconnectHandler is a callback for handling client disconnection

type Hub

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

Hub manages WebSocket client connections

func NewHub

func NewHub(logger api.Logger) *Hub

NewHub creates a new WebSocket hub

func (*Hub) BroadcastAll

func (h *Hub) BroadcastAll(message []byte) int

BroadcastAll sends a message to all connected clients

func (*Hub) DisconnectUser

func (h *Hub) DisconnectUser(userID string)

DisconnectUser closes all connections for a user

func (*Hub) GetClient

func (h *Hub) GetClient(userID, clientID string) (*Client, bool)

GetClient returns a client by userID and clientID

func (*Hub) GetConnectedUserIDs

func (h *Hub) GetConnectedUserIDs(tenantID string) []string

GetConnectedUserIDs returns all connected user IDs (optionally filtered by tenant)

func (*Hub) GetGroupClients

func (h *Hub) GetGroupClients(groupID string) []*Client

GetGroupClients returns all clients in a group

func (*Hub) GetGroupUserIDs

func (h *Hub) GetGroupUserIDs(groupID string) []string

GetGroupUserIDs returns all user IDs in a group

func (*Hub) GetUserClients

func (h *Hub) GetUserClients(userID string) []*Client

GetUserClients returns all clients for a user

func (*Hub) HasActiveConnection

func (h *Hub) HasActiveConnection(userID string) bool

HasActiveConnection checks if a user has any active connections

func (*Hub) JoinGroup

func (h *Hub) JoinGroup(groupID string, client *Client)

JoinGroup adds a client to a group (room, call, etc.)

func (*Hub) LeaveGroup

func (h *Hub) LeaveGroup(groupID string, client *Client)

LeaveGroup removes a client from a group

func (*Hub) Register

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

Register adds a client to the hub

func (*Hub) Run

func (h *Hub) Run()

Run starts the hub's main event loop

func (*Hub) SendToGroup

func (h *Hub) SendToGroup(groupID string, message []byte) int

SendToGroup sends a message to all clients in a group

func (*Hub) SendToGroupExcept

func (h *Hub) SendToGroupExcept(groupID string, excludeUserID string, message []byte) int

SendToGroupExcept sends a message to all clients in a group except specified user

func (*Hub) SendToGroupJSON

func (h *Hub) SendToGroupJSON(groupID string, v interface{}) (int, error)

SendToGroupJSON marshals and sends a JSON message to a group

func (*Hub) SendToTenant

func (h *Hub) SendToTenant(tenantID string, message []byte) int

SendToTenant sends a message to all clients in a tenant

func (*Hub) SendToTenantJSON

func (h *Hub) SendToTenantJSON(tenantID string, v interface{}) (int, error)

SendToTenantJSON marshals and sends a JSON message to a tenant

func (*Hub) SendToUser

func (h *Hub) SendToUser(userID string, message []byte) int

SendToUser sends a message to all connections of a specific user

func (*Hub) SendToUserJSON

func (h *Hub) SendToUserJSON(userID string, v interface{}) (int, error)

SendToUserJSON marshals and sends a JSON message to a user

func (*Hub) Unregister

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

Unregister removes a client from the hub

type HubInterface

type HubInterface interface {
	Register(client *Client)
	Unregister(client *Client)
	Run()
}

HubInterface defines the interface for a WebSocket hub

type MessageHandler

type MessageHandler func(client *Client, message []byte)

MessageHandler is a callback for handling incoming messages

type OriginChecker

type OriginChecker func(r *http.Request) bool

OriginChecker is a function that checks if the origin is allowed

func AllowAllOrigins

func AllowAllOrigins() OriginChecker

AllowAllOrigins returns an origin checker that allows all origins WARNING: Only use this in development

func AllowOrigins

func AllowOrigins(origins ...string) OriginChecker

AllowOrigins returns an origin checker that allows specific origins

Jump to

Keyboard shortcuts

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