Comments (5)
I got distracted by another project. I'll get back to this one soon!
from arduinowebsockets.
Hi! Thank you for opening an issue.
What your code in loop
does, is:
- accept a websockets connection
- read 1 message from the connection
- print that message
So, for every iteration of loop
you accept a new connection. It is not completely sure what you are trying to do, but if I understand correctly you want to accept a connection and then print everything that is sent to you in that connection?
You can do this:
void loop() {
auto client = server.accept();
while(client.available()) {
Serial.println("client.available");
auto msg = client.readBlocking();
// log
Serial.print("Got Message: ");
Serial.println(msg.data());
}
}
Notice how I changed the if(client.available())
to while(client.available())
! So now, while the client is connected, the code will print everything the client sends. If the client disconnects, it will exit the loop and will wait for the next client to connect. Is that more like what you want?
Few notes:
- If the client will disconnect,
readBlocking
will return an empty message. You can check if a message is empty by usingmsg.isEmpty()
. readBlocking
will also return messages that are not data. For example pings, pongs, and close messages (those are all control messages in the websockets protocol). You can check if a message is text by usingmsg.isText()
.
You can get rid of those checks if you use the callback-based interface. It will look like this:
void handle_message(WebsocketsMessage msg) {
// log
Serial.print("Got Message: ");
Serial.println(msg.data());
}
void loop() {
auto client = server.accept();
client.onMessage(handle_message);
while(client.available()) {
client.poll();
}
}
This code accepts a connection, and while the connection is open, does polling
. Polling is checking if there is anything new, and if there is, it will call the callback (in this case, I called it handle_message
,
Gil.
from arduinowebsockets.
Thanks for your detailed response. The final project is to add pan and tilt to an ESP32 based camera board. For this I need to send a 'stream' of jpgs from the camera to the browser and the browser to be able to send a 'stream' of co-ordinates (from a touch interface) to the ESP.
I've tried nearly every combination of async, sync http and websocket servers to get this working and the solution is always just around the corner!
Your code below worked perfectly the first time I tried
void handle_message(WebsocketsMessage msg) {
// log
Serial.print("Got Message: ");
Serial.println(msg.data());
int panValue = msg.data().toInt();
ledcAnalogWrite(2, panValue + 90);
}
void loop() {
auto client = server.accept();
client.onMessage(handle_message);
while (client.available()) {
client.poll();
}
}
I had the servo whizzing back and forth but now it stops after a bit with the same error but I think this is because the ESP32 has locked up. Maybe it's Wifi related that it worked better before. I should probably restrict the number of times coordinates are sent from the browser.
This code worked really well for streaming the camera data:
void loop() {
auto client = server.accept();
client.onMessage(handle_message);
while (client.available()) {
cam.run();
client.sendBinary((const char*)cam.getfb(), (size_t)cam.getSize());
}
}
I'll try again with it tomorrow. Here's my messy code if you want to try it: https://pastebin.com/cs0CSntM
from arduinowebsockets.
Interesting. You can try adding some delay between calls to poll. Also it is a very good idea to add delays between client.send..
calls. From previous experiances it seems like the esp32's internal buffer will overflow if you keep sending without any delay (so it does not finish actually sending the buffer before it overflows).
Also, if you want to see more info about the errors with the esp32, change the board to "ESP32 Dev Module" and change Core Debug Level
to Error
or if you really want to see everything, to Verbose
.
Share the crash/fail logs here, it might help.
Also, where do you see the error "WebSocket is already in CLOSING or CLOSED state"? Is it in the browser that sends the data or in the esp32?
Gil.
from arduinowebsockets.
Is it working now?
from arduinowebsockets.
Related Issues (20)
- ESP32-Server Not Working Properly receives one msg and gets disconnected HOT 3
- How to know hostname and IP from client? HOT 1
- esp32 to wss python server
- ESP32 Websocket Client doesn't connect to Node.js Websocket server HOT 3
- poll() disconnects client HOT 7
- How to get a reference of the web socket client that dispatched the msg callback
- Hieromon/AutoConnect & WebSockets Client on ESP8266 - Can't Establish wss
- Secured-ESP8266-Client can't seem to receive any messages while connection is open HOT 1
- Compilation error: 'WebsocketsClient' does not name a type ?? HOT 3
- Getting a error from the TCPServer using the Arduino GIGA R1 WIFI on the ESP-32Client code. HOT 3
- ESP32-CAM WebSocket Server: Failure to Stream Camera Feed after Page Refresh or Multiple Connections
- WSS Connection error
- [ 5236][E][WiFiClient.cpp:313] setSocketOption(): fail on -1, errno: 9, "Bad file number" HOT 2
- Reconnecting after lost connection HOT 2
- Disconnecting almost inmediately
- Random disconnects, fails on binary ping including 0x00 HOT 1
- Issue with WSS and ESP 32 HOT 4
- Esp32 WebSocket Client won't work with WebSocket Node.js server. HOT 3
- Client-Message-Buffer
- ESP32 Server disconnected when receiving messages
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinowebsockets.