Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to keep receiving messages in server #126

Closed
jaydip-pawar opened this issue Oct 6, 2021 · 23 comments
Closed

How to keep receiving messages in server #126

jaydip-pawar opened this issue Oct 6, 2021 · 23 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed usage queshtion Queshtions about using the library, its interface and so on

Comments

@jaydip-pawar
Copy link

#include <WiFi.h>
#include <ArduinoWebsockets.h>

const char* ssid = "SSID";
const char* password = "PASS";

using namespace websockets;
WebsocketsServer server;

void setup() {
  Serial.begin(115200);

  IPAddress apIP(192, 168, 0, 1);   //Static IP for wifi gateway
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); //set Static IP gateway on NodeMCU
  WiFi.softAP(ssid, password); //turn on Hotspot

  server.listen(80);
}

void loop() {
  auto client = server.accept();
  if(client.available()) {
    auto msg = client.readBlocking();

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

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

    // close the connection
    client.close();
  } 
}

This is my code and I want to listen multiple message from a Single client, but when I receive a message then It stops receiving messages and client.available() returning 0 after that, I have tried putting client.poll(); in loop but It's not working.

@kevin-guzman
Copy link

Can you solve the problem? I have the same issue.

@jaydip-pawar
Copy link
Author

Can you solve the problem? I have the same issue.

No, but For now I am handling it from App side, like when I send message then I am disconnecting client from App and then again connecting it to send another message.

@gilmaimon
Copy link
Owner

Hi, I didn't really get the scenario here..

You want to have a server that accepts a client and reads multiple messages? Because your code only reads one message and then closes the connection... Please try to better explain the use case and the bug (if you think this is a bug).

@jaydip-pawar
Copy link
Author

Hi, I didn't really get the scenario here..

You want to have a server that accepts a client and reads multiple messages? Because your code only reads one message and then closes the connection... Please try to better explain the use case and the bug (if you think this is a bug).

Yes, I want a server that accepts multiple messages until client is connected, But now When client connects to the server and sends a message after that if same client send another message it not listen by server, That's all I want is a server that accepts multiple messages.

@gilmaimon
Copy link
Owner

Hi, I didn't really get the scenario here..
You want to have a server that accepts a client and reads multiple messages? Because your code only reads one message and then closes the connection... Please try to better explain the use case and the bug (if you think this is a bug).

Yes, I want a server that accepts multiple messages until client is connected, But now When client connects to the server and sends a message after that if same client send another message it not listen by server, That's all I want is a server that accepts multiple messages.

Sorry for being rude, but you have to know programming for working with this library... You probably don't expect me to write your code for you? You have most of the API documented in the Wiki and multiple simple and more complex examples as well as a README with examples from which you can understand the basics of how to work with clients and such... In the Wiki there is even an advanced example of working with multiple clients simultaneously.

Meanwhile I will label the issue accordingly, maybe someone with some free time will write you an example specific to your case...

Gil.

@gilmaimon gilmaimon added good first issue Good for newcomers help wanted Extra attention is needed usage queshtion Queshtions about using the library, its interface and so on labels Oct 16, 2021
@jaydip-pawar
Copy link
Author

Hi, I didn't really get the scenario here..
You want to have a server that accepts a client and reads multiple messages? Because your code only reads one message and then closes the connection... Please try to better explain the use case and the bug (if you think this is a bug).

Yes, I want a server that accepts multiple messages until client is connected, But now When client connects to the server and sends a message after that if same client send another message it not listen by server, That's all I want is a server that accepts multiple messages.

Sorry for being rude, but you have to know programming for working with this library... You probably don't expect me to write your code for you? You have most of the API documented in the Wiki and multiple simple and more complex examples as well as a README with examples from which you can understand the basics of how to work with clients and such... In the Wiki there is even an advanced example of working with multiple clients simultaneously.

Meanwhile I will label the issue accordingly, maybe someone with some free time will write you an example specific to your case...

Gil.

I'm not saying to write any code for me, but I think you have not understood my question. I have checked wiki and your examples as well, but I am not saying to work with multiple clients, as your example your code listen to message and when it receives the message it close the connection and again steps repeat like another client connect and send the message, I have only one client but my client is not able to send multiple message that's what I am saying. In my code, client is connecting with server just one time, but If I refer your code, Then I need to connect my client again and again to server for sending multiple messages. My question is simple that I have to receive multiple messages from same client without repeating the client-server connection process. If you understand my question then just tell me what I am missing. Thank you

@adelin-mcbsoft
Copy link
Contributor

@jaydip-pawar , you're calling client.close(); in the body of the loop(), it's pretty obvious that at your next iteration you won't receive any else message as you close the connection...
Remove that line and you'll be able to receive as many messages as you'd like, as long as the client is still connected.
Call the .close() method only when it's appropriate.

@gilmaimon was right, you need to understand these basic aspects before raising any issue, it's about the code logic here, not the library itself. What you have there is an example, you have to adapt it based on your own needs :)
Good luck.

@jaydip-pawar
Copy link
Author

@jaydip-pawar , you're calling client.close(); in the body of the loop(), it's pretty obvious that at your next iteration you won't receive any else message as you close the connection... Remove that line and you'll be able to receive as many messages as you'd like, as long as the client is still connected. Call the .close() method only when it's appropriate.

@gilmaimon was right, you need to understand these basic aspects before raising any issue, it's about the code logic here, not the library itself. What you have there is an example, you have to adapt it based on your own needs :) Good luck.

Sorry, I thought I mentioned it in first question, But I tried with removing client.close() but still It's not working, That's why I asked.

