-
Couldn't load subscription status.
- Fork 21
Ethernet with SSL #17
Description
I am trying to connect to a Socket.IO server with Ethernet and SSL.
The Board: MKR WiFi1010
The Ethernet Shield: MKR ETH stacked on top of the MKR WiFi1010
I was thinking of using the EthernetWebServer_SSL library.
EthernetClient client;
EthernetSSLClient sslClient(client, TAs, (size_t)TAs_NUM);
However, I do not see a way on how to achieve the connection with the SocketIO client.
SocketIOclient socketIO;
Obviously, in my code there is no SSL connection yet. And therefore the connection does not work as can be seen in the following log:
Connected! My IP address: 192.168.1.65
Connecting to WebSockets Server @ IP address: test.test.com, port: 443
[WS] WebSockets_Generic v2.11.1
[WS] [wsIOc] found EIO=4 disable EIO ping on client
[WS] [WS-Client] Connect wss...
Receiving Payload:
[IOc] Disconnected
[WS] [wsIOc] Disconnected!
Below is my entire code. The SSL-certifcate is placed in the trust_anchors.h file as described in the EthernetWebServer_SSL library.
Questions:
-
How do I link the
sslClientto thesocketIOclient correctly ? Or is this the wrong way at all ? -
How would I achieve SSL and Ethernet with the WebSocket_Generic's Socket.IO library ?
-
Is the call to
socketIO.beginSSL(server, serverPort);automatically doing SSL ? Or is there something missing ? How do I achieve SSL with Socket.IO ?
Thanks.
#include <EthernetWebServer_SSL.h>
#include "trust_anchors.h" // You must have Trihow SSL Certificates here
#include "arduino_secrets.h"
#include <ArduinoJson.h>
#include <Adafruit_NeoPixel.h>
#include <Ramp.h>
#define BOARD_NAME "ARDUINO_SAMD_MKRWIFI1010"
#define USE_THIS_SS_PIN 5 // MKR ETH shield
#define _WEBSOCKETS_LOGLEVEL_ 3
#define WEBSOCKETS_NETWORK_TYPE NETWORK_WIFININA
// For transport=websocket instead of transport=polling
#define USING_STICKY_SESSION_SIO false
#include <ArduinoJson.h>
#include <WebSocketsClient_Generic.h>
#include <SocketIOclient_Generic.h>
SocketIOclient socketIO;
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x28, 0x9C }; // MAC Adress of Arduino MKR ETH Shield
uint16_t serverPort = SOCKET_IO_PORT; // from arduino_secrets.h
char server[] = SOCKET_IO_SERVER_URL; // from arduino_secrets.h
EthernetClient client;
EthernetSSLClient sslClient(client, TAs, (size_t)TAs_NUM);
unsigned long messageTimestamp = 0;
void socketIOEvent(socketIOmessageType_t type, uint8_t* payload, size_t length) {
Serial.println("Receiving Payload:") ;
Serial.println((char*)payload);
switch (type) {
case sIOtype_DISCONNECT:
Serial.println("[IOc] Disconnected");
break;
case sIOtype_CONNECT:
Serial.print("[IOc] Connected to url: ");
Serial.println((char*)payload);
// join default namespace (no auto join in Socket.IO V3)
socketIO.send(sIOtype_CONNECT, "/");
break;
case sIOtype_EVENT:
Serial.print("[IOc] Get event: ");
Serial.println((char*)payload);
break;
case sIOtype_ACK:
Serial.print("[IOc] Get ack: ");
Serial.println(length);
//hexdump(payload, length);
break;
case sIOtype_ERROR:
Serial.print("[IOc] Get error: ");
Serial.println(length);
//hexdump(payload, length);
break;
case sIOtype_BINARY_EVENT:
Serial.print("[IOc] Get binary: ");
Serial.println(length);
//hexdump(payload, length);
break;
case sIOtype_BINARY_ACK:
Serial.print("[IOc] Get binary ack: ");
Serial.println(length);
//hexdump(payload, length);
break;
default:
Serial.println("[IOc] Default case...");
break;
}
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
delay(2000);
Ethernet.init(USE_THIS_SS_PIN);
Ethernet.begin(mac);
Serial.print(F("Connected! My IP address: "));
Serial.println(Ethernet.localIP());
// give the Ethernet shield a second to initialize:
delay(2000);
// server address, port and URL
Serial.print("Connecting to WebSockets Server @ IP address: ");
Serial.print(server);
Serial.print(", port: ");
Serial.println(serverPort);
// setReconnectInterval to 10s, new from v2.5.1 to avoid flooding server. Default is 0.5s
socketIO.setReconnectInterval(10000);
socketIO.setExtraHeaders(SOCKET_IO_AUTHORIZATION); // from arduino_secrets.h
socketIO.beginSSL(server, serverPort);
socketIO.onEvent(socketIOEvent);
}
void loop() {
socketIO.loop();
uint64_t now = millis();
if (now - messageTimestamp > 30000) {
messageTimestamp = now;
// create JSON message for Socket.IO (event)
DynamicJsonDocument doc(1024);
JsonArray array = doc.to<JsonArray>();
// add event name // Hint: socket.on('event_name', ....
array.add("subscribe");
// add payload (parameters) for the event
JsonObject param = array.createNestedObject();
param["userName"] = "Default Test";
param["sessionId"] = "th-VIZsyFibjFj1xGLPL4fk";
// JSON to String (serializion)
String output;
output += "0"; // same as Web
serializeJson(doc, output);
// Send event
socketIO.sendEVENT(output);
// Print JSON for debugging
Serial.println(output);
}
}