Skip to content

Commit e98adc3

Browse files
authored
Merge pull request #253 from JAndrassy/server_op_bool
WiFiServer operator bool
2 parents ae63eab + 2b04572 commit e98adc3

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

docs/api.md

+57
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,63 @@ void loop() {
28292829
28302830
```
28312831

2832+
### `if(server)`
2833+
2834+
#### Description
2835+
Indicates whether the server is listening for new clients. You can use this to detect whether server.begin() was successful.
2836+
2837+
2838+
#### Syntax
2839+
2840+
```
2841+
if (server)
2842+
if (!server)
2843+
2844+
```
2845+
2846+
#### Parameters
2847+
none
2848+
2849+
#### Returns
2850+
- whether the server is listening for new clients (bool).
2851+
2852+
#### Example
2853+
2854+
```
2855+
#include <WiFiNINA.h>
2856+
2857+
char ssid[] = "Network"; // your network SSID (name)
2858+
char pass[] = "myPassword"; // your network password
2859+
2860+
WiFiServer server(23);
2861+
2862+
void setup() {
2863+
2864+
Serial.begin(115200);
2865+
while (!Serial) {}
2866+
2867+
int status = WiFi.begin(ssid, pass);
2868+
if ( status != WL_CONNECTED) {
2869+
Serial.println("Couldn't get a WiFi connection");
2870+
while(true);
2871+
}
2872+
2873+
server.begin();
2874+
if (!server) {
2875+
Serial.println("Server failed to start.");
2876+
while(true);
2877+
}
2878+
}
2879+
2880+
void loop() {
2881+
WiFiClient client = server.available();
2882+
if (client) {
2883+
String s = client.readStringUntil('\n');
2884+
server.println(s);
2885+
}
2886+
}
2887+
```
2888+
28322889
### `server.status()`
28332890

28342891
#### Description

src/WiFiServer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ uint8_t WiFiServer::status() {
9494
}
9595
}
9696

97+
WiFiServer::operator bool() {
98+
return (_sock != NO_SOCKET_AVAIL);
99+
}
97100

98101
size_t WiFiServer::write(uint8_t b)
99102
{

src/WiFiServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class WiFiServer : public Server {
4343
virtual size_t write(uint8_t);
4444
virtual size_t write(const uint8_t *buf, size_t size);
4545
uint8_t status();
46+
explicit operator bool();
4647

4748
using Print::write;
4849
};

0 commit comments

Comments
 (0)