Skip to content

Commit

Permalink
WiFiServer operator bool
Browse files Browse the repository at this point in the history
  • Loading branch information
JAndrassy committed Oct 8, 2023
1 parent 12c33f1 commit 2b04572
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
57 changes: 57 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,63 @@ void loop() {
```

### `if(server)`

#### Description
Indicates whether the server is listening for new clients. You can use this to detect whether server.begin() was successful.


#### Syntax

```
if (server)
if (!server)
```

#### Parameters
none

#### Returns
- whether the server is listening for new clients (bool).

#### Example

```
#include <WiFiNINA.h>
char ssid[] = "Network"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
WiFiServer server(23);
void setup() {
Serial.begin(115200);
while (!Serial) {}
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a WiFi connection");
while(true);
}
server.begin();
if (!server) {
Serial.println("Server failed to start.");
while(true);
}
}
void loop() {
WiFiClient client = server.available();
if (client) {
String s = client.readStringUntil('\n');
server.println(s);
}
}
```

### `server.status()`

#### Description
Expand Down
3 changes: 3 additions & 0 deletions src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ uint8_t WiFiServer::status() {
}
}

WiFiServer::operator bool() {
return (_sock != NO_SOCKET_AVAIL);
}

size_t WiFiServer::write(uint8_t b)
{
Expand Down
1 change: 1 addition & 0 deletions src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WiFiServer : public Server {
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
uint8_t status();
explicit operator bool();

using Print::write;
};
Expand Down

0 comments on commit 2b04572

Please sign in to comment.