Documentation
¶
Overview ¶
Package websocket provides WebSocket server functionality for real-time bidirectional communication between cdev and connected clients (mobile apps, desktop apps, or other services).
Architecture:
┌─────────────────────────────────────────────────────────────┐
│ WebSocket Hub │
│ (Manages all connected clients, broadcasts events) │
└─────────────────────────────┬───────────────────────────────┘
│
┌───────────────────┼───────────────────┐
│ │ │
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Client 1 │ │ Client 2 │ │ Client N │
│ (iOS) │ │ (Desktop)│ │ (...) │
└──────────┘ └──────────┘ └──────────┘
Each Client manages:
- A goroutine for reading incoming messages (readPump)
- A goroutine for writing outgoing messages (writePump)
- Automatic ping/pong for connection health monitoring
- Graceful shutdown handling
Message Flow:
- Incoming: WebSocket → readPump → CommandHandler → Process
- Outgoing: Event Hub → Client.Send() → writePump → WebSocket
Thread Safety:
- Send() is safe to call from any goroutine
- Close() is safe to call multiple times
- Internal state is protected by mutex
Package websocket implements the WebSocket server for real-time events.
Index ¶
- type Client
- type ClientSubscriber
- type CommandHandler
- type Server
- func (s *Server) Broadcast(message []byte)
- func (s *Server) ClientCount() int
- func (s *Server) GetClient(id string) *Client
- func (s *Server) SetOriginChecker(checker *security.OriginChecker)
- func (s *Server) SetStatusProvider(provider StatusProvider)
- func (s *Server) Start() error
- func (s *Server) Stop(ctx context.Context) error
- type StatusProvider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a WebSocket client connection.
A Client wraps a WebSocket connection and provides:
- Concurrent-safe message sending via buffered channel
- Automatic connection health monitoring via ping/pong
- Graceful shutdown with proper close frame handling
- Command handling for incoming messages
Lifecycle:
- Create with NewClient()
- Start read/write pumps with Start()
- Send messages with Send()
- Close with Close() or wait for connection to close
Example:
client := NewClient(conn, handleCommand, onClientClose)
client.Start()
// ... later ...
client.Send([]byte(`{"event":"status","payload":{}}`))
func NewClient ¶
func NewClient(conn *websocket.Conn, commandHandler CommandHandler, onClose func(id string)) *Client
NewClient creates a new WebSocket client.
type ClientSubscriber ¶
type ClientSubscriber struct {
// contains filtered or unexported fields
}
ClientSubscriber wraps a WebSocket client as an EventHub subscriber.
func NewClientSubscriber ¶
func NewClientSubscriber(client *Client) *ClientSubscriber
NewClientSubscriber creates a subscriber from a WebSocket client.
func (*ClientSubscriber) Close ¶
func (s *ClientSubscriber) Close() error
Close closes the subscriber.
func (*ClientSubscriber) Done ¶
func (s *ClientSubscriber) Done() <-chan struct{}
Done returns a channel that's closed when the subscriber is done.
func (*ClientSubscriber) ID ¶
func (s *ClientSubscriber) ID() string
ID returns the subscriber's unique identifier.
type CommandHandler ¶
CommandHandler is a function that handles incoming commands.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the WebSocket server.
func (*Server) ClientCount ¶
ClientCount returns the number of connected clients.
func (*Server) SetOriginChecker ¶
func (s *Server) SetOriginChecker(checker *security.OriginChecker)
SetOriginChecker sets the origin checker for websocket handshake validation.
func (*Server) SetStatusProvider ¶
func (s *Server) SetStatusProvider(provider StatusProvider)
SetStatusProvider sets the status provider for heartbeat events.
type StatusProvider ¶
StatusProvider provides status information for heartbeat events.