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

webSockets client and server on same ESP ? #35

Closed
denden4444 opened this issue Oct 5, 2019 · 16 comments
Closed

webSockets client and server on same ESP ? #35

denden4444 opened this issue Oct 5, 2019 · 16 comments
Labels
good first issue Good for newcomers usage queshtion Queshtions about using the library, its interface and so on

Comments

@denden4444
Copy link

Hello Gil

This looks like an awesome alternative to other libraries, and I'm quite excited to try it ;-)

I just need to know the following please :

  1. I need to have the server and client on the same ESP. is this possible and if so could you kindly explain how or provide an example ?
    Please see attached pic for reference.

  2. I would like to enable to disable the connection(client and/or server) when desired/required, independently of eachother.
    For example (pseudo code) :
    client enable
    do something
    client disable
    and same as above for server

  3. Are you able to call (and control - disable/enable) client and/or server from anywhere other than the main loop ?

Really looking forward to your reply

Kind regards

Den

websockets client and server in one

@gilmaimon
Copy link
Owner

Hi,

This looks like an awesome alternative to other libraries, and I'm quite excited to try it ;-)

Thanks! This was my exact intent when creating this library.

  1. It should be possible. There is no limitation as far as the library is concerned. I'm pretty sure it will work. You can have a look at the Wiki for API Refs, it should help you use some advanced features of the library. The Wiki is not related to Arduino, but you can use the examples in it and adjust to Arduino setup-loop kind of code.
    Basically the important thing to notice is that no method in this library will block if you will use the .poll() method (on both client and server).

  2. You could close the client and server (.close) when in disabled mode, and create them again when going to enabled mode. Or you could just ignore connections (not calling .accept) when disabled.

  3. Yup, you can. loop and setup don't really have any special properties as far as I know. They are just called by some-one else's main.

I'd love to help more, good luck!
Gil.

@gilmaimon gilmaimon added good first issue Good for newcomers usage queshtion Queshtions about using the library, its interface and so on labels Oct 5, 2019
@denden4444
Copy link
Author

denden4444 commented Oct 5, 2019

Hi Gil

Thanks a million for the response .. it is truly mega appreciated ;-)

Thanks! This was my exact intent when creating this library.

YAY ;-) .. I have been on a mission since last year trying to find a websockects library or implementation that is more controllable and versatile without compromising speed and program space.

I will load each of these on an ESP8266 and then test client and server , once both are working I will put both on same and ping you back with feedback.

Thanks for the 'love to help more'too, and if it's ok with you I'm sure I'll be sending more questions as the days go by and I'm happy to test or help you as well with the project.

Kind regards

Den

@gilmaimon
Copy link
Owner

gilmaimon commented Oct 6, 2019

For sure, it's a pleasure.

I can't promise maximum efficiency. Iv'e worked hard for this library to be easy to use and user-friendly, but that means I do make some compromises regarding efficiency. That being said, I did try to optimize most operations, so hopefully this library will be good enough for you needs!

Have a great day and good luck,
Gil.

