You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Berry.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -809,6 +809,44 @@ Module `webserver` also defines the following constants:
809
809
810
810
See the [Berry Cookbook](Berry-Cookbook.md) for examples.
811
811
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<aclass="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<aclass="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<aclass="cmnd"id="tcpclient_close">|`close() -> nil`<BR>Drops the current connection.
830
+
write<aclass="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<aclass="cmnd"id="tcpclient_close">|`available() -> int`<BR>Returns the number of bytes received in buffer and ready to be read.
832
+
read<aclass="cmnd"id="tcpclient_read">|`read() -> string`<BR>Returns all the bytes received in Rx buffer as `string`.
833
+
readbytes<aclass="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
+
812
850
### `udp` class
813
851
814
852
Class `udp` provides ability to send and received UDP packets, including multicast addresses.
0 commit comments