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