Replies: 4 comments 5 replies
-
IWebSocketServer *server{nullptr};
void setup() {
if (...) {
server = new WebSocketServer<EthernetServer, EthernetClient, 4>;
}
else {
server = new WebSocketServer<WiFiServer, WiFiClient, 4>;
}
}
void loop() {
server->listen();
} Do you expect something like that? |
Beta Was this translation helpful? Give feedback.
-
Fixed. Try the following code: #include <WiFi.h> // or ESP8266WiFi.h
using WiFiWebSocketServer = WebSocketServer<WiFiServer, WiFiClient, 4>;
#include <Ethernet.h>
using EthernetWebSocketServer = WebSocketServer<EthernetServer, EthernetClient, 4>;
IWebSocketServer *wss{ nullptr };
void setup() {
// setup network ...
if (useEthernet) {
wss = new EthernetWebSocketServer{};
} else {
wss = new WiFiWebSocketServer{};
}
wss->onConnection([](IWebSocket &ws) {
const auto protocol = ws.getProtocol();
if (protocol) {
_SERIAL.print(F("Client protocol: "));
_SERIAL.println(protocol);
}
ws.onMessage([](IWebSocket &ws, const WebSocketDataType dataType,
const char *message, uint16_t length) {
switch (dataType) {
case net::WebSocketDataType::TEXT:
_SERIAL.print(F("Received: "));
_SERIAL.println(message);
break;
case net::WebSocketDataType::BINARY:
_SERIAL.println(F("Received binary data"));
break;
}
ws.send(dataType, message, length);
});
ws.onClose(
[](IWebSocket &, const WebSocketCloseCode, const char *, uint16_t) {
_SERIAL.println(F("Disconnected"));
});
_SERIAL.print(F("New client: "));
_SERIAL.println(ws.getRemoteIP());
const char message[]{ "Hello from Arduino server!" };
ws.send(WebSocketDataType::TEXT, message, strlen(message));
});
wss->begin();
}
void loop() {
wss->listen();
} |
Beta Was this translation helpful? Give feedback.
-
Hi, it seems that calling shutdown on wss is crashing: wss->shutdown(); log: CORRUPT HEAP: Bad head at 0x3fca6f50. Expected 0xabba1234 got 0x3fca3784 while when we call delete wss , which I do believe calls shutdown in turn, it executes properly. Is it something I should further investigate or maybe you have a clue ? |
Beta Was this translation helpful? Give feedback.
-
Hi, again me. I have a function setWSServerwhich is triggered upon network connection change (either Wifi or Ethernet connect, so connection would be tggled. When Eth is on, Wifi is off and vice versa) IWebSocketServer *wss{ nullptr };
void setWSServer(){
if (wss != nullptr){
wss->shutdown();
delete wss;
}
if (hwm->configMedia->media == CABLE_MEDIA) wss = new EthernetWebSocketServer{};
else wss = new WiFiWebSocketServer{};
wss->onConnection(socketConnectionCallback);
wss->begin();
}
It works ok, when you disconnect Ethernet, Wifi take over and when you unplug Eth cable, Wifi Take over and sockets are working fine. Side observation :
if (server == NULL) server = new EthernetServer(80);
if (media != CABLE_MEDIA) server->begin(); // <-- for some reason this begin cause the webserver not react with ethernet if it is called. But is required for WiFi
Your support is as usual much appreciated |
Beta Was this translation helpful? Give feedback.
-
This would cover the need of having a controller with both ethernet and Wifi connectivity without having to instantiate multiple object and double the code
Beta Was this translation helpful? Give feedback.
All reactions