deno.com

UDP 监听器: Ping

在 Github 上编辑

警告: 这是一个不稳定的 API,随时可能更改或删除。
一个在 localhost 上 UDP 监听器的示例,如果写入消息将记录消息,如果连接到将关闭连接。

实例化文本解码器的实例,以将 UDP 流字节读回纯文本。
const decoder = new TextDecoder();
在 localhost 端口 10000 上实例化 UDP 监听器的实例。
const listener = Deno.listenDatagram({
  port: 10000,
  transport: "udp",
});
等待发送到我们的 UDP 监听器的异步消息。
for await (const [data, address] of listener) {
这里我们记录数据发送者的地址
  console.log("Server - received information from", address);
这里我们记录读取到缓冲区数组中的字节结果。
  console.log("Server - received:", decoder.decode(data));
我们关闭已建立的连接。
  listener.close();
}

使用 Deno CLI 在本地运行 此示例

deno run --allow-net --unstable-net https://docs.deno.org.cn/examples/scripts/udp_listener.ts

你找到你需要的东西了吗?

隐私政策