net network communication library
net network communication library
Function
net provides Web service adaptation, TCP, UDP, WebSocket, DNS resolution and native network information capabilities. You can use either the net.listen() shorthand or net().listen().
API List
| API | Description |
|---|---|
| net.listen | Start the Web, TCP, UDP or WebSocket listening service. |
| net.connect | Establish a TCP, UDP, or WebSocket client connection. |
| net.resolve | Resolve the host name and return the deduplicated IP address array. |
| net.interfaces | Read the local network interface information. |
| net.local_ip | Read the default local IP. |
Examples
ips = net.resolve('localhost') result = type(ips) // Output: Array print result
Notes
- Use lowercase strings for all protocol type fields: web, tcp, udp, ws.
- TCP, UDP, WebSocket background I/O uses shared Tokio runtime; scripting API still maintains synchronous call surface.
- TCP, UDP, and WebSocket listening configurations support binary: true. The on_message callback will directly receive Bytes to avoid binary messages being converted into strings in advance.
- The network event queue is a bounded queue, defaulting to BT_NET_EVENT_QUEUE=4096. When the queue is full, the memory will not accumulate indefinitely; TCP/WebSocket message events will trigger the connection to be closed, UDP packets will be discarded, and can be observed through BT.stats().net.event_queue_rejected.
- The resource limit can be adjusted through
BT_NET_CONNECTION_LIMIT,BT_NET_MESSAGE_LIMIT,BT_NET_WRITE_QUEUE,BT_NET_IDLE_TTL_MS. Configuration errors will cause an error when using the net capability for the first time.
-
examples/net-stress-*.btprovides TCP, UDP, and WebSocket stress test scripts; when you need to verify that the queue is full or the message exceeds the limit, you can reduceBT_NET_EVENT_QUEUE,BT_NET_WRITE_QUEUE, orBT_NET_MESSAGE_LIMITbefore starting.