mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-12 15:06:30 +02:00
add IPAddress remoteIP(uint8_t num)
update example
This commit is contained in:
@ -20,13 +20,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
|
|||||||
|
|
||||||
switch(type) {
|
switch(type) {
|
||||||
case WStype_DISCONNECTED:
|
case WStype_DISCONNECTED:
|
||||||
Serial1.printf("[%d] Disconnected!\n", num);
|
Serial1.printf("[%u] Disconnected!\n", num);
|
||||||
break;
|
break;
|
||||||
case WStype_CONNECTED:
|
case WStype_CONNECTED:
|
||||||
Serial1.printf("[%d] Connected.\n", num);
|
{
|
||||||
|
IPAddress ip = webSocket.remoteIP(num);
|
||||||
|
Serial1.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case WStype_TEXT:
|
case WStype_TEXT:
|
||||||
Serial1.printf("[%d] get Text: %s\n", num, payload);
|
Serial1.printf("[%u] get Text: %s\n", num, payload);
|
||||||
|
|
||||||
// echo data back to browser
|
// echo data back to browser
|
||||||
webSocket.sendTXT(num, payload, lenght);
|
webSocket.sendTXT(num, payload, lenght);
|
||||||
@ -35,7 +38,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
|
|||||||
webSocket.broadcastTXT(payload, lenght);
|
webSocket.broadcastTXT(payload, lenght);
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case WStype_BIN:
|
||||||
Serial1.printf("[%d] get binary.\n", num);
|
Serial1.printf("[%u] get binary lenght: %u\n", num, lenght);
|
||||||
hexdump(payload, lenght);
|
hexdump(payload, lenght);
|
||||||
|
|
||||||
// echo data back to browser
|
// echo data back to browser
|
||||||
|
@ -200,7 +200,7 @@ void WebSocketsServer::disconnect(void) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* disconnect one client
|
* disconnect one client
|
||||||
* @param num
|
* @param num uint8_t client id
|
||||||
*/
|
*/
|
||||||
void WebSocketsServer::disconnect(uint8_t num) {
|
void WebSocketsServer::disconnect(uint8_t num) {
|
||||||
if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
|
if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
|
||||||
@ -212,6 +212,22 @@ void WebSocketsServer::disconnect(uint8_t num) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get an IP for a client
|
||||||
|
* @param num uint8_t client id
|
||||||
|
* @return IPAddress
|
||||||
|
*/
|
||||||
|
IPAddress WebSocketsServer::remoteIP(uint8_t num) {
|
||||||
|
if(num < WEBSOCKETS_SERVER_CLIENT_MAX) {
|
||||||
|
WSclient_t * client = &_clients[num];
|
||||||
|
if(clientIsConnected(client)) {
|
||||||
|
return client->tcp.remoteIP();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return IPAddress();
|
||||||
|
}
|
||||||
|
|
||||||
//#################################################################################
|
//#################################################################################
|
||||||
//#################################################################################
|
//#################################################################################
|
||||||
//#################################################################################
|
//#################################################################################
|
||||||
|
@ -86,6 +86,8 @@ public:
|
|||||||
void disconnect(void);
|
void disconnect(void);
|
||||||
void disconnect(uint8_t num);
|
void disconnect(uint8_t num);
|
||||||
|
|
||||||
|
IPAddress remoteIP(uint8_t num);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t _port;
|
uint16_t _port;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user