Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
WebsocketClient.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * WebsocketClient.h
8  *
9  * @authors:
10  * Originally - hrsavla <https://github.com/hrsavla>
11  * Refactored - Alexander V, Ribchansky <https://github.com/avr39-ripe>
12  * Refactored - Slavey Karadzhov <slav@attachix.com>
13  *
14  ****/
15 
16 //TODO: Add stream support for sending big chunks of data via websockets.
17 
18 #ifndef _SMING_CORE_NETWORK_WEBSOCKET_CLIENT_H_
19 #define _SMING_CORE_NETWORK_WEBSOCKET_CLIENT_H_
20 
21 #include "Http/HttpClientConnection.h"
22 #include "Http/Websocket/WebsocketConnection.h"
23 
33 {
34 public:
36  {
37  }
38 
39  WebsocketClient(HttpConnection* connection) : WebsocketConnection(connection)
40  {
41  }
42 
47 
48  HttpConnection* getHttpConnection();
49 
54  bool connect(const Url& url, uint32_t sslOptions = 0);
55 
59 
66  void sendPing(const String& payload = nullptr)
67  {
68  debug_d("Sending PING");
69  WebsocketConnection::send(payload.c_str(), payload.length(), WS_FRAME_PING);
70  }
71 
77  void sendPong(const String& payload = nullptr)
78  {
79  debug_d("Sending PONG");
80  WebsocketConnection::send(payload.c_str(), payload.length(), WS_FRAME_PONG);
81  }
82 
85 
89  void disconnect() SMING_DEPRECATED
90  {
91  close();
92  }
93 
94 protected:
95  int verifyKey(HttpConnection& connection, HttpResponse& response);
96 
97 private:
98  Url uri;
99  String key;
100 };
101 
103 #endif /* _SMING_CORE_NETWORK_WEBSOCKET_CLIENT_H_ */
Class to manage URL instance.
Definition: Url.h:66
Definition: WebsocketConnection.h:61
virtual void send(const char *message, size_t length, ws_frame_type_t type=WS_FRAME_TEXT)
Sends a websocket message from a buffer.
void sendString(const String &message)
Sends a string websocket message.
Definition: WebsocketConnection.h:126
void disconnect() SMING_DEPRECATED
Disconnects websocket client from server.
Definition: WebsocketClient.h:89
The string class.
Definition: WString.h:104
void sendBinary(const uint8_t *data, size_t length)
Sends a binary websocket message.
Definition: WebsocketConnection.h:136
void sendPing(const String &payload=nullptr)
Send websocket ping to server.
Definition: WebsocketClient.h:66
void setBinaryHandler(WebsocketBinaryDelegate handler)
Sets the callback handler to be called after a binary websocket message is received.
Definition: WebsocketConnection.h:210
void sendPong(const String &payload=nullptr)
Send websocket ping to server.
Definition: WebsocketClient.h:77
WsConnectionState getState()
Gets the state of the websocket connection.
Definition: WebsocketConnection.h:259
void setConnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called after successful websocket connection.
Definition: WebsocketConnection.h:192
void setDisconnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called before closing a websocket connection.
Definition: WebsocketConnection.h:219
Definition: HttpConnection.h:29
bool connect(const Url &url, uint32_t sslOptions=0)
Connects websocket client to server.
void close()
Closes a websocket connection (without closing the underlying http connection.
Definition: HttpClientConnection.h:28
Websocket Client.
Definition: WebsocketClient.h:32
Definition: HttpResponse.h:24
void setMessageHandler(WebsocketMessageDelegate handler)
Sets the callback handler to be called after a websocket message is received.
Definition: WebsocketConnection.h:201
WebsocketConnection(HttpConnection *connection, bool isClientConnection=true)
Constructs a websocket connection on top of http client or server connection.