Comments (7)
Hi Jim!
I agree! Other libraries seemed complex to me as well. And complexity is always a bad thing, not only for noobs :)
You need to think of a way to manage multiple clients. You can have a vector or list of connected clients.
For each loop, or on some fixed interval you can remove from the list clients that were disconnected (use client.available()
to see if the client is still connected).
You can also ping the clients on fixed intervals in order to make sure they are still alive and that the TCP connection is really ok.
Then, in order to send an int to the client you can go the easy way, which is to convert it to a string. You can also convert an int pointer to byte array and send it using sendBinary
. I'm currently on mobile, but let me know if want some code samples.
Gil.
from arduinowebsockets.
from arduinowebsockets.
I'm glad to help Jim :)
What happens here is that WebsocketsClient
will call close when it goes out of scope. Meaning once you lost the variable (when loop
ends) you loose the "handle" to that object, and because I don't want the connection to get lost and the resources to stay open, the connection will be closed even if you don't call close
explicitly.
To solve this, you need to use some sort of collection to store all your ongoing connections. In the code below I used a vector.
This code will work fine for you:
/*
Minimal Esp32 Websockets Server
This sketch:
1. Connects to a WiFi network
2. Starts a websocket server on port 80
3. Waits for connections
4. Once a client connects, it wait for a message from the client
5. Sends an "echo" message to the client
6. closes the connection and goes back to step 3
Hardware:
For this sketch you only need an ESP32 board.
Created 15/02/2019
By Gil Maimon
https://github.com/gilmaimon/ArduinoWebsockets
*/
#include <ArduinoWebsockets.h>
#include <WiFi.h>
const char* ssid = "GEE"; //Enter SSID
const char* password = "catface1!"; //Enter Password
int print_type_int = 0;
String print_type_string = "";
using namespace websockets;
WebsocketsServer server;
std::vector<WebsocketsClient> connectedClients;
void setup() {
Serial.begin(115200);
// Connect to wifi
WiFi.begin(ssid, password);
// Wait some time to connect to wifi
for(int i = 0; i < 15 && WiFi.status() != WL_CONNECTED; i++) {
Serial.print(".");
delay(1000);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP
server.listen(80);
Serial.print("Is server live? ");
Serial.println(server.available());
}
void loop() {
// First, lets see if there are clients which wants to connect to our server
while (server.poll()) {
// If we are here, there is another client willing to connect!
// Lets accept the connection, and add it to the list of clients
WebsocketsClient client = server.accept();
// Lets keep a refernce to the client in our collection, so We won't loose it and close the connection early
connectedClients.push_back(client);
}
// Now, lets make sure that all the clients that are in our vector are actually still connected
std::vector<WebsocketsClient> actuallyConnectedClients;
// For every client in connectedClients:
for(const WebsocketsClient& client : connectedClients) {
// If the client is still connected, add it to the new list
if (client.available()) {
actuallyConnectedClients.push_back(client);
}
}
// Now we are pretty sure that everyone in connectedClients are actually connected
connectedClients = actuallyConnectedClients;
// Lets update the int value, Read new value for int "print_type:
print_type_int = random(2,4); // Will return either "2" or "3"
Serial.print("print_type_int: "); Serial.println(print_type_int);
// Convert print_type_int to a String:
print_type_string = print_type_int;
Serial.print("print_type_string: "); Serial.println(print_type_string);
// Last, we send the new value to all the connected clients
for(const WebsocketsClient& client : connectedClients) {
// If the client is still connected, add it to the new list
client.send(print_type_string);
}
// Sleep until next iteration
Serial.print("Num of connected clients: "); Serial.println(connectedClients.size());
delay(1000);
}
What I do here:
- First I accept anyone willing to connect (if there are any) and add them to the list of clients
- I make sure everyone in the list of clients that are still connected, and I keep only the ones connected
- I use your code to calculate the new random value
- I send it to all the clients
- Sleep, and repeat ♻️
I've added detailed comments, make sure you read them and understand what's happening. I made them very detailed because I don't know exactly what your level is, and I want this to be useful to anyone in the future. I'll be glad to further explain anything you wish.
I already drink way too much coffee 😄 The best way to contribute is to use the library and have fun with it! (and starring the project, will also help others to notice it)
Gil.
from arduinowebsockets.
Hi Gil...
Your code on line 92 of the new sketch which is:
client.send(print_type_string);
Gives the following error on compiling:
passing 'const websockets::WebsocketsClient' as 'this' argument discards qualifiers [-fpermissive]
I tried to research this error to see if I could fix it, but it looks like I would have to change something about the const usage in the "Arduino/libraries/ArduinoWebsockets/src/tiny_websockets/internals/websockets_endpoint.hpp " file, which is way beyond my knowledge yet.
I also tried making the "print_type_string" a const and passing that to the client.send(print_type_string);
function in the sketch, but that didn't change anything.
Can you understand why this error is happening?
Thank you so much!!!
--
Jim
from arduinowebsockets.
Hi Gil...
Your code on line 92 of the new sketch which is:
client.send(print_type_string);
Gives the following error on compiling:
passing 'const websockets::WebsocketsClient' as 'this' argument discards qualifiers [-fpermissive]
I tried to research this error to see if I could fix it, but it looks like I would have to change something about the const usage in the "Arduino/libraries/ArduinoWebsockets/src/tiny_websockets/internals/websockets_endpoint.hpp " file, which is way beyond my knowledge yet.
I also tried making the "print_type_string" a const and passing that to the
client.send(print_type_string);
function in the sketch, but that didn't change anything.Can you understand why this error is happening?
Thank you so much!!!
--
Jim
Sorry mate! I uploaded an older version of the code.
This should work:
#include <ArduinoWebsockets.h>
#include <WiFi.h>
const char* ssid = "GEE"; //Enter SSID
const char* password = "catface1!"; //Enter Password
int print_type_int = 0;
String print_type_string = "";
using namespace websockets;
WebsocketsServer server;
std::vector<WebsocketsClient> connectedClients;
void setup() {
Serial.begin(115200);
// Connect to wifi
WiFi.begin(ssid, password);
// Wait some time to connect to wifi
for(int i = 0; i < 15 && WiFi.status() != WL_CONNECTED; i++) {
Serial.print(".");
delay(1000);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP
server.listen(80);
Serial.print("Is server live? ");
Serial.println(server.available());
}
void loop() {
// First, lets see if there are clients which wants to connect to our server
while (server.poll()) {
// If we are here, there is another client willing to connect!
// Lets accept the connection, and add it to the list of clients
WebsocketsClient client = server.accept();
// Lets keep a refernce to the client in our collection, so We won't loose it and close the connection early
connectedClients.push_back(client);
}
// Now, lets make sure that all the clients that are in our vector are actually still connected
std::vector<WebsocketsClient> actuallyConnectedClients;
// For every client in connectedClients:
for(WebsocketsClient& client : connectedClients) {
// If the client is still connected, add it to the new list
if (client.available()) {
actuallyConnectedClients.push_back(client);
}
}
// Now we are pretty sure that everyone in connectedClients are actually connected
connectedClients = actuallyConnectedClients;
// Lets update the int value, Read new value for int "print_type:
print_type_int = random(2,4); // Will return either "2" or "3"
Serial.print("print_type_int: "); Serial.println(print_type_int);
// Convert print_type_int to a String:
print_type_string = print_type_int;
Serial.print("print_type_string: "); Serial.println(print_type_string);
// Last, we send the new value to all the connected clients
for(WebsocketsClient& client : connectedClients) {
// If the client is still connected, add it to the new list
client.send(print_type_string);
}
// Sleep until next iteration
Serial.print("Num of connected clients: "); Serial.println(connectedClients.size());
delay(1000);
}
Gil 😄
from arduinowebsockets.
Hi Gil...
YAY!!! It works great! Thank you SOOO much!
--
Jim
from arduinowebsockets.
😄
from arduinowebsockets.
Related Issues (20)
- 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
- Issue with WSS and ESP 32 HOT 4
- Esp32 WebSocket Client won't work with WebSocket Node.js server. HOT 3
- Client-Message-Buffer
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.