ping
ping sends one ICMP Echo request and returns its round-trip time in milliseconds inside Ok; recoverable probe failures are returned as Err(NetError).
The function is available when supportsIcmp() is true. It is implemented on Linux, macOS, and Windows and returns Err(NetError) when the platform or sandbox does not provide the required capability. macOS requires permission to open a raw ICMP socket.
Signature
ping(address: string, timeoutMs?: u64): Promise<Result<u64, NetError>>tyrus
import { NetError, ping, supportsIcmp } from "tyrus:net";
async function measure(): Promise<void> {
if (supportsIcmp()) {
const result: Result<u64, NetError> = await ping("127.0.0.1", 1000);
match (result) {
Ok(milliseconds) => { print(milliseconds); } // 0
Err(error) => { printError(error.code); } // ERR_NET_TIMEOUT
}
}
}Error Handling
error
tyrus
import { NetError, ping } from "tyrus:net";
async function measure(): Promise<void> {
const result: Result<u64, NetError> = await ping("invalid host\n", 1000);
match (result) {
Ok(_) => {}
Err(error) => { printError(error.code); } // ERR_NET_RESOLVE
}
}Failures use ERR_NET_INVALID_ADDRESS, ERR_NET_PERMISSION, ERR_NET_TIMEOUT, or ERR_NET_UNSUPPORTED.
