39 lines
1.5 KiB
C++
39 lines
1.5 KiB
C++
#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();
|
|
void start() const;
|
|
void on(const std::string& event, eventHandlers handler);
|
|
void once(const std::string& event, eventHandlers handler);
|
|
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} };
|
|
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);
|
|
public:
|
|
__maybe_unused
|
|
void sendPresenceUpdate(const char* statusType, const std::string& activityName, const int& activityType);
|
|
static WebSocket& getInstance(const std::string& token = "", const int& intents = 0, const bool& bot = true);
|
|
~WebSocket();
|
|
std::string getToken() const;
|
|
int getIntents() const;
|
|
};
|
|
#endif |