How can I send UDP data in Deno?
Asked Answered
A

1

6

I've tried using the following code in my measure.ts script:

Deno.DatagramConn.send(...)

When I run my script like this: deno run --unstable --allow-all measure.ts I get the following error:

Property 'DatagramConn' does not exist on type 'typeof Deno'. 'Deno.DatagramConn' is an 
unstable API. Did you forget to run with the '--unstable' flag?

This error seems to simultaneously deny and confirm the existence of the Deno.DatagramConn API

Similarly I've tried

Deno.connect({transport : 'udp'})

but this gives me the follow error (which probably makes sense as UDP is 'connectionless'):

Type '"udp"' is not assignable to type '"tcp"
Acne answered 8/12, 2020 at 19:33 Comment(0)
A
7

I seem to have figured it out. I actually need to first listen on a socket, and then send data on it.

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);

It's easy when you know how ¯\_(ツ)_/¯

Acne answered 8/12, 2020 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.