summaryrefslogtreecommitdiff
path: root/src/discord/websocket.hpp
blob: 26cd5d462857ac97fb44c7478854aeba7fc74900 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
#include <string>
#include <functional>
#include <nlohmann/json.hpp>
#include <sigc++/sigc++.h>

class Websocket {
public:
    Websocket();
    void StartConnection(std::string url);

    void SetUserAgent(std::string agent);

    bool GetPrintMessages() const noexcept;
    void SetPrintMessages(bool show) noexcept;

    void Send(const std::string &str);
    void Send(const nlohmann::json &j);
    void Stop();
    void Stop(uint16_t code);
    bool IsOpen() const;

private:
    void OnMessage(const ix::WebSocketMessagePtr &msg);

    ix::WebSocket m_websocket;
    std::string m_agent;

public:
    using type_signal_open = sigc::signal<void>;
    using type_signal_close = sigc::signal<void, uint16_t>;
    using type_signal_message = sigc::signal<void, std::string>;

    type_signal_open signal_open();
    type_signal_close signal_close();
    type_signal_message signal_message();

private:
    type_signal_open m_signal_open;
    type_signal_close m_signal_close;
    type_signal_message m_signal_message;

    bool m_print_messages = true;
};