Git Product home page Git Product logo

Comments (5)

xgarb avatar xgarb commented on May 23, 2024 1

I got distracted by another project. I'll get back to this one soon!

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 23, 2024

Hi! Thank you for opening an issue.

What your code in loop does, is:

  1. accept a websockets connection
  2. read 1 message from the connection
  3. 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 using msg.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 using msg.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.

xgarb avatar xgarb commented on May 23, 2024

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.

gilmaimon avatar gilmaimon commented on May 23, 2024

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.

gilmaimon avatar gilmaimon commented on May 23, 2024

Is it working now?

from arduinowebsockets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.