Skip to content

udpBind ​

udpBind vincula um socket UDP local e retorna um handle UdpSocket explícito.

O socket retornado pode enviar e receber datagramas de forma assíncrona. O handle deve ser fechado quando não for mais necessário.

AssinaturaudpBind(address: string): Promise<Result<UdpSocket, NetError>>
tyrus
import { NetError, UdpSocket, udpBind } from "tyrus:net";

async function openSocket(): Promise<Result<void, NetError>> {
    const socket: UdpSocket = (await udpBind("127.0.0.1:0"))?;
    print(socket.localAddress()); // "127.0.0.1:random-port"
    socket.close();
    return Ok();
}

Tratamento de erro ​

error

tyrus
import { NetError, udpBind } from "tyrus:net";

async function openSocket(): Promise<void> {
    const result: Result<UdpSocket, NetError> = await udpBind("");
    match (result) {
        Ok(socket) => { socket.close(); }
        Err(error) => { print(error.code); // "ERR_NET_INVALID_ADDRESS"
        }
    }
}

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