(I'm keeping this issue open in case you will have any follow-ups)

@denden4444
Copy link
Author

denden4444 commented Oct 7, 2019

Hi Gil

Just a quick update .. I have success so far with both server and client on same .. still need to perform further testing but its working so far.
What seems to and be a little confusing is this statement for accepting new connections at the server :
WebsocketsClient client = server.accept(); // handle client as described before :)
What exactly is the above doing ?

Could it not be more server related in terms of the names e.g :
WebsocketsClient **SERVER** = server.accept(); // handle client as described before :)
or
WebsocketsServer **SERVER** = server.accept(); // handle client as described before :)

It's probably a silly question but it's going to be a little difficult to follow the code in my instance where I'm running server and client on same ESP8266.

I look forward to your reply as always.

Den

@gilmaimon
Copy link
Owner

Hi Den,

There is documentation and example for almost everything you ask about in the Wiki.
Please spend the time and effort to lookup methods in the docs, study and experiment with the examples. It will greatly help you, and will be more efficient than opening an issue for each API method.

Also, lets keep all discussion here unless it is a bug report or issue with the library.

Gil.

@denden4444
Copy link
Author

denden4444 commented Oct 8, 2019

Hi Gil

Thanks for the replies, much appreciated.
I actually was going through the documentation and wiki.
Sadly not being a C++ programmer and only being an electronics hobbyist the code is rather challenging to follow for me.
I am constantly reading up and trying to get up to speed as well as understand how things are working.
The documentation in the wiki shows wexamples with tinywebsockets and this is now arduino websockets not so ?

Is there a Q&A forum ?

I had read the multiple clients section as well as the poll options.
I have the following in the main loop now and it's not blocking anymore :

//another websockets server
 if(wsserver.poll()){
      Serial.println("New Connection");
      client.close();
      client = wsserver.accept();
      client.onMessage([](WebsocketsClient& client, WebsocketsMessage msg) {
        
        Serial.printf("Message T%d B%d Pi%d Po%d C%d stream%d length: %u\n", msg.isText(), msg.isBinary(), msg.isPing(), msg.isPong(), msg.isClose(),msg.isPartial(), msg.data().length());

         if (msg.data() == "#") {
      Serial.print ("I got a hash #");
      //get client to connect
      wsclient_connect();
       }
      });
      
       
    }
//end another websockets server

I am able to make a websocket connection from PC to the server(ESP) but no sent data appears ..did i miss something ? or mess seomthing up ?

Thanks again for the replies and I will keep questions limited to this thread and will double check documentation first.

@gilmaimon
Copy link
Owner

gilmaimon commented Oct 8, 2019

Hi Den,

The docs are indeed for TinyWebsockets which is the source project for this project. The two code-bases are the same except for few files and file-structure differences between an Arduino library and a CMake library. Everything that is written there is applicable here as well.

Is there a Q&A forum ?

Nope, there is not. I never had so many usage questions to be honest. I don't know if a forum is a good idea - currently I'm the only one here maintaining and answering issues so I won't have the time to manage a forum as well.

Because you set a callback, the client need to sometimes check and see if there is anything new (new messages, new events). In order for the client to check those stuff, you must call client.poll() as often as possible. You could do (inside loop):

if (server.poll()) {
  client = server.accept();
  // Do other stuff with the client
}

if (client.available()) {
  client.poll();
}

So at the end of each loop, if there is a client connected it will check for changes, messages, etc.

@denden4444
Copy link
Author

Hi again Gil

I managed to get the client and server working on same ESP.

I have an issue now where the websockets work but I cannot load any html webpage from the ESP SPIFFS using a web browser when the websockets code is in the main loop.

If i remove the websockets code from the main loop and upload the sketch then the web pages load correctly.
Below is the code :

void loop(){

server.handleClient();                      // run the web server

//websockets
  WebsocketsClient client = wsserver.accept();
  if (client.available()) {
    WebsocketsMessage msg = client.readBlocking();

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

    if (msg.data() == "#") {
      Serial.print ("I got a hash #");
    }
    const char *returned_str = msg.data().c_str();
     addAR(msg.data().c_str());//add a new record

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

    // close the connection
    client.close();
  }
  //endof websockets
}//endof void_loop

Am I setting up the websockets incorrectly ?
Is a Delay or something similar needed ?

I hope you can shed some light here.

If you need extra info please let me know.

Kind regards
Den

@gilmaimon
Copy link
Owner

Hi Den,

Notice that the code is blocked until there is a client to accept. Meaning until there is a WS client that tries to connect, the loop won't re-start and server.handleClient() won't be called.

I'm not sure how the webserver code works, but I assume the method should be called frequently.

In order to only accept a client when there is actually a client trying to connect, use wsserver.poll(). If it turns true: go ahead and accept the client. If false, skip the accept code.

I hope this will solve your issue.
Gil.

@denden4444
Copy link
Author

Hey Gil

Thanks for the reply ;-), hope alls well with you.

Notice that the code is blocked until there is a client to accept'

Actually I don't notice that because i have other functions running in void() loop which run and are running in each iteration of the loop whether there is a client or not ;-(
If read.blocking was preventing it then the serial console would be sitting waiting right ?

Would you mind indicating where I should place server.poll , I tried it a few nights ago and was not successfull (I couldn't get a websocket connection)

I'm happy to run the tests with server.poll in place I'm just really unsure of exactly where to put it in and didn't notice an example it in the sketch examples but did see it used in some examples in the issues and the api pages for tinywebsockets.

Thanks again Gil .

Den

@gilmaimon
Copy link
Owner

Add a serial print before server.handleClient(), how often is it called?

@denden4444
Copy link
Author

will do that now
do you want me to do that with code as it is ?
Are you on gitter.im ?

@denden4444
Copy link
Author

Gil
You are correct .. it is blocking !
I changed it becuase I couldnt get server.poll working (see 5 messages above this one) https://github.com/gilmaimon/ArduinoWebsockets/issues/35#issuecomment-539262461
Can't see where I am going wrong in trying to implement server.poll

@denden4444
Copy link
Author

@gil

I think I finally have it working .. ;-) YAY
Just hope I have it correct :-)

Thank you kindly
Den

@gilmaimon
Copy link
Owner

gilmaimon commented Oct 10, 2019

Hi Den!

I'm glad it's working. The link to your comment seems to be broken, can you link it again? poll should work.

what server.accept does, is: "server, please wait until there is a client knocking on the door. when there is a client knocking, let him in and give me it's object".

So, if you call .accept without anyone on the other side, the server will just wait until there is. Which is why your code was blocking.

server.poll is like: "is there anyone knocking on the door?", which is why when it returns true you can call .accept without blocking for a long time.

Best wishes,
Gil.

@gilmaimon
Copy link
Owner

Hi Den!

Seems like this issue is inactive for now, I hope it means that everything is working as expected 😄

I'm closing it for now, if you have any other usage question feel free to open a new issue or send me an email.

Also, I'm considering some sort of chat channel for support. Maybe on discord or slack. I'll just place that sentence here and see if there is anyone interested in that.

Best wishes,
Gil.

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 usage queshtion Queshtions about using the library, its interface and so on
Projects
None yet
Development

No branches or pull requests

2 participants