Versions 1.4.0 and later break code that uses more than one WiFiServer and socket #87
Description
The change in WebSocketServer.cpp involving _lastSock ("check previous received client socket for data before querying a new one") broke one of my sketches that uses two WiFiServer-s and two sockets, one for a web server on port 80 and the other for a websocket server on port 8080.
Setting up the websocket works fine, but once the webserver on port 80 has processed a request, data from the websocket client (web browser) starts ending up on port 80 (the previously used socket) and not on port 8080 (where the websocket server is).
I'm not sure, but could it be that the change involving _lastSock needs a check to see if the endpoints (IP/port) of sock and lastsock are the same? It seems it is cross-connecting two different sockets now.
Anyway, the sketch below works fine with 1.3.0, but not with 1.4.0 or later:
#include <WiFiNINA.h>
#include <WebSocketServer.h>
#include <Arduino_LSM6DS3.h>
#include <Base64.h>
#include <RTCZero.h>
#include "wifi_secrets.h"
#define DEBUG 1
#ifdef DEBUG
#define Sprint(a) (Serial.print(a))
#define Sprintln(a) (Serial.println(a))
#else
#define Sprint(a)
#define Sprintln(a)
#endif
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
const int IPLastByte = 99;
const int webPort = 80;
const int socketPort = 8080;
WiFiServer webServer(webPort);
WiFiServer socketServer(socketPort);
WebSocketServer webSocketServer;
WiFiClient socketClient;
RTCZero rtc;
const int ledPin = 12;
boolean on = false;
float volume = 0;
int cowbell = 1000;
float zdata[] = {1,1,1,1,1};
int zdatasize = sizeof(zdata) / sizeof(zdata[0]);
float tdata[] = {20,20,20,20,20,20,20,20,20,20};
int tdatasize = sizeof(tdata) / sizeof(tdata[0]);
int ledState = 0;
unsigned long epoch;
unsigned long previousCowbellMillis = 0;
unsigned long previousGravityMillis = 0;
unsigned long previousClockMillis = 0;
unsigned long previousAdjustMillis = 0;
char webpage_base64[] = "H4sICABelF0CAHdlYnNvY2tldHNlcnZlci5odG1sAOxYe2/TMBD/H4nvEEATIMj70WQwJKdvYNDAxmOIP9LUedA8SuI2XRHfnXOcNekKiPGSkPBWx3e+O//ufBdHfnij97x78nbS50KSxI+uX3tIn1zspsHRTZzeBA7HPQyxO6tGMCY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FWaV1gksREmFCz26KDVRSHrKI+0KLLGi5+tYa/H5aQnixCmOdK0f5c0cSwt+j5wFOlXX9C9MmfnaVs9c6tzgLc6FsBjUcCJqjfGJ8MtujDYNtytKpN7YyW14zWJXFdTHf+vmmz6HNbCgCaVU2VnKYLPkt4Sk8AT2g0gHtobkZ1ECQYFYiHJt3Yh0GGUIF5YvIFwDRYVR8JI+qAUDJ0TwP8oJfYXWYRWnSrIUqNAtqAioC4Q4ckO/SoIWQVG80SGYB9JnK0iAjh0PLpN68jjkA5xLDfLI8YAuUgw8Pr19ZbukXZ9X3I9bSoFtPWcqLuFBupUWesZn70va2QleX5Km9u4GmmjG3I5t4ecn8bfYaK01BxGipOQ8VpqDgNFac/rzgdU206ttJ0dJWpjwrTH1SX/ryydExV6c8rSv1Wk/qtJPX1wqZZ4+kzu2xS7y/paKm79JlhtJDvM59okCfip0/1WzXqpWLUT7WosjutdkjuksvSXR5gUoEawMieDXCn+Caz2d2PkODBfFNksNyR9zqDZrMPS7yqm0G/9ZyhJd34Pq4f2Wt8cdLiFfmXvYwFWddstDs4iHzQwR9f3fJvMCLTpPlfKl3f2XSC5cVGWxXsp3O4UU5TVYXVD1463OxjsM4zmqk79qvC0P0+Gn0kpIZBhbWnxlayHOo2Oa5zEJhZABQ4BQt55rMWYpY9wBYHRbDvRmamltfg+bbO08CDYDBZrzH5t9/NF7PRz5RLhMbj6leId0dTqeE+PnztsvlPkX8ALLcu/yR5AAA=";
void setup() {
#ifdef DEBUG
Serial.begin(115200);
#endif
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);
rtc.begin();
IMU.begin();
WiFiConnect();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, LOW);
Sprintln("\n--Lost WiFi connection");
WiFi.end();
WiFiConnect();
}
//if (!socketClient.connected()) {
WiFiClient webClient = webServer.available();
if (webClient.connected()) {
Sprint("\n--New client: "); Sprint(webClient.remoteIP()); Sprint(":"); Sprintln(webClient.remotePort());
String header = "";
while (webClient.available()) {
char c = webClient.read();
if (c != '\r') {
header += c;
}
if (header.substring(0,1) != "G" && header.substring(0,2) != "GE" && header.substring(0,3) != "GET") {
Sprintln("--Wrong method in header");
webClient.println("HTTP/1.1 405 Method Not Allowed\nAllow: GET\nContent-Type: text/plain; charset=utf-8\n\n405 Method not allowed; GET only\n");
webClient.stop();
Sprintln("--Client disconnected");
}
if (header.length() > 512) {
Sprintln("--Header too long");
webClient.println("HTTP/1.1 431 Request Header Fields Too Large\nContent-Type: text/plain; charset=utf-8\n\n431 Request header fields too large\n");
webClient.stop();
Sprintln("--Client disconnected");
}
if (header.substring(header.length() - 2) == "\n\n") {
Sprint(header.substring(0, header.length() - 1));
if (header.indexOf("GET / HTTP") > -1) {
const int webpage_base64_length = sizeof(webpage_base64);
const int webpage_gz_length = base64_dec_len(webpage_base64, webpage_base64_length);
char webpage_gz[webpage_gz_length];
base64_decode(webpage_gz, webpage_base64, webpage_base64_length);
int packetsize = 1024;
int done = 0;
webClient.println("HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Encoding: gzip\n");
while (webpage_gz_length > done) {
webClient.write(webpage_gz + done, packetsize * sizeof(char));
done = done + packetsize;
if (webpage_gz_length - done < packetsize) {
packetsize = webpage_gz_length - done;
}
}
Sprintln("--Interface webpage sent");
} else if (header.indexOf("GET /author HTTP") > -1) {
webClient.println("HTTP/1.1 200 OK\nContent-Type: text/plain; charset=utf-8\n\nOscar den Uijl, oscar@den-uijl.nl\n");
Sprintln("--Email address sent");
} else {
webClient.println("HTTP/1.1 404 Not Found\nContent-Type: text/plain; charset=utf-8\n\n404 Not Found\n");
Sprintln("--Page not found");
}
webClient.stop();
Sprintln("--Client disconnected");
}
}
}
//}
if (!socketClient.connected()) {
socketClient = socketServer.available();
if (socketClient.connected() && webSocketServer.handshake(socketClient)) {
Sprint("\n--Websocket connected to: "); Sprint(socketClient.remoteIP()); Sprint(":"); Sprintln(socketClient.remotePort());
if (on) {
webSocketServer.sendData("sw:true");
} else {
webSocketServer.sendData("sw:false");
}
webSocketServer.sendData("volume:" + (String)round((volume*100)/255));
webSocketServer.sendData("cowbell:" + (String)((1000-cowbell)/10));
Sprintln("\n--Settings sent");
} else {
Sprintln("\n--Couldn't connect websocket");
socketClient.stop();
delay(100);
}
}
if (socketClient.connected()) {
String data = webSocketServer.getData();
if (data.length() > 0) {
String name = data.substring(0, data.indexOf(":"));
String value = data.substring(data.indexOf(":") + 1);
boolean goodSettings = true;
if (name == "switch") {
on = (value == "true");
} else if (name == "volume") {
volume = (value.toFloat()*255)/100;
if (cowbell == 1000) {
analogWrite(ledPin, (int) round(volume));
}
} else if (name == "cowbell") {
cowbell = 1000 - (value.toInt()*10);
} else {
goodSettings = false;
webSocketServer.sendData("message:Bad data; ignored");
}
if (goodSettings) {
webSocketServer.sendData("message:" + name + " set to " + value);
}
}
}
unsigned long currentCowbellMillis = millis();
if ((currentCowbellMillis - previousCowbellMillis) > cowbell) {
previousCowbellMillis = currentCowbellMillis;
if (on) {
if (cowbell == 1000) {
analogWrite(ledPin, (int) round(volume));
} else {
if (ledState == 0) {
ledState = 1;
analogWrite(ledPin, (int) round(volume));
} else {
ledState = 0;
analogWrite(ledPin, 0);
}
}
} else {
analogWrite(ledPin, 0);
}
}
float x, y, z, zsum = 0;
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
if (x > 0.2 || x < -0.2 || y > 0.2 || y < -0.2 || z > 1.2 || z < 0.8) {
analogWrite(ledPin, 0);
Sprintln("\nTILT!!");
if (on) {
on = false;
if (socketClient.connected()) {
webSocketServer.sendData("sw:false");
}
}
}
for (int i=0; i<zdatasize-1; i++) {
zdata[i] = zdata[i+1];
}
zdata[zdatasize-1] = z;
for (int i=0; i<zdatasize; i++) {
zsum += zdata[i];
}
if (socketClient.connected()) {
unsigned long currentGravityMillis = millis();
if ((currentGravityMillis - previousGravityMillis) > 2000) {
previousGravityMillis = currentGravityMillis;
webSocketServer.sendData("gravity:" + String(zsum/zdatasize, 3));
}
}
}
unsigned long currentClockMillis = millis();
if (socketClient.connected()) {
if ((currentClockMillis - previousClockMillis) > 1000) {
previousClockMillis = currentClockMillis;
webSocketServer.sendData("time:" + String(rtc.getEpoch()));
}
}
unsigned long currentAdjustMillis = millis();
if ((currentAdjustMillis - previousAdjustMillis) > 300000) {
previousAdjustMillis = currentAdjustMillis;
epoch = WiFi.getTime();
if (epoch > 0) {
rtc.setEpoch(epoch);
Sprintln("\n--RTC adjusted");
};
}
}
void WiFiConnect() {
WiFi.setHostname("nano-webserver");
while (WiFi.status() != WL_CONNECTED) {
Sprintln("Connecting to " + (String)ssid + " ...");
WiFi.begin(ssid, pass);
delay(5000);
}
IPAddress IP = WiFi.localIP();
IP[3] = IPLastByte;
WiFi.config(IP, WiFi.gatewayIP(), WiFi.gatewayIP(), WiFi.subnetMask());
Sprintln("Connected to " + (String)ssid);
int tries = 5;
do {
Sprintln("Getting NTP time ...");
epoch = WiFi.getTime();
tries--;
delay(500);
} while (epoch == 0 && tries > 0);
if (tries == 0) {
Sprintln("Failed to get NTP time");
} else {
rtc.setEpoch(epoch);
Sprintln("RTC set to NTP time");
}
webServer.begin();
socketServer.begin();
#ifdef DEBUG
printWifiStatus();
#endif
WiFi.lowPowerMode();
digitalWrite(LED_BUILTIN, HIGH);
}
#ifdef DEBUG
void printWifiStatus() {
Sprint("SSID: "); Sprintln(WiFi.SSID());
Sprint("Signal strength (RSSI): "); Sprint(WiFi.RSSI()); Sprintln(" dBm");
Sprint("IP address: "); Sprintln(WiFi.localIP());
Sprint("Gateway: "); Sprintln(WiFi.gatewayIP());
Sprint("Netmask: "); Sprintln(WiFi.subnetMask());
Sprint("Websocket is at http://"); Sprint(WiFi.localIP()); Sprintln(":" + (String)socketPort + "/");
}
#endif