WebTransport: thuộc tính incomingUnidirectionalStreams

Baseline 2026
Newly available

Since March 2026, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Thuộc tính chỉ đọc incomingUnidirectionalStreams của giao diện WebTransport đại diện cho một hoặc nhiều luồng một chiều do máy chủ mở. Nó trả về một ReadableStream của các đối tượng WebTransportReceiveStream. Mỗi đối tượng có thể được dùng để đọc dữ liệu từ máy chủ một cách đáng tin cậy.

"Đáng tin cậy" có nghĩa là việc truyền và thứ tự dữ liệu được bảo đảm. Điều này mang lại khả năng phân phối chậm hơn (dù vẫn nhanh hơn WebSockets) so với datagrams, nhưng cần thiết trong các tình huống mà độ tin cậy và thứ tự là quan trọng, như ứng dụng chat.

Giá trị

Một ReadableStream của các đối tượng WebTransportReceiveStream.

Ví dụ

Một hàm ban đầu được dùng để đọc các đối tượng WebTransportReceiveStream từ ReadableStream. Mỗi object sau đó được chuyển cho một hàm khác để đọc từ các luồng đó.

js
async function receiveUnidirectional() {
  const uds = transport.incomingUnidirectionalStreams;
  const reader = uds.getReader();
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      break;
    }
    // value là một thể hiện của WebTransportReceiveStream
    await readData(value);
  }
}

async function readData(receiveStream) {
  const reader = receiveStream.getReader();
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      break;
    }
    // value là một Uint8Array
    console.log(value);
  }
}

Thông số kỹ thuật

Specification
WebTransport
# dom-webtransport-incomingunidirectionalstreams

Tương thích trình duyệt

Xem thêm