obs-websocket/src/WebSocketServer.h

58 lines
1.3 KiB
C
Raw Normal View History

#pragma once
#include <QObject>
2021-04-27 21:52:48 +00:00
#include <QThreadPool>
#include <QMutex>
#include <nlohmann/json.hpp>
2021-04-27 21:52:48 +00:00
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
2021-04-27 21:52:48 +00:00
#include "WebSocketSession.h"
#include "requesthandler/RequestHandler.h"
2021-04-27 21:52:48 +00:00
using json = nlohmann::json;
class WebSocketServer : public QObject
{
Q_OBJECT
public:
2021-04-27 21:52:48 +00:00
WebSocketServer();
~WebSocketServer();
void Start();
void Stop();
void InvalidateSession(websocketpp::connection_hdl hdl);
struct WebSocketState {
websocketpp::connection_hdl hdl;
std::string remoteAddress;
uint64_t durationSeconds;
uint64_t incomingMessages;
uint64_t outgoingMessages;
};
std::vector<WebSocketState> GetWebSocketSessions();
QThreadPool *GetThreadPool() {
return &_threadPool;
}
public Q_SLOTS:
void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr);
private:
2021-04-27 21:52:48 +00:00
void onOpen(websocketpp::connection_hdl hdl);
void onClose(websocketpp::connection_hdl hdl);
void onMessage(websocketpp::connection_hdl hdl);
WebSocketSession *GetWebSocketSession(websocketpp::connection_hdl hdl);
websocketpp::server<websocketpp::config::asio> _server;
QThreadPool _threadPool;
QMutex _sessionMutex;
std::map<websocketpp::connection_hdl, WebSocketSession, std::owner_less<websocketpp::connection_hdl>> _sessions;
};