Skip to content

Commit 9ac6b29

Browse files
authored
Add tcpclient
1 parent a7023d6 commit 9ac6b29

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/Berry.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,44 @@ Module `webserver` also defines the following constants:
809809

810810
See the [Berry Cookbook](Berry-Cookbook.md) for examples.
811811

812+
### `tcpclient` class
813+
814+
Simple tcp client supporting string and binary transfers:
815+
816+
- create an instance of the client with `var tcp = tcpclient()`
817+
- connect to the server `tcp.connect(address:string, port:int [, timeout_ms:int]) -> bool` Address can be numerical IPv4 or domain name. Returns `true` if the connection succeeded. Optional `timeout` in milliseconds. The default timeout is `USE_BERRY_WEBCLIENT_TIMEOUT` (2 seconds).
818+
- check if the socket is connected with `tcp.connected()`
819+
- send content with `tcp.write(content:string or bytes) -> int`. Accepts either a string or a bytes buffer, returns the number of bytes sent. It's you responsibility to resend the missing bytes
820+
- check if bytes are available for reading `tcp.available() -> int`. Returns `0` if nothing was received. This is the call you should make in loops for polling.
821+
- read incoming content as string `tcp.read() -> string` or as bytes `tcp.readbytes() -> bytes`. It is best to call `tcp.available()` first to avoid creating empty response objects when not needed
822+
- close the socket with `tcp.close()`
823+
824+
825+
tcpclient Function|Parameters and details
826+
:---|:---
827+
connect<a class="cmnd" id="tcpclient_connect">|`connect(address:string, port:int [, timeout_ms:int]) -> bool`<BR>Connect to `addr:port` with optional timeout in milliseconds (default 2s).<BR>Returns `true` if connection was successful, the call is blocking until the connection succeeded to the timeout expired.
828+
connected<a class="cmnd" id="tcpclient_connected">|`connected() -> bool`<BR>Returns `true` if the connection was successful and is still valid (not dropped by server or closed by client)
829+
close<a class="cmnd" id="tcpclient_close">|`close() -> nil`<BR>Drops the current connection.
830+
write<a class="cmnd" id="tcpclient_write">|`content:string or bytes) -> int`<BR>Accepts either a string or a bytes buffer, returns the number of bytes sent. It's you responsibility to resend the missing bytes.<BR>Returns `0` if something went wrong.
831+
available<a class="cmnd" id="tcpclient_close">|`available() -> int`<BR>Returns the number of bytes received in buffer and ready to be read.
832+
read<a class="cmnd" id="tcpclient_read">|`read() -> string`<BR>Returns all the bytes received in Rx buffer as `string`.
833+
readbytes<a class="cmnd" id="tcpclient_read">|`read() -> bytes()`<BR>Returns all the bytes received in Rx buffer as `bytes()`.
834+
Full example:
835+
836+
```
837+
tcp = tcpclient()
838+
tcp.connect("192.168.2.204", 80)
839+
print("connected:", tcp.connected())
840+
s= "GET / HTTP/1.0\r\n\r\n"
841+
tcp.write(s)
842+
print("available1:", tcp.available())
843+
tasmota.delay(100)
844+
print("available1:", tcp.available())
845+
r = tcp.read()
846+
tcp.close()
847+
print(r)
848+
```
849+
812850
### `udp` class
813851

814852
Class `udp` provides ability to send and received UDP packets, including multicast addresses.

0 commit comments

Comments
 (0)