Skip to content

Commit 2798232

Browse files
committed
add IPAddress remoteIP(uint8_t num)
update example
1 parent 44c00d2 commit 2798232

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

examples/WebSocketsServer/WebSocketsServer.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
2020

2121
switch(type) {
2222
case WStype_DISCONNECTED:
23-
Serial1.printf("[%d] Disconnected!\n", num);
23+
Serial1.printf("[%u] Disconnected!\n", num);
2424
break;
2525
case WStype_CONNECTED:
26-
Serial1.printf("[%d] Connected.\n", num);
26+
{
27+
IPAddress ip = webSocket.remoteIP(num);
28+
Serial1.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
29+
}
2730
break;
2831
case WStype_TEXT:
29-
Serial1.printf("[%d] get Text: %s\n", num, payload);
32+
Serial1.printf("[%u] get Text: %s\n", num, payload);
3033

3134
// echo data back to browser
3235
webSocket.sendTXT(num, payload, lenght);
@@ -35,7 +38,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
3538
webSocket.broadcastTXT(payload, lenght);
3639
break;
3740
case WStype_BIN:
38-
Serial1.printf("[%d] get binary.\n", num);
41+
Serial1.printf("[%u] get binary lenght: %u\n", num, lenght);
3942
hexdump(payload, lenght);
4043

4144
// echo data back to browser

src/WebSocketsServer.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void WebSocketsServer::disconnect(void) {
200200

201201
/**
202202
* disconnect one client
203-
* @param num
203+
* @param num uint8_t client id
204204
*/
205205
void WebSocketsServer::disconnect(uint8_t num) {
206206
if(num >= WEBSOCKETS_SERVER_CLIENT_MAX) {
@@ -212,6 +212,22 @@ void WebSocketsServer::disconnect(uint8_t num) {
212212
}
213213
}
214214

215+
/**
216+
* get an IP for a client
217+
* @param num uint8_t client id
218+
* @return IPAddress
219+
*/
220+
IPAddress WebSocketsServer::remoteIP(uint8_t num) {
221+
if(num < WEBSOCKETS_SERVER_CLIENT_MAX) {
222+
WSclient_t * client = &_clients[num];
223+
if(clientIsConnected(client)) {
224+
return client->tcp.remoteIP();
225+
}
226+
}
227+
228+
return IPAddress();
229+
}
230+
215231
//#################################################################################
216232
//#################################################################################
217233
//#################################################################################

src/WebSocketsServer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class WebSocketsServer: private WebSockets {
8686
void disconnect(void);
8787
void disconnect(uint8_t num);
8888

89+
IPAddress remoteIP(uint8_t num);
90+
8991
private:
9092
uint16_t _port;
9193

0 commit comments

Comments
 (0)