Products > Programming

Websockets client.readnonblocking but it is blocking !

(1/1)

timdf911:
Hi,
I hope this is the right part of the forum for this question.

I'm running a websocket server on a Blackpill STM32F11 using Arduino code from websockets2_generic.
The websocket server part works fine but it blocks any other code from running at the point commented below

This is the code from the example.  The  " if (client.available())" statement results in it accepting only one message before closing the socket.  Changing the `if` to `while` allows continual message traffic but still doesn't allow sensor updates due to the `while loop` additionally `client.readnonblocking` is still blocking anyway.


--- Code: ---  // You can do some other tasks here, such as WebServer, controlling tasks, etc.
 
  WebsocketsClient client = SocketsServer.accept();

  if (client.available())
  {
    WebsocketsMessage msg = client.readNonBlocking();  // the code always hangs here waiting for traffic despite claiming to be non blocking

    // log
    Serial.print("Got Message: ");
    Serial.println(msg.data());

    // return echo
    client.send("Echo: " + msg.data());

    // close the connection
    client.close();
  }
--- End code ---

I've googled this to death but have yet to find a solution.
I need to be able to monitor other sensors and be able to send them to the client, but so far this has not been possible due to the blocking effect of client.readnonblocking statement.

Any pointer as to how I can fix this ?

Regards Tim

magic:

--- Quote from: timdf911 on November 06, 2023, 07:00:11 pm ---I hope this is the right part of the forum for this question.

--- End quote ---
Probably not TBH, and you seem to have already posted the same thread in "Programming", which is better.
Note that double posting is against the rules here because it's messy when discussion is spread across multiple places.

AndyBeez:

--- Code: ---// close the connection
    client.close()
--- End code ---

Put this outside the conditional?

Make the conditional a while loop?

abeyer:

--- Quote from: timdf911 on November 06, 2023, 06:53:22 pm ---This is the code from the example.  The  " if (client.available())" statement results in it accepting only one message before closing the socket.  Changing the `if` to `while` allows continual message traffic but still doesn't allow sensor updates due to the `while loop`
--- End quote ---

Yup. If you need to handle multiple streams and/or multiple messages per stream you need to multiplex them yourself, just looping will hang your other logic even if nonblocking io was working


--- Quote from: timdf911 on November 06, 2023, 06:53:22 pm ---additionally `client.readnonblocking` is still blocking anyway.
I've googled this to death but have yet to find a solution.

--- End quote ---

The repo is archived without any notes as to where it might be supported now... so presumably it's not.

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod