Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
WebSocketServer Class Reference

WebSocket server — the single bidirectional connection between the PWA and the backend. More...

#include <ws_server.h>

Classes

struct  Config
 Server configuration. More...
 

Public Member Functions

 WebSocketServer (Config config)
 Constructs the server with the given configuration (copied internally).
 
 ~WebSocketServer ()
 Destructor. Calls stop() if the server is still running.
 
bool start ()
 Starts the server background thread and begins accepting connections.
 
void stop ()
 Stops the server, closes the listen socket, and joins the thread. Idempotent.
 
void broadcast (std::string json)
 Sends a JSON message to all currently-connected WebSocket clients.
 
void publish (std::string topic, std::string json)
 Publishes a JSON message to the clients subscribed to topic (native uWS pub/sub).
 

Detailed Description

WebSocket server — the single bidirectional connection between the PWA and the backend.

Runs on its own TLS port, event loop, and thread, fully isolated from the HTTP API server (HttpServer). Because the two run on separate loops, a slow or blocking HTTP handler can never stall this connection: the loop only ever does non-blocking I/O. The whole port is the WebSocket, so it accepts the upgrade on any path — clients connect to the bare wss://host:port.

Carries traffic in both directions:

  • server → client: monitoring batches (published per topic), notifications (slaves changed, watchdog, ...), and long-running procedure progress (firmware, calibration, ...);
  • client → server: topic subscribe/unsubscribe, and process-data output values (target-map writes staged for the RT loop).

Today the inbound path handles only subscribe/unsubscribe and the outbound path only monitoring publishes; notifications, procedure progress, and output staging plug in here as those features land. All public methods are thread-safe — publish() and broadcast() may be called from any thread; the send is marshalled onto this server's event loop via defer() so the caller never blocks.

Constructor & Destructor Documentation

◆ WebSocketServer()

WebSocketServer::WebSocketServer ( Config  config)
explicit

Constructs the server with the given configuration (copied internally).

◆ ~WebSocketServer()

WebSocketServer::~WebSocketServer ( )

Destructor. Calls stop() if the server is still running.

Member Function Documentation

◆ broadcast()

void WebSocketServer::broadcast ( std::string  json)

Sends a JSON message to all currently-connected WebSocket clients.

Parameters
jsonSerialised JSON string. Moved into the deferred closure.

◆ publish()

void WebSocketServer::publish ( std::string  topic,
std::string  json 
)

Publishes a JSON message to the clients subscribed to topic (native uWS pub/sub).

Parameters
topicTopic to publish under (a monitoring id). Moved into the deferred closure.
jsonSerialised JSON string. Moved into the deferred closure.

◆ start()

bool WebSocketServer::start ( )

Starts the server background thread and begins accepting connections.

Blocks until the listen attempt completes and reports whether the port was bound. The port is claimed exclusively (LIBUS_LISTEN_EXCLUSIVE_PORT), so a port already in use fails here instead of silently sharing it via SO_REUSEPORT. Idempotent: a second call while already running returns true.

Returns
true if the server is listening; false if the port could not be bound.

◆ stop()

void WebSocketServer::stop ( )

Stops the server, closes the listen socket, and joins the thread. Idempotent.


The documentation for this class was generated from the following files: