WebsocketClient.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/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 #pragma once
19 
22 
32 {
33 public:
35  {
36  }
37 
39  {
40  }
41 
46 
48 
52  bool connect(const Url& url);
53 
57 
64  void sendPing(const String& payload = nullptr)
65  {
66  debug_d("Sending PING");
67  WebsocketConnection::send(payload.c_str(), payload.length(), WS_FRAME_PING);
68  }
69 
75  void sendPong(const String& payload = nullptr)
76  {
77  debug_d("Sending PONG");
78  WebsocketConnection::send(payload.c_str(), payload.length(), WS_FRAME_PONG);
79  }
80 
83 
89  {
90  sslInitHandler = handler;
91  }
92 
97  {
98  close();
99  }
100 
101 protected:
102  int verifyKey(HttpConnection& connection, HttpResponse& response);
103 
104 private:
105  Url uri;
106  String key;
107  Ssl::Session::InitDelegate sslInitHandler;
108 };
109 
Class to manage URL instance.
Definition: Url.h:66
Definition: WebsocketConnection.h:60
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:125
WebsocketClient()
Definition: WebsocketClient.h:34
WebsocketClient(HttpConnection *connection)
Definition: WebsocketClient.h:38
void disconnect()
Disconnects websocket client from server.
Definition: WebsocketClient.h:96
int verifyKey(HttpConnection &connection, HttpResponse &response)
The String class.
Definition: WString.h:136
HttpConnection * getHttpConnection()
void sendBinary(const uint8_t *data, size_t length)
Sends a binary websocket message.
Definition: WebsocketConnection.h:135
void sendPing(const String &payload=nullptr)
Send websocket ping to server.
Definition: WebsocketClient.h:64
void setBinaryHandler(WebsocketBinaryDelegate handler)
Sets the callback handler to be called after a binary websocket message is received.
Definition: WebsocketConnection.h:209
#define debug_d
Definition: debug_progmem.h:100
bool connect(const Url &url)
Connects websocket client to server.
#define SMING_DEPRECATED
Definition: sming_attr.h:30
void sendPong(const String &payload=nullptr)
Send websocket ping to server.
Definition: WebsocketClient.h:75
void setSslInitHandler(Ssl::Session::InitDelegate handler)
Set the SSL session initialisation callback.
Definition: WebsocketClient.h:88
WsConnectionState getState()
Gets the state of the websocket connection.
Definition: WebsocketConnection.h:258
void setConnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called after successful websocket connection.
Definition: WebsocketConnection.h:191
void setDisconnectionHandler(WebsocketDelegate handler)
Sets the callback handler to be called before closing a websocket connection.
Definition: WebsocketConnection.h:218
Provides http base used for client and server connections.
Definition: HttpConnection.h:27
void close()
Closes a websocket connection (without closing the underlying http connection.
Definition: HttpClientConnection.h:27
Websocket Client.
Definition: WebsocketClient.h:31
Definition: HttpResponse.h:20
void setMessageHandler(WebsocketMessageDelegate handler)
Sets the callback handler to be called after a websocket message is received.
Definition: WebsocketConnection.h:200