Motion Master 6.0.0-alpha.86
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) and notifications (slaves changed, watchdog, ...);
  • client → server: topic subscribe/unsubscribe, and process-data output values (target-map writes for the RT loop).

Today the inbound path handles only subscribe/unsubscribe, and the outbound path monitoring batches and notifications; output values plug in here as that feature lands. Procedure progress deliberately does not: ProcedureManager holds no publish callback and names no WebSocket, so that surface is HTTP-poll-only. 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.

Nothing calls this today: every outbound message, notifications included, goes to a topic a client asked for. It stays because reaching every client regardless of what they subscribed to is a different thing from publishing, and the day something has to say it — the server is going down, this build is incompatible — a topic is the wrong mechanism.

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, or mm::node::kNotificationTopic. 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: