Motion Master 6.0.0-alpha.50
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
32 public:
34 struct Config {
35 uint16_t port = 62281;
36 std::string certFile;
37 std::string keyFile;
38 };
39
41 explicit WebSocketServer(Config config);
42
45
53 bool start();
54
56 void stop();
57
60 void broadcast(std::string json);
61
65 void publish(std::string topic, std::string json);
66
67 private:
68 struct WsData {};
69
70 void run();
71
72 Config config_;
73 std::atomic<bool> running_{false};
74 std::thread thread_;
75 std::atomic<uWS::Loop*> loop_{nullptr};
76 std::atomic<uWS::SSLApp*> app_{nullptr};
77 std::unordered_set<uWS::WebSocket<true, true, WsData>*> connections_;
79 std::promise<bool> listenResult_;
80};
WebSocket server — the single bidirectional connection between the PWA and the backend.
Definition ws_server.h:31
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:100
Server configuration.
Definition ws_server.h:34
uint16_t port
TCP port to listen on (TLS).
Definition ws_server.h:35
std::string keyFile
Path to the TLS private key (PEM).
Definition ws_server.h:37
std::string certFile
Path to the TLS certificate (PEM).
Definition ws_server.h:36