This commit is contained in:
fluttershy 2025-01-25 18:40:57 +05:00
parent 02cd1298c4
commit 85779d857c
2 changed files with 11 additions and 8 deletions

View File

@ -46,9 +46,9 @@ 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.setHandshakeTimeout(5); 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](const ix::WebSocketMessagePtr& msg) mutable { webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 1000, connected = false](const ix::WebSocketMessagePtr& msg) mutable {
if (msg->type == ix::WebSocketMessageType::Message) { if (msg->type == 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());
@ -70,6 +70,9 @@ private:
} }
break; break;
} }
} else if (msg->type == ix::WebSocketMessageType::Error) {
std::cout << msg->errorInfo.reason << std::endl;
exit(-1);
} }
}); });
webSocket.start(); webSocket.start();
@ -90,7 +93,7 @@ public:
} }
*/ */
static WebSocket& getInstance(const std::string& token = "", const int intents = 0, bool bot = true) { static WebSocket& getInstance(const std::string& token = "", const int intents = 0, bool bot = true) {
Log::create(WARNING, WEBSOCKET, "Instance event"); Log::create(INFO, WEBSOCKET, "Instance event");
static WebSocket instance(token, intents, bot); static WebSocket instance(token, intents, bot);
return instance; return instance;
} }
@ -111,11 +114,11 @@ public:
} }
void once(const std::string& event, std::function<void(const nlohmann::json&)> handler) { void once(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
eventHandlers[event] = [event, handler, isCalled = false](const nlohmann::json& message) mutable { eventHandlers[event] = [event, handler, isCalled = false](const nlohmann::json& message) mutable {
isCalled == false ? isCalled = true : 0, handler(message.get<nlohmann::json>()); isCalled ? handler(message.get<nlohmann::json>()), true : isCalled = true;
}; };
} }
void start() { void start() {
while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1)); while (1) std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
}; };
#endif #endif

View File

@ -9,7 +9,7 @@ enum level { INFO, WARNING, ERROR, CRITICAL };
enum type { WEBSOCKET, NETWORK, API }; enum type { WEBSOCKET, NETWORK, API };
class Log { class Log {
public: public:
static void create([[maybe_unused]] const level& lvl, [[maybe_unused]] const type& t, [[maybe_unused]] const std::string& message) { [[maybe_unused]] static void create([[maybe_unused]] const level& lvl, [[maybe_unused]] const type& t, [[maybe_unused]] const std::string& message) {
#ifdef DEBUG #ifdef DEBUG
std::string color; std::string color;
switch (lvl) { switch (lvl) {
@ -23,7 +23,7 @@ public:
#endif #endif
} }
private: private:
static std::string getCurrentTime() { static const std::string getCurrentTime() {
std::time_t now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); std::time_t now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm* timer = std::localtime(&now_c); std::tm* timer = std::localtime(&now_c);
std::ostringstream oss; std::ostringstream oss;
@ -33,7 +33,7 @@ private:
<< timer->tm_sec; << timer->tm_sec;
return oss.str(); return oss.str();
} }
static std::string str(const level& lvl) { static std::string str(const level& lvl) noexcept {
switch (lvl) { switch (lvl) {
case INFO: return "INFO"; case INFO: return "INFO";
case WARNING: return "WARNING"; case WARNING: return "WARNING";