Motion Master 6.0.0-alpha.86
Next-generation motion control software
Loading...
Searching...
No Matches
ws_server.h
Go to the documentation of this file.
1#pragma once
2
3#include <uwebsockets/App.h>
4
5#include <atomic>
6#include <cstdint>
7#include <future>
8#include <string>
9#include <thread>
10#include <unordered_set>
11
33 public:
35 struct Config {
37 std::string bindAddress{"127.0.0.1"};
38 uint16_t port = 62281;
39 std::string certFile;
40 std::string keyFile;
41 };
42
44 explicit WebSocketServer(Config config);
45
48
56 bool start();
57
59 void stop();
60
68 void broadcast(std::string json);
69
74 void publish(std::string topic, std::string json);
75
76 private:
77 struct WsData {};
78
79 void run();
80
81 Config config_;
82 std::atomic<bool> running_{false};
83 std::thread thread_;
84 std::atomic<uWS::Loop*> loop_{nullptr};
85 std::atomic<uWS::SSLApp*> app_{nullptr};
86 std::unordered_set<uWS::WebSocket<true, true, WsData>*> connections_;
88 std::promise<bool> listenResult_;
89};
WebSocket server — the single bidirectional connection between the PWA and the backend.
Definition ws_server.h:32
void broadcast(std::string json)
Sends a JSON message to all currently-connected WebSocket clients.
Definition ws_server.cc:53
~WebSocketServer()
Destructor. Calls stop() if the server is still running.
Definition ws_server.cc:13
void publish(std::string topic, std::string json)
Publishes a JSON message to the clients subscribed to topic (native uWS pub/sub).
Definition ws_server.cc:63
void stop()
Stops the server, closes the listen socket, and joins the thread. Idempotent.
Definition ws_server.cc:32
bool start()
Starts the server background thread and begins accepting connections.
Definition ws_server.cc:21
The whole config file. Top-level keys map to these members.
Definition config.h:197
Server configuration.
Definition ws_server.h:35
uint16_t port
TCP port to listen on (TLS).
Definition ws_server.h:38
std::string bindAddress
Local address to bind. Loopback serves only this machine; "0.0.0.0" serves the network.
Definition ws_server.h:37
std::string keyFile
Path to the TLS private key (PEM).
Definition ws_server.h:40
std::string certFile
Path to the TLS certificate (PEM).
Definition ws_server.h:39