REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef ARDUINO
4
5#include "websockets/common.h"
9#include "websockets/tcp.h"
10
11#include <functional>
12#include <memory>
13#include <vector>
14
15namespace websockets {
17
18class WebsocketsClient;
19typedef std::function<void(WebsocketsClient &, WebsocketsMessage)> MessageCallback;
20typedef std::function<void(WebsocketsMessage)> PartialMessageCallback;
21
22typedef std::function<void(WebsocketsClient &, WebsocketsEvent, std::string)> EventCallback;
23typedef std::function<void(WebsocketsEvent, std::string)> PartialEventCallback;
24
26public:
28 WebsocketsClient(std::shared_ptr<network::TcpClient> client);
29
32
35
37 void addHeader(const std::string key, const std::string value);
38
39 // bool connect(const std::string url);
40 // bool connect(const std::string host, const int port, const std::string path);
41
42 void onMessage(const MessageCallback callback);
43 void onMessage(const PartialMessageCallback callback);
44
45 void onEvent(const EventCallback callback);
46 void onEvent(const PartialEventCallback callback);
47
48 bool poll();
49 bool available(const bool activeTest = false);
50
51 bool send(const std::string &&data);
52 bool send(const std::string &data);
53 bool send(const char *data);
54 bool send(const char *data, const size_t len);
55
56 bool sendBinary(const std::string data);
57 bool sendBinary(const char *data, const size_t len);
58
59 // stream messages
60 bool stream(const std::string data = "");
61 bool streamBinary(const std::string data = "");
62 bool end(const std::string data = "");
63
64 void setFragmentsPolicy(const FragmentsPolicy newPolicy);
66
68
69 bool ping(const std::string data = "");
70 bool pong(const std::string data = "");
71
72 void close(const CloseReason reason = CloseReason_NormalClosure);
74
75 void setUseMasking(bool useMasking) { _endpoint.setUseMasking(useMasking); }
76
77 virtual ~WebsocketsClient();
78
79 std::shared_ptr<network::TcpClient> client() { return _client; }
80
81private:
82 std::shared_ptr<network::TcpClient> _client;
83 std::vector<std::pair<std::string, std::string>> _customHeaders;
85 bool _connectionOpen;
86 MessageCallback _messagesCallback;
87 EventCallback _eventsCallback;
88
89 enum SendMode { SendMode_Normal, SendMode_Streaming } _sendMode;
90
91 void _handlePing(WebsocketsMessage);
92 void _handlePong(WebsocketsMessage);
93 void _handleClose(WebsocketsMessage);
94
95 // void upgradeToSecuredConnection();
96};
97} // namespace websockets
98
99#endif // ARDUINO
bool ping(const std::string data="")
Definition client.cpp:271
bool end(const std::string data="")
Definition client.cpp:242
CloseReason getCloseReason() const
Definition client.cpp:293
void onMessage(const MessageCallback callback)
Definition client.cpp:117
void close(const CloseReason reason=CloseReason_NormalClosure)
Definition client.cpp:285
FragmentsPolicy getFragmentsPolicy() const
void setUseMasking(bool useMasking)
Definition client.h:75
std::shared_ptr< network::TcpClient > client()
Definition client.h:79
bool stream(const std::string data="")
Definition client.cpp:226
bool send(const std::string &&data)
Definition client.cpp:180
bool available(const bool activeTest=false)
Definition client.cpp:255
bool streamBinary(const std::string data="")
Definition client.cpp:234
void onEvent(const EventCallback callback)
Definition client.cpp:125
void addHeader(const std::string key, const std::string value)
Definition client.cpp:112
bool sendBinary(const std::string data)
Definition client.cpp:204
WebsocketsClient & operator=(const WebsocketsClient &other)
Definition client.cpp:46
void setFragmentsPolicy(const FragmentsPolicy newPolicy)
Definition client.cpp:250
bool pong(const std::string data="")
Definition client.cpp:278
WebsocketsMessage readBlocking()
Definition client.cpp:162
void setUseMasking(bool useMasking)
Definition endpoint.h:70
std::function< void(WebsocketsEvent, std::string)> PartialEventCallback
Definition client.h:23
@ CloseReason_NormalClosure
Definition endpoint.h:19
std::function< void(WebsocketsMessage)> PartialMessageCallback
Definition client.h:20
WebsocketsEvent
Definition client.h:16
std::function< void(WebsocketsClient &, WebsocketsEvent, std::string)> EventCallback
Definition client.h:22
std::function< void(WebsocketsClient &, WebsocketsMessage)> MessageCallback
Definition client.h:19