Author Topic: Websockets client.readnonblocking but it is blocking !  (Read 472 times)

0 Members and 1 Guest are viewing this topic.

Offline timdf911Topic starter

  • Contributor
  • Posts: 23
  • Country: gb
Websockets client.readnonblocking but it is blocking !
« on: November 06, 2023, 06:53:22 pm »
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: [Select]
  // 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();
  }

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
Tinkering for over 50 years and still learning  G4WIM KT6UK
 

Offline magic

  • Super Contributor
  • ***
  • Posts: 6779
  • Country: pl
Re: Websockets - readnonblocking - but it is blocking
« Reply #1 on: November 06, 2023, 07:33:52 pm »
I hope this is the right part of the forum for this question.
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.
 

Offline AndyBeez

  • Frequent Contributor
  • **
  • Posts: 856
  • Country: nu
Re: Websockets client.readnonblocking but it is blocking !
« Reply #2 on: November 06, 2023, 07:40:46 pm »
Code: [Select]
// close the connection
    client.close()

Put this outside the conditional?

Make the conditional a while loop?
« Last Edit: November 06, 2023, 10:51:20 pm by AndyBeez »
 

Offline abeyer

  • Frequent Contributor
  • **
  • Posts: 292
  • Country: us
Re: Websockets client.readnonblocking but it is blocking !
« Reply #3 on: November 06, 2023, 10:14:38 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`

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

additionally `client.readnonblocking` is still blocking anyway.
I've googled this to death but have yet to find a solution.

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


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf