Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
WebsocketClient.h
1 /*
2  * File: WebsocketClient.h
3  * Author: https://github.com/hrsavla
4  *
5  * Created on August 4, 2015, 1:37 PM
6  * This Websocket Client library is ported by hrsavla into Sming from
7  * https://github.com/MORA99/Stokerbot/tree/master/Libraries/WebSocketClient
8  *
9  * Refactor and improve by https://github.com/avr39-ripe - Alexander V, Ribchansky
10  *
11  */
12 
13 #ifndef WEBSOCKETCLIENT_H
14 #define WEBSOCKETCLIENT_H
15 
16 #include "../Wiring/WiringFrameworkIncludes.h"
17 #include "TcpClient.h"
18 #include "../Delegate.h"
19 #include "URL.h"
20 #include "../../Services/WebHelpers/aw-sha1.h"
21 #include "../../Services/WebHelpers/base64.h"
22 #include "../../Wiring/WString.h"
23 #include "../Digital.h"
24 #include <Network/WebsocketFrame.h>
25 
30 class WebsocketClient;
33 enum class wsMode : uint8_t
34 {
35  Disconnected = 0, Connecting, Connected
36 };
37 
53 class WebsocketClient: protected TcpClient
54 {
55 public:
56  WebsocketClient(bool autoDestruct = false) :TcpClient(autoDestruct) {};
57  virtual ~WebsocketClient() {};
58  void setWebSocketMessageHandler(WebSocketClientMessageDelegate handler);
62  void setWebSocketDisconnectedHandler(WebSocketClientDisconnectDelegate handler);
66  void setWebSocketConnectedHandler(WebSocketClientConnectedDelegate handler);
70  void setWebSocketBinaryHandler(WebSocketClientBinaryDelegate handler);
74  bool connect(String url, uint32_t sslOptions = 0);
79  void sendPing();
82  void sendPong();
85  void disconnect();
88  void sendMessage(char* msg, uint16_t length);
93  void sendMessage(String str);
97  void sendBinary(uint8_t* msg, uint16_t length);
102  wsMode getWSMode();
107 #ifdef ENABLE_SSL
108  using TcpClient::addSslOptions;
109  using TcpClient::setSslFingerprint;
110  using TcpClient::pinCertificate;
111  using TcpClient::setSslClientKeyCert;
112  using TcpClient::freeSslClientKeyCert;
113  using TcpClient::getSsl;
114 #endif
115 
116 protected:
117  virtual void onFinished(TcpClientState finishState);
118  virtual void onError(err_t err);
119  virtual err_t onReceive(pbuf *buf);
120  bool _verifyKey(char *buf, int size);
121  void _sendFrame(WSFrameType frameType, uint8_t* msg, uint16_t length);
122 private:
123  URL _uri;
124  String _url;
125  wsMode _mode = wsMode::Disconnected;
126  WebSocketClientMessageDelegate wsMessage = nullptr;
127  WebSocketClientBinaryDelegate wsBinary = nullptr;
128  WebSocketClientDisconnectDelegate wsDisconnect = nullptr;
129  WebSocketClientConnectedDelegate wsConnect = nullptr;
130  String _key;
131 };
132 
133 #endif /* WEBSOCKETCLIENT_H */
134 
Definition: WString.h:42
Definition: TcpClient.h:35
WSFrameType
Websocket frame types enum.
Definition: WebsocketFrame.h:21
Delegate< void(WebsocketClient &wsClient, bool successful)> WebSocketClientDisconnectDelegate
Delegate callback type for received binary message.
Definition: WebsocketClient.h:44
Delegate< void(WebsocketClient &wsClient, wsMode Mode)> WebSocketClientConnectedDelegate
Delegate callback type for disconnection of websocket client.
Definition: WebsocketClient.h:47
wsMode
Websocket client modes enum.
Definition: WebsocketClient.h:33
Definition: URL.h:17
Delegate< void(WebsocketClient &wsClient, uint8_t *data, size_t size)> WebSocketClientBinaryDelegate
Delegate callback type for received text message.
Definition: WebsocketClient.h:41
Delegate callback type for connection of websocket client.
Definition: WebsocketClient.h:53