tcpConnect
tcpConnect connects to a TCP endpoint and returns a TcpStream handle inside Ok; recoverable connection failures return Err(NetError).
The optional timeout is expressed in milliseconds. The returned stream supports asynchronous reads, writes, shutdown, and close.
Signature
tcpConnect(address: string, timeoutMs?: u64): Promise<Result<TcpStream, NetError>>tyrus
import { NetError, TcpStream, tcpConnect } from "tyrus:net";
async function connect(): Promise<Result<void, NetError>> {
const stream: TcpStream = (await tcpConnect("127.0.0.1:8080", 1000))?;
stream.close();
return Ok();
}Error Handling
error
tyrus
import { NetError, TcpStream, tcpConnect } from "tyrus:net";
async function connect(): Promise<void> {
const result: Result<TcpStream, NetError> = await tcpConnect("127.0.0.1:1", 100);
match (result) {
Ok(stream) => { stream.close(); }
Err(error) => { printError(error.code); }
}
}Connection failures fulfill with Err(NetError) using ERR_NET_TIMEOUT, ERR_NET_CONNECTION_REFUSED, or another dedicated ERR_NET_* code.
