大网数据广告活动
Web 标准与前端开发

WebTransportDatagramDuplexStream

· 52CE 我爱测

The **`WebTransportDatagramDuplexStream`** interface of the WebTransport API represents a duplex stream that can be used for unreliable transport of datagrams between client and server. Provides access to a ReadableStream for reading incoming datagrams, a WritableStream for writi

WebTransport API

The WebTransportDatagramDuplexStream interface of the WebTransport API represents a duplex stream that can be used for unreliable transport of datagrams between client and server. Provides access to a ReadableStream for reading incoming datagrams, a WritableStream for writing outgoing datagrams, and various settings and statistics related to the stream.

This is accessed via the WebTransport.datagrams property.

"Unreliable" means that transmission of data is not guaranteed, nor is arrival in a specific order. This is fine in some situations and provides very fast delivery. For example, you might want to transmit regular game state updates where each message supersedes the last one that arrives, and order is not important.

Instance properties

  • WebTransportDatagramDuplexStream.incomingHighWaterMark
    • : Represents the maximum number of incoming datagrams that can be queued while waiting to be read from the WebTransportDatagramDuplexStream.readable stream. Use WebTransportDatagramDuplexStream.incomingMaxBufferedDatagrams instead.
  • WebTransportDatagramDuplexStream.incomingMaxAge
    • : Gets or sets the maximum age for incoming datagrams, in milliseconds. Returns null if no maximum age has been set.
  • WebTransportDatagramDuplexStream.incomingMaxBufferedDatagrams
    • : Represents the maximum number of incoming datagrams that can be queued while waiting to be read from the WebTransportDatagramDuplexStream.readable stream.
  • WebTransportDatagramDuplexStream.maxDatagramSize
    • : Returns the maximum allowable size of outgoing datagrams, in bytes, that can be written to a WebTransportDatagramsWritable obtained via WebTransportDatagramDuplexStream.createWritable, or the deprecated WebTransportDatagramDuplexStream/writable property.
  • WebTransportDatagramDuplexStream.outgoingHighWaterMark
    • : Represents the maximum number of outgoing datagrams that can be queued for sending before backpressure is applied. Use WebTransportDatagramDuplexStream.outgoingMaxBufferedDatagrams instead.
  • WebTransportDatagramDuplexStream.outgoingMaxAge
    • : Gets or sets the maximum age for outgoing datagrams, in milliseconds. Returns null if no maximum age has been set.
  • WebTransportDatagramDuplexStream.outgoingMaxBufferedDatagrams
    • : Represents the maximum number of outgoing datagrams that can be queued for sending on each WebTransportDatagramsWritable before backpressure is applied.
  • WebTransportDatagramDuplexStream.readable
    • : Returns a ReadableStream instance that can be used to read incoming datagrams from the stream.
  • WebTransportDatagramDuplexStream.writable
    • : Returns a WritableStream instance that can be used to write outgoing datagrams to the stream.

Instance methods

  • WebTransportDatagramDuplexStream.createWritable
    • : Returns a WebTransportDatagramsWritable instance that can be used to write outgoing datagrams to the stream.

Examples

Writing outgoing datagrams

This code uses the WebTransportDatagramDuplexStream.createWritable method, if it is supported, to get a WebTransportDatagramsWritable instance that can be used for writing data to the transport. Otherwise, it falls back to the WebTransportDatagramDuplexStream/writable property , which returns a WritableStream object that you can write data to using a writer instead:

const writableStream =
  typeof transport.datagrams.createWritable === "function"
    ? transport.datagrams.createWritable()
    : transport.datagrams.writable; // Deprecated and non-standard

const writer = writableStream.getWriter();
const data1 = new Uint8Array([65, 66, 67]);
const data2 = new Uint8Array([68, 69, 70]);
await writer.ready;
writer.write(data1);
await writer.ready;
writer.write(data2);

Reading incoming datagrams

The WebTransportDatagramDuplexStream.readable property returns a ReadableStream object that you can use to receive data from the server:

async function readData() {
  const reader = transport.datagrams.readable.getReader();
  while (true) {
    const { value, done } = await reader.read();
    if (done) {
      break;
    }
    // value is a Uint8Array.
    console.log(value);
  }
}

Alternatively, to read datagrams into a buffer that you supply, construct the transport with the datagramsReadableType option set to bytes, and read the stream using a ReadableStreamBYOBReader. See the Reading datagrams with a BYOB reader example on the WebTransport() constructor page.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。