Comments (16)
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.
-
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). -
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. -
Yup, you can.
loop
andsetup
don't really have any special properties as far as I know. They are just called by some-one else'smain
.
I'd love to help more, good luck!
Gil.
from arduinowebsockets.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Add a serial print before server.handleClient()
, how often is it called?
from arduinowebsockets.
will do that now
do you want me to do that with code as it is ?
Are you on gitter.im ?
from arduinowebsockets.
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.
I think I finally have it working .. ;-) YAY
Just hope I have it correct :-)
Thank you kindly
Den
from arduinowebsockets.
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.
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)
- Websocket Client Became Slower and Unstable After Upgrading ESP32 from 1.0.4 to 1.0.6 HOT 1
- Websocket server memory leak
- Can we get a documentation/wiki? HOT 1
- where to find certificate Eg: echo_org_ssl_ca_cert[] HOT 1
- ESP32-Server Not Working Properly receives one msg and gets disconnected HOT 3
- How to know hostname and IP from client? HOT 1
- esp32 to wss python server
- ESP32 Websocket Client doesn't connect to Node.js Websocket server HOT 3
- poll() disconnects client HOT 7
- How to get a reference of the web socket client that dispatched the msg callback
- Hieromon/AutoConnect & WebSockets Client on ESP8266 - Can't Establish wss
- Secured-ESP8266-Client can't seem to receive any messages while connection is open HOT 1
- Compilation error: 'WebsocketsClient' does not name a type ?? HOT 3
- Getting a error from the TCPServer using the Arduino GIGA R1 WIFI on the ESP-32Client code. HOT 3
- ESP32-CAM WebSocket Server: Failure to Stream Camera Feed after Page Refresh or Multiple Connections
- WSS Connection error
- [ 5236][E][WiFiClient.cpp:313] setSocketOption(): fail on -1, errno: 9, "Bad file number" HOT 2
- Reconnecting after lost connection HOT 2
- Disconnecting almost inmediately
- Random disconnects, fails on binary ping including 0x00 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinowebsockets.