1
0
forked from rcxpony/sparkle

121 lines
4.8 KiB
C++
Raw Normal View History

2024-12-31 02:20:56 +05:00
#ifndef GATEWAY_WEBSOCKET_HPP_
#define GATEWAY_WEBSOCKET_HPP_
2025-01-25 13:22:59 +05:00
#include <includes.hpp>
2024-12-31 02:20:56 +05:00
#include <thread>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
class WebSocket {
private:
2025-01-13 22:30:24 +05:00
bool isBot;
2025-01-13 14:40:21 +05:00
int intents;
std::string token;
2024-12-31 02:20:56 +05:00
ix::WebSocket webSocket;
2025-01-25 11:24:56 +05:00
nlohmann::json payload = { {"op", 1},{"d", nullptr} }, id;
std::unordered_map<std::string, std::function<void(const nlohmann::json&)>> eventHandlers;
2025-01-13 14:40:21 +05:00
WebSocket& operator=(const WebSocket&) = delete;
2025-01-25 14:21:16 +05:00
WebSocket& operator=(WebSocket&&) = delete;
WebSocket(WebSocket&&) = delete;
2025-01-13 14:40:21 +05:00
WebSocket(const WebSocket&) = delete;
2025-01-25 14:21:16 +05:00
explicit WebSocket(const std::string& token, const int& intents, const bool& isBot) {
2025-01-13 14:40:21 +05:00
WebSocket::token = token;
WebSocket::intents = intents;
2024-12-31 04:46:39 +05:00
WebSocket::isBot = isBot;
2024-12-31 02:20:56 +05:00
id = {
{"op", 2},
{"d", {
{"token", token},
{"intents", intents},
{"properties", {
{"os", "linux"},
{"browser", "firefox"},
{"device", "firefox"}
}},
2025-01-25 13:22:59 +05:00
//{"compress", 1},
{"presence", {
{"activities", nlohmann::json::array({
{
//{"name", "asdsadsadsadsa"},
//{"type", 2}
}
})},
{"status", "idle"},
{"since", 91879201},
{"afk", false}
2024-12-31 02:20:56 +05:00
}}
2025-01-25 13:22:59 +05:00
}}
2024-12-31 02:20:56 +05:00
};
2025-01-06 22:18:30 +05:00
ix::initNetSystem();
2024-12-31 02:20:56 +05:00
webSocket.setUrl("wss://gateway.discord.gg/?v=10&encoding=json");
2025-01-14 05:18:08 +05:00
webSocket.setHandshakeTimeout(5);
2025-01-19 04:35:20 +05:00
Log::create(INFO, WEBSOCKET, "ixwebsocket init");
2025-01-25 11:24:56 +05:00
webSocket.setOnMessageCallback([this, res = nlohmann::json(), heartbeat_interval = 0, connected = false](const ix::WebSocketMessagePtr& msg) mutable {
2025-01-06 22:18:30 +05:00
if (msg->type == ix::WebSocketMessageType::Message) {
2025-01-25 11:24:56 +05:00
res = nlohmann::json::parse(msg->str);
2025-01-19 04:35:20 +05:00
Log::create(INFO, WEBSOCKET, res["op"].dump() + " " + res["t"].dump());
2024-12-31 02:20:56 +05:00
switch (res["op"].get<int>()) {
2025-01-25 13:22:59 +05:00
case 10:
heartbeat_interval = res["d"]["heartbeat_interval"].get<int>();
!connected ? connected = true, webSocket.send(id.dump()) : 0;
std::thread([this, &heartbeat_interval, &connected]() {
while (connected && heartbeat_interval != -1) {
Log::create(INFO, WEBSOCKET, "Heartbeat " + std::to_string(heartbeat_interval));
2025-01-25 14:21:16 +05:00
std::this_thread::sleep_for(std::chrono::milliseconds(heartbeat_interval));
2025-01-25 13:22:59 +05:00
webSocket.send(payload.dump());
}
}).detach();
break;
case 0:
if (eventHandlers.find(res["t"].get<std::string>()) != eventHandlers.end()) {
eventHandlers[res["t"].get<std::string>()](res);
2025-01-13 14:40:21 +05:00
}
2025-01-25 13:22:59 +05:00
break;
2024-12-31 02:20:56 +05:00
}
}
});
webSocket.start();
}
public:
2025-01-13 14:40:21 +05:00
/*
2024-12-31 05:58:15 +05:00
void sendPresenceUpdate(int statusType, const std::string& activityName) {
json prsUpdate = {
{"op", 3},
{"d", {
2024-12-31 06:32:29 +05:00
{"since", 0},
{"activities", json::array({{"name", activityName}, {"type", 2}})},
2024-12-31 05:58:15 +05:00
{"status", statusType == 1 ? "online" : "idle"},
{"afk", false}
}}
};
webSocket.send(prsUpdate.dump());
}
2025-01-13 14:40:21 +05:00
*/
static WebSocket& getInstance(const std::string& token = "", const int intents = 0, bool bot = true) {
2025-01-19 04:35:20 +05:00
Log::create(WARNING, WEBSOCKET, "Instance event");
2025-01-13 14:40:21 +05:00
static WebSocket instance(token, intents, bot);
2024-12-31 02:20:56 +05:00
return instance;
}
~WebSocket() {
webSocket.close();
ix::uninitNetSystem();
}
std::string getToken() const {
2025-01-13 14:40:21 +05:00
return isBot ? std::string("Bot " + WebSocket::token) : WebSocket::token;
2024-12-31 02:20:56 +05:00
}
int getIntents() const {
2025-01-13 14:40:21 +05:00
return WebSocket::intents;
2024-12-31 02:20:56 +05:00
}
2025-01-25 14:21:16 +05:00
void on(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
2025-01-25 11:24:56 +05:00
eventHandlers[event] = [handler](const nlohmann::json& message) {
2025-01-25 14:21:16 +05:00
handler(message.get<nlohmann::json>());
2024-12-31 02:20:56 +05:00
};
}
2025-01-25 12:23:31 +05:00
void once(const std::string& event, std::function<void(const nlohmann::json&)> handler) {
2025-01-25 11:24:56 +05:00
eventHandlers[event] = [event, handler, isCalled = false](const nlohmann::json& message) mutable {
isCalled == false ? isCalled = true : 0, handler(message.get<nlohmann::json>());
2024-12-31 02:20:56 +05:00
};
}
2025-01-14 05:18:08 +05:00
void start() {
2025-01-25 14:21:16 +05:00
while (1) std::this_thread::sleep_for(std::chrono::milliseconds(1));
2024-12-31 02:20:56 +05:00
}
};
#endif