WebTransport: ready 属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

readyWebTransport 接口的一个只读属性,它返回一个 Promise,该 Promise 在传输准备好使用时解析。

此 Promise 在创建 WebTransport 对象时创建,并在建立连接时解析。访问或等待此 Promise 不会触发任何操作;它只允许在传输准备就绪时执行操作,类似于监听事件。

一个 Promise,解析为 undefined

示例

js
const url = "https://example.com:4999/wt";

async function initTransport(url) {
  // Initialize transport connection
  const transport = new WebTransport(url);

  // The connection can be used once ready fulfills
  await transport.ready;

  // …
}

// …

async function closeTransport(transport) {
  // Respond to connection closing
  try {
    await transport.closed;
    console.log(`The HTTP/3 connection to ${url} closed gracefully.`);
  } catch (error) {
    console.error(`The HTTP/3 connection to ${url} closed due to ${error}.`);
  }
}

规范

规范
WebTransport
# dom-webtransport-ready

浏览器兼容性

另见