-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWsClient.h
69 lines (51 loc) · 1.71 KB
/
WsClient.h
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "libwebsockets.h"
#include <string>
#include <map>
#include <thread>
#include <mutex>
#include <queue>
#include <atomic>
class WsCallback {
public:
virtual ~WsCallback() = default;
virtual void onConnected() = 0;
virtual void onDisconnected() = 0;
virtual void onConnectionError() = 0;
virtual void onReceiveMessage(std::string& message) = 0;
};
class WsClient {
public:
WsClient(std::string address, int port, std::string url, std::string ca_path, WsCallback *callback);
~WsClient();
void start();
void stop();
void sendMessage(std::shared_ptr<std::string> message);
void drainMessageQueue(struct lws* wsi);
private:
static int lwsCallback(struct lws* wsi, enum lws_callback_reasons reason,
void* userData, void* in, size_t len);
static void lwsThread(WsClient* pClient);
void lwsTask();
std::string address_;
int port_;
std::string path_;
std::string ca_path_;
uint64_t last_ping_tick_;
// websocket sending and receiving thread
std::thread *lws_thread_;
std::recursive_mutex lock_;
// It will be used when the websocket is reconnected.
struct lws *lws_client_;
// Thread exit flag stop && noMsg
volatile bool stop_;
// noMsg is used to ensure that messages leaving the room are sent out before the thread exits
std::atomic_bool no_msg_;
struct lws_protocols protocols_[2];
unsigned char *buffer_;
// Used to store messages received by the websocket
std::string message_;
// Pending message queue
std::queue<std::shared_ptr<std::string>> message_queue_;
// Callback, please do not handle time-consuming operations in callbacks
WsCallback *callback_;
};