Documentation
¶
Index ¶
- func NewUpgrader(config Config, checkOrigin OriginChecker) *websocket.Upgrader
- func Upgrade(w http.ResponseWriter, r *http.Request, config Config, ...) (*websocket.Conn, error)
- type Client
- func (c *Client) Close()
- func (c *Client) GetMetadata(key string) (interface{}, bool)
- func (c *Client) ReadPump()
- func (c *Client) Send(message []byte) bool
- func (c *Client) SendJSON(v interface{}) error
- func (c *Client) SetMetadata(key string, value interface{})
- func (c *Client) Start()
- func (c *Client) WritePump()
- type ClientOption
- type Config
- type DisconnectHandler
- type Hub
- func (h *Hub) BroadcastAll(message []byte) int
- func (h *Hub) DisconnectUser(userID string)
- func (h *Hub) GetClient(userID, clientID string) (*Client, bool)
- func (h *Hub) GetConnectedUserIDs(tenantID string) []string
- func (h *Hub) GetGroupClients(groupID string) []*Client
- func (h *Hub) GetGroupUserIDs(groupID string) []string
- func (h *Hub) GetUserClients(userID string) []*Client
- func (h *Hub) HasActiveConnection(userID string) bool
- func (h *Hub) JoinGroup(groupID string, client *Client)
- func (h *Hub) LeaveGroup(groupID string, client *Client)
- func (h *Hub) Register(client *Client)
- func (h *Hub) Run()
- func (h *Hub) SendToGroup(groupID string, message []byte) int
- func (h *Hub) SendToGroupExcept(groupID string, excludeUserID string, message []byte) int
- func (h *Hub) SendToGroupJSON(groupID string, v interface{}) (int, error)
- func (h *Hub) SendToTenant(tenantID string, message []byte) int
- func (h *Hub) SendToTenantJSON(tenantID string, v interface{}) (int, error)
- func (h *Hub) SendToUser(userID string, message []byte) int
- func (h *Hub) SendToUserJSON(userID string, v interface{}) (int, error)
- func (h *Hub) Unregister(client *Client)
- type HubInterface
- type MessageHandler
- type OriginChecker
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
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) GetMetadata ¶
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) SetMetadata ¶
SetMetadata sets a metadata value
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 (*Hub) BroadcastAll ¶
BroadcastAll sends a message to all connected clients
func (*Hub) DisconnectUser ¶
DisconnectUser closes all connections for a user
func (*Hub) GetConnectedUserIDs ¶
GetConnectedUserIDs returns all connected user IDs (optionally filtered by tenant)
func (*Hub) GetGroupClients ¶
GetGroupClients returns all clients in a group
func (*Hub) GetGroupUserIDs ¶
GetGroupUserIDs returns all user IDs in a group
func (*Hub) GetUserClients ¶
GetUserClients returns all clients for a user
func (*Hub) HasActiveConnection ¶
HasActiveConnection checks if a user has any active connections
func (*Hub) LeaveGroup ¶
LeaveGroup removes a client from a group
func (*Hub) SendToGroup ¶
SendToGroup sends a message to all clients in a group
func (*Hub) SendToGroupExcept ¶
SendToGroupExcept sends a message to all clients in a group except specified user
func (*Hub) SendToGroupJSON ¶
SendToGroupJSON marshals and sends a JSON message to a group
func (*Hub) SendToTenant ¶
SendToTenant sends a message to all clients in a tenant
func (*Hub) SendToTenantJSON ¶
SendToTenantJSON marshals and sends a JSON message to a tenant
func (*Hub) SendToUser ¶
SendToUser sends a message to all connections of a specific user
func (*Hub) SendToUserJSON ¶
SendToUserJSON marshals and sends a JSON message to a user
func (*Hub) Unregister ¶
Unregister removes a client from the hub
type HubInterface ¶
HubInterface defines the interface for a WebSocket hub
type MessageHandler ¶
MessageHandler is a callback for handling incoming messages
type OriginChecker ¶
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