Documentation
¶
Overview ¶
Package websocket provides WebSocket infrastructure for real-time communication.
Index ¶
- func MakeChannel(channelType ChannelType, id string) string
- type AuthorizeFunc
- type BroadcastMessage
- type ChannelType
- type Client
- func (c *Client) Close()
- func (c *Client) GetSubscriptions() []string
- func (c *Client) IsSubscribed(channel string) bool
- func (c *Client) ReadPump()
- func (c *Client) SendMessage(msg *Message) error
- func (c *Client) Subscribe(channel string) bool
- func (c *Client) Unsubscribe(channel string) bool
- func (c *Client) WritePump()
- type ErrorData
- type Handler
- type Hub
- func (h *Hub) Broadcast(channel string, msg *Message, tenantID string)
- func (h *Hub) BroadcastEvent(channel string, data any, tenantID string)
- func (h *Hub) BroadcastToTenant(tenantID string, msg *Message)
- func (h *Hub) GetChannelsByPrefix(prefix string) []string
- func (h *Hub) GetClientsByTenant(tenantID string) []*Client
- func (h *Hub) GetStats() HubStats
- func (h *Hub) RegisterClient(client *Client)
- func (h *Hub) Run(ctx context.Context)
- func (h *Hub) SetAuthorizeFunc(fn AuthorizeFunc)
- func (h *Hub) UnregisterClient(client *Client)
- type HubStats
- type Message
- type MessageType
- type SubscribeRequest
- type UnsubscribeRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MakeChannel ¶
func MakeChannel(channelType ChannelType, id string) string
MakeChannel creates a channel string from type and ID.
Types ¶
type AuthorizeFunc ¶
AuthorizeFunc is a function that checks if a client can subscribe to a channel. Returns true if authorized, false otherwise.
type BroadcastMessage ¶
type BroadcastMessage struct {
Channel string
Message *Message
TenantID string // If set, only clients in this tenant receive the message
}
BroadcastMessage represents a message to broadcast to a channel.
type ChannelType ¶
type ChannelType string
ChannelType represents the type of channel.
const ( // Channel types ChannelTypeFinding ChannelType = "finding" // finding:{id} - activity updates for a finding ChannelTypeScan ChannelType = "scan" // scan:{id} - scan progress updates ChannelTypeTenant ChannelType = "tenant" // tenant:{id} - tenant-wide notifications ChannelTypeNotification ChannelType = "notification" // notification:{tenant_id} - notification delivery ChannelTypeTriage ChannelType = "triage" // triage:{finding_id} - AI triage progress updates ChannelTypeGroup ChannelType = "group" // group:{id} - group membership/scope rule changes )
func ParseChannel ¶
func ParseChannel(channel string) (ChannelType, string)
ParseChannel extracts the channel type and ID from a channel string. Channel format: "{type}:{id}" e.g., "finding:abc-123"
type Client ¶
type Client struct {
// Identity
ID string
UserID string
TenantID string
// contains filtered or unexported fields
}
Client represents a single WebSocket connection.
func (*Client) GetSubscriptions ¶
GetSubscriptions returns all subscribed channels.
func (*Client) IsSubscribed ¶
IsSubscribed checks if client is subscribed to a channel.
func (*Client) ReadPump ¶
func (c *Client) ReadPump()
ReadPump pumps messages from the WebSocket connection to the hub.
func (*Client) SendMessage ¶
SendMessage sends a message to the client.
func (*Client) Subscribe ¶
Subscribe adds a channel subscription. Returns false if already subscribed or rate limit exceeded.
func (*Client) Unsubscribe ¶
Unsubscribe removes a channel subscription.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles WebSocket connections.
func NewHandler ¶
NewHandler creates a new WebSocket handler.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub maintains the set of active clients and broadcasts messages to them.
func (*Hub) BroadcastEvent ¶
BroadcastEvent is a convenience method to broadcast an event to a channel.
func (*Hub) BroadcastToTenant ¶
BroadcastToTenant sends a message to all clients in a tenant.
func (*Hub) GetChannelsByPrefix ¶
GetChannelsByPrefix returns all channels matching a prefix.
func (*Hub) GetClientsByTenant ¶
GetClientsByTenant returns all clients for a tenant.
func (*Hub) RegisterClient ¶
RegisterClient registers a new client.
func (*Hub) SetAuthorizeFunc ¶
func (h *Hub) SetAuthorizeFunc(fn AuthorizeFunc)
SetAuthorizeFunc sets a custom authorization function.
func (*Hub) UnregisterClient ¶
UnregisterClient unregisters a client.
type HubStats ¶
type HubStats struct {
TotalClients int `json:"total_clients"`
TotalChannels int `json:"total_channels"`
ChannelClients map[string]int `json:"channel_clients"`
}
HubStats contains hub statistics.
type Message ¶
type Message struct {
Type MessageType `json:"type"`
Channel string `json:"channel,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
Timestamp int64 `json:"timestamp"`
RequestID string `json:"request_id,omitempty"`
}
Message is the base WebSocket message structure.
func NewMessage ¶
func NewMessage(msgType MessageType) *Message
NewMessage creates a new message with current timestamp.
func (*Message) WithChannel ¶
WithChannel sets the channel for the message.
func (*Message) WithRequestID ¶
WithRequestID sets the request ID for the message.
type MessageType ¶
type MessageType string
MessageType defines the type of WebSocket message.
const ( // Client -> Server messages MessageTypeSubscribe MessageType = "subscribe" MessageTypeUnsubscribe MessageType = "unsubscribe" MessageTypePing MessageType = "ping" // Server -> Client messages MessageTypePong MessageType = "pong" MessageTypeSubscribed MessageType = "subscribed" MessageTypeUnsubscribed MessageType = "unsubscribed" MessageTypeEvent MessageType = "event" MessageTypeError MessageType = "error" )
type SubscribeRequest ¶
type SubscribeRequest struct {
Channel string `json:"channel"`
RequestID string `json:"request_id,omitempty"`
}
SubscribeRequest represents a subscribe message from client.
type UnsubscribeRequest ¶
type UnsubscribeRequest struct {
Channel string `json:"channel"`
RequestID string `json:"request_id,omitempty"`
}
UnsubscribeRequest represents an unsubscribe message from client.