Good morning, I am testing with this example of the course, and I am trying to index.html, (I have it in SPIFF) I want to show the connected clients and the IP of these in index.html.
For console is correct but in index no:
Serial.printf(“WebSocket client #%u connected from %s\n”, client->id(), client->remoteIP().toString().c_str());
Regards from Spain.
Hi Javier.
What do you have done so far to save the client’s IP in the web page?
You can send a message via websocket with the client’s IP, every time a new client connects. Then, you need to handle that on the JavaScript file and place the IP on a place in your HTML file.
Regards,
Sara
Hi sara, this is the problem, place the IP on a place in your HTML file.
The IP and number of clients
�SPIFFS mounted successfully
Connecting to WiFi …..192.168.1.210
HTTP server started
WebSocket client #1 connected from 192.168.1.59
WebSocket client #2 connected from 192.168.1.44
WebSocket client #2 disconnected
I have this in:
client->id(), client->remoteIP().toString().c_str()
No?
Regards
Hi.
But, how are you sending that information to the client (the HTML page)?
What have you tried so far?
Regards,
Sara
OK, explain to you, part of the code is yours.
In index.html:
<p> ID e IP Cliente conectado: <span id=”clientein”>%CLIENTE%</span></p>
Script:
setInterval(updateValues, 10000, “clientein”);
function updateValues(value) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById(value).innerHTML = this.responseText;
}
};
xhttp.open(“GET”, “/” + value, true);
xhttp.send();
}
I made a variable:
const char *clienteipin = “ID cliente a mostrar mas IP”;
ESP32:
// MI PRUEBA
server.on(“/clientein”, HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send_P(200, “text/plain”, clienteipin); });
I need put in variable clienteipin de ID and IP the new conection in the web server:
SAME this:
case WS_EVT_CONNECT:
Serial.printf(“WebSocket client #%u connected from %s\n”, client->id(), client->remoteIP().toString().c_str());
Thanks and regards.
I made a change:
Script:
setInterval(updateValues, 10000, “clientein”);
function updateValues(value) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById(value).innerHTML = this.responseText;
}
};
xhttp.open(“GET”, “/” + value, true);
xhttp.send();
}
This is, no for time, I nedd with same new id connect:
function onOpen(event) {
console.log(‘Connection opened’);
websocket.send(updateValues(“clientein”));
Is it correct?? THANKS
Hi, my pronblem is put this:
client->id()
client->remoteIP()
Both in two variable, for example strings.
I can not or I know how……
Thanks.
Hi.
When the onEvent() function runs, you can save the variables as follows:
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type,
void *arg, uint8_t *data, size_t len) {
uint32_t id = client->id();
String remoteIp = client->remoteIP().toString();
The id is a uint32_t variable, and the remoteIp gives the Ip of the client as a String.
I hope this helps.
Regards,
Sara
Hi Sara, when you explain it, now I see it clearly. I was stuck looking for more complicated options. THANKS THAT WAS.
Hi, this doesn’t work for me
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type,
void *arg, uint8_t *data, size_t len)
{
uint32_t idp = client->id();
String ipc = client->remoteIP().toString();
Serial.printf(ipc);
The fault in serial.printf(ipc) is:
there is no suitable “String” to “const char *” conversion function
And in uint32_t idp:
unused variable ‘idp’ [-Wunused-variable]
Thanks.
Hi, if I dreclare the variable inside of onEvent:
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type,
void *arg, uint8_t *data, size_t len)
{
uint32_t idp = client->id();
String ipc = client->remoteIP().toString();
For other parts of the skecht is imposible access to it, for example:
server.on(“/clientein”, HTTP_GET, [](AsyncWebServerRequest *request)
{
request->send(200, “text/plain”, ipc);
});
For IPC the VS give to me “NO defined”
Regards.
Hi, the solution is that the variable is declared in GLOBAL, but for my is not possible or I dont Know the opcion.
Help…
If I declared the variable here, t e request IS PERFECT.
But in this part it is not possible for mi, its…
uint32_t idp = client->id();
String ipc = client->remoteIP().toString();
Always client is:
el identificador “client” no está definido
Thanks and regards.
Hi.
You should declare the variables inside the onEvent function.
A workaround to use those variables globally is to create two more auxiliary variables globally, that then, will have the value of the local variables.
For example:
declare these variables outside setup():
uint32_t idp_global; String ipc_global;
Then, inside the onEvent function:
uint32_t idp = client->id(); //local variable
String ipc = client->remoteIP().toString(); //loval variable idp_global = idp; // this is a global variable, you can use it thoughout your code ipc_global = ipc; // this is a global variable, you can use it thoughout your code
I’m sure there must be a more “professional” and efficient way to do this. However, this is an easy trick that should work.
Regards,
Sara