async function sendDatagrams(url, datagrams, writableOptions = {}) {
const wt = new WebTransport(url);
await wt.ready;
const writable =
typeof wt.datagrams.createWritable === "function"
? wt.datagrams.createWritable(writableOptions)
: wt.datagrams.writable;
const writer = writable.getWriter();
for (const bytes of datagrams) {
await writer.ready;
writer.write(bytes).catch(() => {});
}
}WebTransportDatagramDuplexStream: createWritable() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const url = "https://example.com/webtransport";
const datagrams = [new Uint8Array([65, 66, 67]), new Uint8Array([68, 69, 70])];
await sendDatagrams(url, datagrams, { sendOrder: 1 });