WebTransport: closed 属性
注意:此功能在 Web Workers 中可用。
WebTransport 接口的只读属性 closed 返回一个 Promise,该 Promise 在传输关闭时解析。
此 Promise 在创建 WebTransport 对象时创建,并在以任何方式关闭传输时解析,例如通过调用 close() 方法,连接失败时,或由服务器关闭连接时。访问或等待此 Promise 不会触发任何操作;它只允许在传输关闭时采取操作,类似于监听事件。
值
一个 Promise,它解析为一个包含以下属性的对象:
或者,在意外关闭(例如网络故障)的情况下,Promise 会被一个错误拒绝。
示例
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-closed |
浏览器兼容性
加载中…