如何在 Deno 中发送 UDP 数据?
How can I send UDP data in Deno?
我已经尝试在我的 measure.ts
脚本中使用以下代码:
Deno.DatagramConn.send(...)
当我 运行 我的脚本是这样的:deno run --unstable --allow-all measure.ts
我得到以下错误:
Property 'DatagramConn' does not exist on type 'typeof Deno'. 'Deno.DatagramConn' is an
unstable API. Did you forget to run with the '--unstable' flag?
这个错误似乎同时否认和确认 Deno.DatagramConn
API
的存在
我也试过
Deno.connect({transport : 'udp'})
但这给了我以下错误(这可能是有道理的,因为 UDP 是 'connectionless'):
Type '"udp"' is not assignable to type '"tcp"
我好像明白了。我实际上需要先 listen
在套接字上,然后在其上发送数据。
const addr : Deno.NetAddr = {transport: "udp", port: 8125, hostname: "1.2.3.4"};
const socket = await Deno.listenDatagram({
port: 0,
transport: "udp",
hostname: "0.0.0.0"
});
socket.send(new Uint8Array(), addr);
知道怎么做就很容易了¯\_(ツ)_/¯
我已经尝试在我的 measure.ts
脚本中使用以下代码:
Deno.DatagramConn.send(...)
当我 运行 我的脚本是这样的:deno run --unstable --allow-all measure.ts
我得到以下错误:
Property 'DatagramConn' does not exist on type 'typeof Deno'. 'Deno.DatagramConn' is an
unstable API. Did you forget to run with the '--unstable' flag?
这个错误似乎同时否认和确认 Deno.DatagramConn
API
我也试过
Deno.connect({transport : 'udp'})
但这给了我以下错误(这可能是有道理的,因为 UDP 是 'connectionless'):
Type '"udp"' is not assignable to type '"tcp"
我好像明白了。我实际上需要先 listen
在套接字上,然后在其上发送数据。
const addr : Deno.NetAddr = {transport: "udp", port: 8125, hostname: "1.2.3.4"};
const socket = await Deno.listenDatagram({
port: 0,
transport: "udp",
hostname: "0.0.0.0"
});
socket.send(new Uint8Array(), addr);
知道怎么做就很容易了¯\_(ツ)_/¯