Repository navigation
Expand file tree
/
Copy pathWebSocketServer.h
More file actions
62 lines (48 loc) · 1.97 KB
/
Copy pathWebSocketServer.h
File metadata and controls
62 lines (48 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef WEBSOCKETSERVER_H
#define WEBSOCKETSERVER_H
#include <App.h>
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <unordered_map>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/lockfree/queue.hpp>
#include "global.h"
// Per-socket data (can be used to store state for each connection)
struct PerSocketData {
std::string clientIP; // Store the client's IP address or other information
int clientId; // Unique client identifier
};
// Singleton WebSocket Server class
class WebSocketServer {
public:
// Public method to access the singleton instance
static WebSocketServer& getInstance();
// Starts the WebSocket server
void startServer();
// Sends data to a specific client
void sendDataToClient(ClientTXData &data);
void sendDataToClient(const std::array<float, 1025> &data, int clientid = -1);
private:
// Constructor is private to enforce singleton pattern
WebSocketServer() : nextClientId(1) {} // Initialize client ID counter to 1
~WebSocketServer() = default;
// Deleted copy constructor and assignment operator to prevent copying
WebSocketServer(const WebSocketServer&) = delete;
WebSocketServer& operator=(const WebSocketServer&) = delete;
// Internal method to handle client connections
void onClientConnect(uWS::WebSocket<false, true, PerSocketData>* ws);
// Internal method to handle client disconnections
void onClientDisconnect(uWS::WebSocket<false, true, PerSocketData>* ws);
// Internal method to handle received messages from clients
void onClientMessage(uWS::WebSocket<false, true, PerSocketData>* ws, std::string_view message);
// Helper to convert IPv6 to IPv4 if applicable
std::string ipv6ToIpv4(const std::string& ipv6);
// read external data from the queue and send it to a specific or all clients
static void processQueue();
// Client ID generator
int nextClientId;
};
#endif // WEBSOCKETSERVER_H