sparkle/libs/network/websocket.hpp

38 lines
1.5 KiB
C++
Raw Normal View History

2025-01-29 16:06:47 +05:00
#ifndef NETWORK_WEBSOCKET_HPP_
#define NETWORK_WEBSOCKET_HPP_
#include <includes.hpp>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
class EventEmitter {
private:
using eventHandlers = std::function<void(const nlohmann::json&)>;
std::unordered_map<std::string, std::vector<eventHandlers>> handlers;
protected:
void emit(const std::string& event, const nlohmann::json& data);
public:
void getEvents();
2025-01-30 17:21:08 +05:00
void start() const;
2025-01-29 16:06:47 +05:00
void on(const std::string& event, eventHandlers handler);
2025-01-30 23:05:59 +05:00
void once(const std::string& event, const eventHandlers& handler);
2025-01-29 16:06:47 +05:00
void off(const std::string& event, eventHandlers handler);
};
class WebSocket : public EventEmitter {
private:
std::string token;
int intents;
bool isBot;
ix::WebSocket webSocket;
const nlohmann::json payload = { {"op", GatewayOpcodes::Heartbeat}, {"d", nullptr} };
2025-01-30 23:05:59 +05:00
WebSocket(const std::string& token, int intents, bool isBot);
public:
auto operator=(const WebSocket&)->WebSocket & = delete;
auto operator=(WebSocket&&)->WebSocket & = delete;
2025-01-29 16:06:47 +05:00
WebSocket(WebSocket&&) = delete;
WebSocket(const WebSocket&) = delete;
2025-01-30 23:05:59 +05:00
_maybe_unused void sendPresenceUpdate(const char* statusType, const std::string& activityName, const int& activityType);
static auto getInstance(const std::string& token = "", int intents = 0, bool bot = true) -> WebSocket&;
2025-01-29 16:06:47 +05:00
~WebSocket();
2025-01-30 23:05:59 +05:00
auto getToken() const->std::string;
auto getIntents() const -> int;
2025-01-29 16:06:47 +05:00
};
#endif