Git Product home page Git Product logo

Comments (16)

gilmaimon avatar gilmaimon commented on May 24, 2024

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.

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

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

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 24, 2024

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)

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

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

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 24, 2024

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.

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

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.

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 24, 2024

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.

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

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

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 24, 2024

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.

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

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

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 24, 2024

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

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

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

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

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

from arduinowebsockets.

denden4444 avatar denden4444 commented on May 24, 2024

@gil

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

Thank you kindly
Den

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 24, 2024

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.

from arduinowebsockets.

gilmaimon avatar gilmaimon commented on May 24, 2024

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.

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.