@Sampozzo
Copy link

The same here.
Using the server example here: https://github.com/gilmaimon/ArduinoWebsockets/blob/master/examples/Esp32-Server/Esp32-Server.ino and removing the client.close(); at the end do not solve the problem.

You can connect to the server, but when you send the first message, the server reply and then close the connection.

@Sampozzo
Copy link

@jaydip-pawar have you solved the issue?

@jaydip-pawar
Copy link
Author

Not yet, I also tried other libraries for it but in my case I face many bugs so I tried with Bluetooth for transferring data and it's work fine with my code, So if you just want to transfer the data then try bluetooth instead of WiFi.

@Sampozzo
Copy link

Sampozzo commented Dec 26, 2021

I think that they must provide some working example, instead of asking to study the Basic programming concepts.

@gilmaimon
Copy link
Owner

I think that they must provide some working example, instead of asking to study the Basic programming concepts.

Who are these "they" you mentioned 🤔

Your question has been answered, if you don't like the examples you can use another library, that will surely fix your problems.

@Sampozzo
Copy link

Where it was answered?

@Sampozzo
Copy link

Sampozzo commented Dec 26, 2021

Can you simply explain why the server close the connection even if you remove the client.close() line?

@jaydip-pawar
Copy link
Author

I think that they must provide some working example, instead of asking to study the Basic programming concepts.

Who are these "they" you mentioned 🤔

Your question has been answered, if you don't like the examples you can use another library, that will surely fix your problems.

Sorry for being rude but without answering to the answer and just closing the issue just because some arrogance is not good, rather than just arguing just answer it, simple! We use libraries doesn't mean we haven't any idea about programming just to save time we use it, but if someone ask questions or queries, rather than arguing give answer to them and improve your library by fixing any issue if there.

@gilmaimon
Copy link
Owner

Obviously the client instance is going out of scope so you can't interact with it anymore, so why would the connection stay open? The destructor is called and the connection is closed. If you want to keep interacting with the client you must keep interacting with the client object. If you let it go out of scope the connection will be closed (like any other RAII class).

@gilmaimon
Copy link
Owner

I think that they must provide some working example, instead of asking to study the Basic programming concepts.

Who are these "they" you mentioned 🤔
Your question has been answered, if you don't like the examples you can use another library, that will surely fix your problems.

Sorry for being rude but without answering to the answer and just closing the issue just because some arrogance is not good...

"Apology" not accepted...

@jaydip-pawar
Copy link
Author

Obviously the client instance is going out of scope so you can't interact with it anymore, so why would the connection stay open? The destructor is called and the connection is closed. If you want to keep interacting with the client you must keep interacting with the client object. If you let it go out of scope the connection will be closed (like any other RAII class).

Right! But if object is declared globally and destructor is not called then why it's not interacting with client? Just after completing one loop client is disconnecting without calling client.close()

@gilmaimon
Copy link
Owner

Obviously the client instance is going out of scope so you can't interact with it anymore, so why would the connection stay open? The destructor is called and the connection is closed. If you want to keep interacting with the client you must keep interacting with the client object. If you let it go out of scope the connection will be closed (like any other RAII class).

Right! But if object is declared globally and destructor is not called then why it's not interacting with client? Just after completing one loop client is disconnecting without calling client.close()

That's the point, it's not declared globally. It's declared inside the function loop. And when the function finishes, the client is destructed (and thus, closed)

@jaydip-pawar
Copy link
Author

Obviously the client instance is going out of scope so you can't interact with it anymore, so why would the connection stay open? The destructor is called and the connection is closed. If you want to keep interacting with the client you must keep interacting with the client object. If you let it go out of scope the connection will be closed (like any other RAII class).

Right! But if object is declared globally and destructor is not called then why it's not interacting with client? Just after completing one loop client is disconnecting without calling client.close()

That's the point, it's not declared globally. It's declared inside the function loop. And when the function finishes, the client is destructed (and thus, closed)

But I have tried it by declaring globally and it won't work, why?

@gilmaimon
Copy link
Owner

Without seeing the code I have no idea, but I assume you kept the loop code exactly as is so your client keep getting re-initialized (so the client keeps getting closed)

These questions are symptoms of lacking experience in cpp and programming in general, which is why I think you should focus on those subjects before doing embedded and networking. You can be offended by this if you want, it's not arrogance. Even if I'll help you with your code line by line, you would still encounter other issues down the road, that has nothing to do with the library. I see that I already said that 2 months ago, if you would have invested the time in it, you could answer all of your questions yourself.

@jaydip-pawar
Copy link
Author

Without seeing the code I have no idea, but I assume you kept the loop code exactly as is so your client keep getting re-initialized (so the client keeps getting closed)

These questions are symptoms of lacking experience in cpp and programming in general, which is why I think you should focus on those subjects before doing embedded and networking. You can be offended by this if you want, it's not arrogance. Even if I'll help you with your code line by line, you would still encounter other issues down the road, that has nothing to do with the library. I see that I already said that 2 months ago, if you would have invested the time in it, you could answer all of your questions yourself.

Sorry but i don't think adding auto client = server.accept(); in setup re-initialise object with every loop iteration, and I would have been happy, If you had told me this earlier, I would have shown you my code and we would have reached to the conclusion. But no problem I have that much knowledge to find solutions and I found better solution over it, later but ok thanks for your valuable answers, it wouldn't help me but if someone encounter this issue It would be great if he could get his solution as you said if I'm wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed usage queshtion Queshtions about using the library, its interface and so on
Projects
None yet
Development

No branches or pull requests

5 participants