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;
return transport;
}
let transport = await initTransport(url);WebTransport: draining property / notebook
WebTransport: draining property
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
async function migrateOnDraining(url, oldTransport) {
// Promise fulfills when signaled by server
await oldTransport.draining;
// Open a replacement session so the ongoing feed isn't interrupted
const newTransport = await initTransport(url);
// Switch to sending further data on newTransport instead of oldTransport
// …
// The old session is no longer needed once the new one is in use
oldTransport.close();
return newTransport;
}
// Not awaited: settles when draining starts
migrateOnDraining(url, transport).then((newTransport) => {
transport = newTransport;
});async function watchForClose(transport) {
try {
const { closeCode, reason } = await transport.closed;
console.log(`Session closed gracefully (code ${closeCode}): ${reason}`);
} catch (error) {
// The session can close abruptly, without draining being signaled first
console.error(`Session closed unexpectedly: ${error}`);
}
}
watchForClose(transport);