websocket

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 15 Imported by: 0

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

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:

  1. Create with NewClient()
  2. Start read/write pumps with Start()
  3. Send messages with Send()
  4. 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.

func (*Client) Close

func (c *Client) Close()

Close closes the client connection.

func (*Client) ID

func (c *Client) ID() string

ID returns the client's unique identifier.

func (*Client) Send

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

Send queues a message to be sent to the client.

func (*Client) Start

func (c *Client) Start()

Start starts the client's read and write pumps.

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.

func (*ClientSubscriber) Send

func (s *ClientSubscriber) Send(event events.Event) error

Send sends an event to the subscriber.

type CommandHandler

type CommandHandler func(clientID string, message []byte)

CommandHandler is a function that handles incoming commands.

type Server

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

Server is the WebSocket server.

func NewServer

func NewServer(host string, port int, commandHandler CommandHandler, hub ports.EventHub) *Server

NewServer creates a new WebSocket server.

func (*Server) Broadcast

func (s *Server) Broadcast(message []byte)

Broadcast sends a message to all connected clients.

func (*Server) ClientCount

func (s *Server) ClientCount() int

ClientCount returns the number of connected clients.

func (*Server) GetClient

func (s *Server) GetClient(id string) *Client

GetClient returns a client by ID.

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.

func (*Server) Start

func (s *Server) Start() error

Start starts the WebSocket server.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop gracefully stops the WebSocket server.

type StatusProvider

type StatusProvider interface {
	GetClaudeStatus() string
	GetUptimeSeconds() int64
}

StatusProvider provides status information for heartbeat events.

Jump to

Keyboard shortcuts

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