connect and heartbeat

This commit is contained in:
ouwou
2020-08-17 02:40:03 -04:00
parent 212511e29d
commit 18af78e6af
14 changed files with 653 additions and 0 deletions

30
discord/websocket.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "websocket.hpp"
#include <functional>
#include <nlohmann/json.hpp>
Websocket::Websocket() {}
void Websocket::StartConnection(std::string url) {
m_websocket.setUrl(url);
m_websocket.setOnMessageCallback(std::bind(&Websocket::OnMessage, this, std::placeholders::_1));
m_websocket.start();
}
void Websocket::SetJSONCallback(JSONCallback_t func) {
m_json_callback = func;
}
void Websocket::Send(const std::string &str) {
m_websocket.sendText(str);
}
void Websocket::OnMessage(const ix::WebSocketMessagePtr &msg) {
switch (msg->type) {
case ix::WebSocketMessageType::Message:
printf("%s\n", msg->str.c_str());
auto obj = nlohmann::json::parse(msg->str);
if (m_json_callback)
m_json_callback(obj);
break;
}
}