ifelse -> switch
This commit is contained in:
parent
69e16e9c96
commit
99cf38cf12
@ -7,5 +7,4 @@
|
|||||||
#include <utils/functions.hpp>
|
#include <utils/functions.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
|
||||||
#endif
|
#endif
|
@ -7,6 +7,14 @@ class EventEmitter {
|
|||||||
private:
|
private:
|
||||||
using eventHandlers = std::function<void(const nlohmann::json&)>;
|
using eventHandlers = std::function<void(const nlohmann::json&)>;
|
||||||
std::unordered_map<std::string, std::list<eventHandlers>> handlers;
|
std::unordered_map<std::string, std::list<eventHandlers>> handlers;
|
||||||
|
protected:
|
||||||
|
void emit(const std::string& event, const nlohmann::json& data) {
|
||||||
|
if (handlers.find(event) != handlers.end()) {
|
||||||
|
for (const auto& handler : handlers[event]) {
|
||||||
|
handler(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
void on(const std::string& event, eventHandlers handler) {
|
void on(const std::string& event, eventHandlers handler) {
|
||||||
handlers[event].emplace_back(handler);
|
handlers[event].emplace_back(handler);
|
||||||
@ -18,13 +26,6 @@ public:
|
|||||||
};
|
};
|
||||||
handlers[event].emplace_back(wrappedHandler);
|
handlers[event].emplace_back(wrappedHandler);
|
||||||
}
|
}
|
||||||
void emit(const std::string& event, const nlohmann::json& data) {
|
|
||||||
if (handlers.find(event) != handlers.end()) {
|
|
||||||
for (const auto& handler : handlers[event]) {
|
|
||||||
handler(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void off(const std::string& event, eventHandlers handler) {
|
void off(const std::string& event, eventHandlers handler) {
|
||||||
if (handlers.find(event) != handlers.end()) {
|
if (handlers.find(event) != handlers.end()) {
|
||||||
auto& vec = handlers[event];
|
auto& vec = handlers[event];
|
||||||
@ -60,7 +61,7 @@ private:
|
|||||||
{"presence", {
|
{"presence", {
|
||||||
{"activities", nlohmann::json::array({
|
{"activities", nlohmann::json::array({
|
||||||
{
|
{
|
||||||
{"name", "asdsadsadsadsa"},
|
{"name", "meow"},
|
||||||
{"type", 2}
|
{"type", 2}
|
||||||
}
|
}
|
||||||
})},
|
})},
|
||||||
@ -72,10 +73,11 @@ private:
|
|||||||
};
|
};
|
||||||
ix::initNetSystem();
|
ix::initNetSystem();
|
||||||
webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json");
|
webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json");
|
||||||
webSocket.disableAutomaticReconnection();
|
//webSocket.disableAutomaticReconnection();
|
||||||
Log::create(INFO, WEBSOCKET, "ixwebsocket init");
|
Log::create(INFO, WEBSOCKET, "ixwebsocket init");
|
||||||
webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false, id](const ix::WebSocketMessagePtr& msg) mutable {
|
webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false, id](const ix::WebSocketMessagePtr& msg) mutable {
|
||||||
if (msg->type == ix::WebSocketMessageType::Message) {
|
switch (msg->type) {
|
||||||
|
case ix::WebSocketMessageType::Message:
|
||||||
res = nlohmann::json::parse(msg->str);
|
res = nlohmann::json::parse(msg->str);
|
||||||
Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump());
|
Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump());
|
||||||
switch (res["op"].get<int>()) {
|
switch (res["op"].get<int>()) {
|
||||||
@ -92,28 +94,31 @@ private:
|
|||||||
break;
|
break;
|
||||||
case 0: emit(res["t"].get<std::string>(), res); break;
|
case 0: emit(res["t"].get<std::string>(), res); break;
|
||||||
}
|
}
|
||||||
} else if (msg->type == ix::WebSocketMessageType::Error) {
|
break;
|
||||||
|
case ix::WebSocketMessageType::Error:
|
||||||
std::cout << msg->errorInfo.reason << std::endl;
|
std::cout << msg->errorInfo.reason << std::endl;
|
||||||
exit(-1);
|
exit(-1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
webSocket.start();
|
webSocket.start();
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
/*
|
[[maybe_unused]]
|
||||||
void sendPresenceUpdate(int statusType, const std::string& activityName) {
|
void sendPresenceUpdate(const int& statusType, const std::string& activityName) {
|
||||||
json prsUpdate = {
|
nlohmann::json prsUpdate = {
|
||||||
{"op", 3},
|
{"op", 3},
|
||||||
{"d", {
|
{"d", {
|
||||||
{"since", 0},
|
{"since", 0},
|
||||||
{"activities", json::array({{"name", activityName}, {"type", 2}})},
|
{"activities", nlohmann::json::array({{"name", activityName}, {"type", 2}})},
|
||||||
{"status", statusType == 1 ? "online" : "idle"},
|
{"status", statusType == 1 ? "online" : "idle"},
|
||||||
{"afk", false}
|
{"afk", false}
|
||||||
}}
|
}}
|
||||||
};
|
};
|
||||||
webSocket.send(prsUpdate.dump());
|
webSocket.send(prsUpdate.dump());
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
static WebSocket& getInstance(const std::string& token = "", const int& intents = 0, const bool& bot = true) {
|
static WebSocket& getInstance(const std::string& token = "", const int& intents = 0, const bool& bot = true) {
|
||||||
Log::create(INFO, WEBSOCKET, "Instance event");
|
Log::create(INFO, WEBSOCKET, "Instance event");
|
||||||
static WebSocket instance(token, intents, bot);
|
static WebSocket instance(token, intents, bot);
|
||||||
|
@ -10,12 +10,13 @@ private:
|
|||||||
WebSocket& web;
|
WebSocket& web;
|
||||||
std::chrono::duration<double, std::milli> duration;
|
std::chrono::duration<double, std::milli> duration;
|
||||||
NetworkManager& operator=(const NetworkManager&) = delete;
|
NetworkManager& operator=(const NetworkManager&) = delete;
|
||||||
|
NetworkManager& operator=(NetworkManager&&) = delete;
|
||||||
|
NetworkManager(NetworkManager&&) = delete;
|
||||||
NetworkManager(const NetworkManager&) = delete;
|
NetworkManager(const NetworkManager&) = delete;
|
||||||
NetworkManager() : web(WebSocket::getInstance()) {
|
NetworkManager() : web(WebSocket::getInstance()) {
|
||||||
Log::create(INFO, NETWORK, "Network init");
|
Log::create(INFO, NETWORK, "Network init");
|
||||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
curl = curl_easy_init();
|
if (!(curl = curl_easy_init())) {
|
||||||
if (!curl) {
|
|
||||||
Log::create(CRITICAL, NETWORK, "Failed to initialize CURL");
|
Log::create(CRITICAL, NETWORK, "Failed to initialize CURL");
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user