summaryrefslogtreecommitdiff
path: root/discord/websocket.cpp
blob: 3590db36c7d7ee9cbcb01cf78b370e1353a829e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
    }
}