This commit is contained in:
fluttershy 2025-01-25 14:21:16 +05:00
parent dce9bfef07
commit b978dd998f
2 changed files with 8 additions and 9 deletions

View File

@ -4,6 +4,7 @@ project(sparkle)
include(GoogleTest)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCE sources/main.cpp)

View File

@ -4,8 +4,6 @@
#include <thread>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
using namespace std::chrono;
using namespace std::chrono_literals;
class WebSocket {
private:
bool isBot;
@ -15,8 +13,10 @@ private:
nlohmann::json payload = { {"op", 1},{"d", nullptr} }, id;
std::unordered_map<std::string, std::function<void(const nlohmann::json&)>> eventHandlers;
WebSocket& operator=(const WebSocket&) = delete;
WebSocket& operator=(WebSocket&&) = delete;
WebSocket(WebSocket&&) = delete;
WebSocket(const WebSocket&) = delete;
WebSocket(const std::string& token, const int& intents, const bool& isBot) {
explicit WebSocket(const std::string& token, const int& intents, const bool& isBot) {
WebSocket::token = token;
WebSocket::intents = intents;
WebSocket::isBot = isBot;
@ -59,7 +59,7 @@ private:
std::thread([this, &heartbeat_interval, &connected]() {
while (connected && heartbeat_interval != -1) {
Log::create(INFO, WEBSOCKET, "Heartbeat " + std::to_string(heartbeat_interval));
std::this_thread::sleep_for(milliseconds(heartbeat_interval));
std::this_thread::sleep_for(std::chrono::milliseconds(heartbeat_interval));
webSocket.send(payload.dump());
}
}).detach();
@ -104,11 +104,9 @@ public:
int getIntents() const {
return WebSocket::intents;
}
void on(const std::string& event, std::function<void(const nlohmann::json&)> handler = nullptr) {
void on(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
eventHandlers[event] = [handler](const nlohmann::json& message) {
if (handler) {
handler(message.get<nlohmann::json>());
}
};
}
void once(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
@ -117,7 +115,7 @@ public:
};
}
void start() {
while (1) std::this_thread::sleep_for(1ms);
while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
};
#endif