如何在 Deno 中发送 UDP 数据?

Dar*_*agh 6 udp deno

我尝试在我的measure.ts脚本中使用以下代码:

Deno.DatagramConn.send(...)
Run Code Online (Sandbox Code Playgroud)

当我像这样运行我的脚本时: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?
Run Code Online (Sandbox Code Playgroud)

Deno.DatagramConn这个错误似乎同时否认和确认了API的存在

同样我也尝试过

Deno.connect({transport : 'udp'})
Run Code Online (Sandbox Code Playgroud)

但这给了我以下错误(这可能是有意义的,因为 UDP 是“无连接”):

Type '"udp"' is not assignable to type '"tcp"
Run Code Online (Sandbox Code Playgroud)

Dar*_*agh 7

我似乎已经想通了。我实际上需要首先listen在套接字上,然后在其上发送数据。

\n
const addr : Deno.NetAddr = {transport: "udp", port: 8125, hostname: "1.2.3.4"};\n\nconst socket = await Deno.listenDatagram({\n        port: 0,\n        transport: "udp",\n        hostname: "0.0.0.0"\n       });\n  \nsocket.send(new Uint8Array(), addr);\n
Run Code Online (Sandbox Code Playgroud)\n

当你知道如何做时,这很容易 \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf

\n