diff --git a/examples/WebSocketsServer/WebSocketsServer.ino b/examples/WebSocketsServer/WebSocketsServer.ino index a4d4848..5936d4b 100644 --- a/examples/WebSocketsServer/WebSocketsServer.ino +++ b/examples/WebSocketsServer/WebSocketsServer.ino @@ -20,13 +20,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght switch(type) { case WStype_DISCONNECTED: - Serial1.printf("[%d] Disconnected!\n", num); + Serial1.printf("[%u] Disconnected!\n", num); break; 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; 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 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); break; case WStype_BIN: - Serial1.printf("[%d] get binary.\n", num); + Serial1.printf("[%u] get binary lenght: %u\n", num, lenght); hexdump(payload, lenght); // echo data back to browser diff --git a/src/WebSocketsServer.cpp b/src/WebSocketsServer.cpp index 4624b48..7045fa2 100644 --- a/src/WebSocketsServer.cpp +++ b/src/WebSocketsServer.cpp @@ -200,7 +200,7 @@ void WebSocketsServer::disconnect(void) { /** * disconnect one client - * @param num + * @param num uint8_t client id */ void WebSocketsServer::disconnect(uint8_t num) { 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(); +} + //################################################################################# //################################################################################# //################################################################################# diff --git a/src/WebSocketsServer.h b/src/WebSocketsServer.h index 1132686..b24afb4 100644 --- a/src/WebSocketsServer.h +++ b/src/WebSocketsServer.h @@ -86,6 +86,8 @@ public: void disconnect(void); void disconnect(uint8_t num); + IPAddress remoteIP(uint8_t num); + private: uint16_t _port;