wangrong
2026-08-24 beef6536e7c8dd3f5cbe8f73585611479910bea7
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
#ifndef WEBSOCKET_CONTROL_SERVER_H
#define WEBSOCKET_CONTROL_SERVER_H
 
#include <esp_http_server.h>
#include <cJSON.h>
#include <string>
#include <map>
 
class WebSocketControlServer {
public:
    WebSocketControlServer();
    ~WebSocketControlServer();
 
    bool Start(int port = 8080);
    
    void Stop();
 
    size_t GetClientCount() const;
 
private:
    httpd_handle_t server_handle_;
    std::map<int, httpd_req_t*> clients_;
 
    static esp_err_t ws_handler(httpd_req_t *req);
    
    void HandleMessage(httpd_req_t *req, const char* data, size_t len);
    void AddClient(httpd_req_t *req);
    void RemoveClient(httpd_req_t *req);
    static WebSocketControlServer* instance_;
};
 
#endif // WEBSOCKET_CONTROL_SERVER_H