-
Notifications
You must be signed in to change notification settings - Fork 11
/
WebSocketStreamClient.h
106 lines (90 loc) · 1.95 KB
/
WebSocketStreamClient.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef __WEBSOCKETSTREAMCLIENT_H_
#define __WEBSOCKETSTREAMCLIENT_H_
#include <WiFiClientSecure.h>
#include "WebSocketClient250.h"
#include <Client.h>
class WebSocketStreamClient : public Client
{
public:
WebSocketStreamClient(WebSocketClient250 &webSocketClient, const char *path)
{
_webSocketClient = &webSocketClient;
_path = path;
}
int connect(const IPAddress ip, uint16_t port)
{
_webSocketClient->begin(_path);
return 1;
}
int connect(const char *host, uint16_t port)
{
_webSocketClient->begin(_path);
return 1;
}
size_t write(uint8_t b)
{
if (!connected())
return -1;
return write(&b, 1);
}
size_t write(const uint8_t *buf, size_t size)
{
if (!connected())
return -1;
_webSocketClient->beginMessage(TYPE_BINARY);
_webSocketClient->write(buf, size);
_webSocketClient->endMessage();
return size;
}
int available()
{
if (!connected())
return 0;
if (_webSocketClient->available() == 0)
_webSocketClient->parseMessage();
return _webSocketClient->available();
}
int read()
{
if (!connected() || !available())
return -1;
return _webSocketClient->read();
}
int read(uint8_t *buf, size_t size)
{
if (!connected() || !available())
return -1;
return _webSocketClient->read(buf, size);
}
int peek()
{
if (!connected() || !available())
return -1;
return _webSocketClient->peek();
}
void flush() {
if (!connected())
return;
_webSocketClient->flush();
return;
}
void stop()
{
if (!connected())
return;
_webSocketClient->stop();
return;
}
uint8_t connected()
{
return _webSocketClient->connected();
}
operator bool()
{
return _webSocketClient != NULL;
}
private:
WebSocketClient250 *_webSocketClient;
const char *_path;
};
#endif