diff --git a/libs/gateway/Websocket.hpp b/libs/gateway/Websocket.hpp index e5df68e..8f5c845 100644 --- a/libs/gateway/Websocket.hpp +++ b/libs/gateway/Websocket.hpp @@ -46,9 +46,9 @@ private: }; ix::initNetSystem(); webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json"); - webSocket.setHandshakeTimeout(5); + webSocket.disableAutomaticReconnection(); 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) { res = nlohmann::json::parse(msg->str); Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump()); @@ -70,6 +70,9 @@ private: } break; } + } else if (msg->type == ix::WebSocketMessageType::Error) { + std::cout << msg->errorInfo.reason << std::endl; + exit(-1); } }); webSocket.start(); @@ -90,7 +93,7 @@ public: } */ 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); return instance; } @@ -111,11 +114,11 @@ public: } void once(const std::string& event, std::function handler) { eventHandlers[event] = [event, handler, isCalled = false](const nlohmann::json& message) mutable { - isCalled == false ? isCalled = true : 0, handler(message.get()); + isCalled ? handler(message.get()), true : isCalled = true; }; } 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 \ No newline at end of file diff --git a/libs/utils/log.hpp b/libs/utils/log.hpp index b3e22d3..fade943 100644 --- a/libs/utils/log.hpp +++ b/libs/utils/log.hpp @@ -9,7 +9,7 @@ enum level { INFO, WARNING, ERROR, CRITICAL }; enum type { WEBSOCKET, NETWORK, API }; class Log { 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 std::string color; switch (lvl) { @@ -23,7 +23,7 @@ public: #endif } 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::tm* timer = std::localtime(&now_c); std::ostringstream oss; @@ -33,7 +33,7 @@ private: << timer->tm_sec; return oss.str(); } - static std::string str(const level& lvl) { + static std::string str(const level& lvl) noexcept { switch (lvl) { case INFO: return "INFO"; case WARNING: return "WARNING";