Skip to content

Commit 2b04572

Browse files
committed
WiFiServer operator bool
1 parent 12c33f1 commit 2b04572

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

docs/api.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,6 +2764,63 @@ void loop() {
27642764
27652765
```
27662766

2767+
### `if(server)`
2768+
2769+
#### Description
2770+
Indicates whether the server is listening for new clients. You can use this to detect whether server.begin() was successful.
2771+
2772+
2773+
#### Syntax
2774+
2775+
```
2776+
if (server)
2777+
if (!server)
2778+
2779+
```
2780+
2781+
#### Parameters
2782+
none
2783+
2784+
#### Returns
2785+
- whether the server is listening for new clients (bool).
2786+
2787+
#### Example
2788+
2789+
```
2790+
#include <WiFiNINA.h>
2791+
2792+
char ssid[] = "Network"; // your network SSID (name)
2793+
char pass[] = "myPassword"; // your network password
2794+
2795+
WiFiServer server(23);
2796+
2797+
void setup() {
2798+
2799+
Serial.begin(115200);
2800+
while (!Serial) {}
2801+
2802+
int status = WiFi.begin(ssid, pass);
2803+
if ( status != WL_CONNECTED) {
2804+
Serial.println("Couldn't get a WiFi connection");
2805+
while(true);
2806+
}
2807+
2808+
server.begin();
2809+
if (!server) {
2810+
Serial.println("Server failed to start.");
2811+
while(true);
2812+
}
2813+
}
2814+
2815+
void loop() {
2816+
WiFiClient client = server.available();
2817+
if (client) {
2818+
String s = client.readStringUntil('\n');
2819+
server.println(s);
2820+
}
2821+
}
2822+
```
2823+
27672824
### `server.status()`
27682825

27692826
#### Description

src/WiFiServer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ uint8_t WiFiServer::status() {
8888
}
8989
}
9090

91+
WiFiServer::operator bool() {
92+
return (_sock != NO_SOCKET_AVAIL);
93+
}
9194

9295
size_t WiFiServer::write(uint8_t b)
9396
{

src/WiFiServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class WiFiServer : public Server {
4242
virtual size_t write(uint8_t);
4343
virtual size_t write(const uint8_t *buf, size_t size);
4444
uint8_t status();
45+
explicit operator bool();
4546

4647
using Print::write;
4748
};

0 commit comments

Comments
 (0)