The WebTransport()
constructor creates a new WebTransport
object instance.
new WebTransport(url, options)
url
-
A string representing the URL of the HTTP/3 server to connect to. The scheme needs to be HTTPS, and the port number needs to be explicitly specified.
-
options
Optional
-
An object containing the following properties:
-
serverCertificateHashes
Optional
-
An array of WebTransportHash
objects. If specified, it allows the website to connect to a server by authenticating the certificate against the expected certificate hash instead of using the Web public key infrastructure (PKI). This feature allows Web developers to connect to WebTransport servers that would normally find obtaining a publicly trusted certificate challenging, such as hosts that are not publicly routable, or ephemeral hosts like virtual machines.
WebTransportHash
objects contain two properties:
algorithm
-
A string representing the algorithm to use to verify the hash. Any hash using an unknown algorithm will be ignored.
value
-
A BufferSource
representing the hash value.
const url = "https://example.com:4999/wt";
async function initTransport(url) {
const transport = new WebTransport(url);
await transport.ready;
}
async function closeTransport(transport) {
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}.`);
}
}