From 098c488ff0767672dc1a3d6631aee58ba42d8440 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 10 Dec 2015 10:10:06 +0100 Subject: [PATCH] add beginSSL --- examples/WebSocketClient/WebSocketClient.ino | 2 +- .../WebSocketClientSSL/WebSocketClientSSL.ino | 88 +++++++++++++++++++ src/WebSocketsClient.cpp | 11 +++ src/WebSocketsClient.h | 5 ++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 examples/WebSocketClientSSL/WebSocketClientSSL.ino diff --git a/examples/WebSocketClient/WebSocketClient.ino b/examples/WebSocketClient/WebSocketClient.ino index 39dcaf0..e68e97c 100644 --- a/examples/WebSocketClient/WebSocketClient.ino +++ b/examples/WebSocketClient/WebSocketClient.ino @@ -32,7 +32,7 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) { USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); // send message to server when Connected - webSocket.sendTXT(num, "Connected"); + webSocket.sendTXT("Connected"); } break; case WStype_TEXT: diff --git a/examples/WebSocketClientSSL/WebSocketClientSSL.ino b/examples/WebSocketClientSSL/WebSocketClientSSL.ino new file mode 100644 index 0000000..f4bced1 --- /dev/null +++ b/examples/WebSocketClientSSL/WebSocketClientSSL.ino @@ -0,0 +1,88 @@ +/* + * WebSocketClientSSL.ino + * + * Created on: 10.12.2015 + * + * note SSL is only possible with the ESP8266 + * + */ + +#include + +#include +#include + +#include + +#include + +ESP8266WiFiMulti WiFiMulti; +WebSocketsClient webSocket; + + +#define USE_SERIAL Serial1 + +void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) { + + + switch(type) { + case WStype_DISCONNECTED: + USE_SERIAL.printf("[WSc] Disconnected!\n"); + break; + case WStype_CONNECTED: + { + USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); + + // send message to server when Connected + webSocket.sendTXT("Connected"); + } + break; + case WStype_TEXT: + USE_SERIAL.printf("[WSc] get text: %s\n", payload); + + // send message to server + // webSocket.sendTXT("message here"); + break; + case WStype_BIN: + USE_SERIAL.printf("[WSc] get binary lenght: %u\n", lenght); + hexdump(payload, lenght); + + // send data to server + // webSocket.sendBIN(payload, lenght); + break; + } + +} + +void setup() { + // USE_SERIAL.begin(921600); + USE_SERIAL.begin(115200); + + //Serial.setDebugOutput(true); + USE_SERIAL.setDebugOutput(true); + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for(uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + WiFiMulti.addAP("SSID", "passpasspass"); + + //WiFi.disconnect(); + while(WiFiMulti.run() != WL_CONNECTED) { + delay(100); + } + + webSocket.beginSSL("192.168.0.123", 81); + webSocket.onEvent(webSocketEvent); + +} + +void loop() { + webSocket.loop(); +} diff --git a/src/WebSocketsClient.cpp b/src/WebSocketsClient.cpp index 2848893..9d2edeb 100644 --- a/src/WebSocketsClient.cpp +++ b/src/WebSocketsClient.cpp @@ -70,6 +70,17 @@ void WebSocketsClient::begin(String host, uint16_t port, String url) { begin(host.c_str(), port, url.c_str()); } +#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) +void WebSocketsClient::beginSSL(const char *host, uint16_t port, const char * url) { + begin(host, port, url); + _client.isSSL = true; +} + +void WebSocketsClient::beginSSL(String host, uint16_t port, String url) { + beginSSL(host.c_str(), port, url.c_str()); +} +#endif + /** * called in arduino loop */ diff --git a/src/WebSocketsClient.h b/src/WebSocketsClient.h index f590edc..7e42cc4 100644 --- a/src/WebSocketsClient.h +++ b/src/WebSocketsClient.h @@ -50,6 +50,11 @@ class WebSocketsClient: private WebSockets { void begin(const char *host, uint16_t port, const char * url = "/"); void begin(String host, uint16_t port, String url = "/"); +#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) + void beginSSL(const char *host, uint16_t port, const char * url = "/"); + void beginSSL(String host, uint16_t port, String url = "/"); +#endif + void loop(void); void onEvent(WebSocketClientEvent cbEvent);