WebTransport: thuộc tính incomingBidirectionalStreams

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 incomingBidirectionalStreams của giao diện WebTransport đại diện cho một hoặc nhiều luồng hai chiều do máy chủ mở. Nó trả về một ReadableStream của các đối tượng WebTransportBidirectionalStream. 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 và ghi dữ liệu trở lại máy chủ.

"Đá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 WebTransportBidirectionalStream.

Ví dụ

Một hàm ban đầu được dùng để đọc các đối tượng WebTransportBidirectionalStream từ ReadableStream. Với mỗi đối tượng, các giá trị WebTransportBidirectionalStream.readableWebTransportBidirectionalStream.writable được chuyển cho các hàm khác để đọc từ và ghi vào các luồng đó.

js
async function receiveBidirectional() {
  const bds = transport.incomingBidirectionalStreams;
  const reader = bds.getReader();
  while (true) {
    const { done, value } = await reader.read();
    if (done) {
      break;
    }
    // value là một thể hiện của WebTransportBidirectionalStream
    await readData(value.readable);
    await writeData(value.writable);
  }
}

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

async function writeData(writable) {
  const writer = writable.getWriter();
  const data1 = new Uint8Array([65, 66, 67]);
  const data2 = new Uint8Array([68, 69, 70]);
  writer.write(data1);
  writer.write(data2);
}

Thông số kỹ thuật

Thông số kỹ thuật
WebTransport
# dom-webtransport-incomingbidirectionalstreams

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

Xem thêm