【问题标题】:How can I send UDP data in Deno?如何在 Deno 中发送 UDP 数据?
【发布时间】:2020-12-09 04:29:19
【问题描述】:

我尝试在我的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 是“无连接”的):

Type '"udp"' is not assignable to type '"tcp"

【问题讨论】:

    标签: udp deno


    【解决方案1】:

    我好像明白了。我实际上需要先在一个套接字上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);
    

    当你知道¯\_(ツ)_/¯如何时,这很容易

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 2011-09-24
      • 2019-05-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      相关资源
      最近更新 更多