Skip to content

tcpListen ​

tcpListen vincula um endpoint TCP e retorna um handle TcpListener dentro de Ok; falhas recuperáveis de bind retornam Err(NetError).

Use accept para receber conexões. O timeout opcional se aplica ao bind e é expresso em milissegundos.

AssinaturatcpListen(address: string, timeoutMs?: u64): Promise<Result<TcpListener, NetError>>
tyrus
import { NetError, TcpListener, tcpListen } from "tyrus:net";

async function listen(): Promise<Result<void, NetError>> {
    const listener: TcpListener = (await tcpListen("127.0.0.1:0", 1000))?;
    print(listener.localAddress()); // "127.0.0.1:random-port"
    listener.close();
    return Ok();
}

Tratamento de erro ​

error

tyrus
import { NetError, TcpListener, tcpListen } from "tyrus:net";

async function listen(): Promise<void> {
    const result: Result<TcpListener, NetError> = await tcpListen("", 1000);
    match (result) {
        Ok(listener) => { listener.close(); }
        Err(error) => { printError(error.code); }
    }
}

Falhas de bind cumprem com Err(NetError) usando ERR_NET_INVALID_ADDRESS, ERR_NET_PERMISSION ou outro código ERR_NET_* dedicado.