Skip to content

Commit

Permalink
webtransport: add a page about WebTransport sessions (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann authored Apr 1, 2024
1 parent d976fb6 commit 194a481
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion content/docs/webtransport/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The `webtransport.Server` wraps an `http3.Server`. For more details on how to se

Assume a server is running on `example.com`. This code starts an HTTP/3 server on (UDP) port 443. The server can handle regular HTTP/3 requests to `https://example.com`.

To accept the Extended CONNECT request, the application needs to define an HTTP handler. In this example, we want to accept WebTransport sessions at `https://example.com/webtransport`. It is possible to reject an upgrade request by sending a non-2xx status code. Inside the handler, calling `webtransport.Server.Upgrade` accepts the WebTransport session, and it returns a `webtransport.Session`.
To accept the Extended CONNECT request, the application needs to define an HTTP handler. In this example, we want to accept WebTransport sessions at `https://example.com/webtransport`. It is possible to reject an upgrade request by sending a non-2xx status code. Inside the handler, calling `webtransport.Server.Upgrade` accepts the [WebTransport session]({{< relref "session.md" >}}), and it returns a `webtransport.Session`.

```go
s := webtransport.Server{
Expand Down
38 changes: 38 additions & 0 deletions content/docs/webtransport/session.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: WebTransport Session
toc: true
weight: 3
---

A WebTransport Session functions similarly to a [QUIC Connection]({{< relref "../quic/connection.md" >}}), enabling the opening and accepting of streams, as well as the sending and receiving of datagrams.

The API of `webtransport.Session` is _almost_ identical to that of `quic.Connection`, with a few minor differences: For example, QUIC allows streams to be reset using a 62-bit error code, whereas WebTransport limits the error code range to 32 bits.

## Closing a WebTransport Session

The WebTransport session can be closed by calling the `CloseWithError` method:
```go
sess.CloseWithError(1234, "please stop talking to me 🤐")
```

Similar to closing a `quic.Connection`, this action causes all calls to `AcceptStream` and `OpenStream`, as well as stream `Read` and `Write` calls, to return immediately.

{{< callout type="warning" >}}
`CloseWithError` only closes the WebTransport session, but not the underlying QUIC connection.
{{< /callout >}}

On the receiver side, this error will be surfaced as a `webtransport.SessionError`:
```go
var sessErr *webtransport.SessionError
if errors.As(err, &sessErr) {
errorCode := sessErr.ErrorCode
errorMessage := sessErr.Message
}
```

Additionally, the underlying QUIC connection might close for various reasons, potentially triggering any of the errors detailed in the [error assertion section]({{< relref "../quic/connection.md#error-assertion" >}}).


## 📝 Future Work

* WebTransport Datagrams: [#8](https://github.com/quic-go/webtransport-go/issues/8)

0 comments on commit 194a481

Please sign in to comment.