From 18af78e6af49821f8c7adb5b4325d75c8bf4fd03 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Mon, 17 Aug 2020 02:40:03 -0400 Subject: connect and heartbeat --- discord/websocket.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 discord/websocket.cpp (limited to 'discord/websocket.cpp') diff --git a/discord/websocket.cpp b/discord/websocket.cpp new file mode 100644 index 0000000..3590db3 --- /dev/null +++ b/discord/websocket.cpp @@ -0,0 +1,30 @@ +#include "websocket.hpp" +#include +#include + +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; + } +} -- cgit v1.2.3