ownedWebSocketSession

suspend fun HttpClient.ownedWebSocketSession(urlString: String, block: HttpRequestBuilder.() -> Unit = {}): DefaultClientWebSocketSession

Open a WebSocket to urlString and hand the session to the caller, or give it up.

Walk away from a dial and the socket goes with it, on every engine but one. Cancel this call while the far end is still answering, and on CIO and Darwin the request is aborted. Cancel it in the instant the session arrives, and that session gets a normal close before the cancellation reaches you, since nobody will ever hold it. Only a return makes the session yours, and from then on closing it is your job. A cancel does not wait for an abort to land, so the socket can outlive this call by a moment.

Use this in place of Ktor's webSocketSession, which does not make that promise. That one runs the handshake on the HttpClient's scope and only waits for it, so a cancel ends the wait and nothing else. The finished socket then stays open with no owner until the session ends from outside or the client closes. The far end sees a healthy connection the whole time.

Where a cancel before the upgrade does not end the socket. Pick CIO or Darwin if a far end might never answer.

  • OkHttp does not abort. The socket waits on OkHttp's own read timeout, about 10 s by default, and with readTimeout(0) it was still open at 30 s. A far end that answers after the cancel gets a finished upgrade and then a 1011 Close frame. Nothing here can reach that socket.

  • Darwin loses one race. An answer written right behind the cancel, on the first dial of a process, beat the cancel in 12 of 12 fresh processes, and the socket was still open at 4 to 8 s in eleven and at 90 s in the twelfth. With 1 ms or more between the two it ended in 21 of 21. Rarely, a dial cancelled as the session arrives stays open too, with no Close frame (see #3102).

It differs in two more ways, both about failure:

  • On an HttpClient that has closed, with no session still open on it, this throws IOException. Ktor's call never returns there: its handshake coroutine is cancelled before it starts, so nothing completes what the caller is waiting on. A session still open keeps the client's job active, and nothing public says the client closed, so the dial simply goes ahead. On CIO and OkHttp it reached the far end and returned a session, and it was still waiting at 10 s on a far end that stayed silent. Darwin is unmeasured.

  • A cancellation that is not the caller's own arrives as IOException. The caller's job is alive, so a CancellationException would cancel it rather than fail it, with no handler and no stack trace (see us.tractat.kuilt.core.Loom.weave).

What it does not bound. There is no clock on the dial, and an HttpClient closed during one does not end it. To bound it, wrap this call in a timeout, which is an ordinary cancel with the outcomes above. install(HttpTimeout) is not a way to bound it: Ktor turns the request timeout off for a WebSocket request, so on CIO a 500 ms requestTimeoutMillis was still waiting at 13 s on a far end holding its 101, and a 500 ms socketTimeoutMillis killed an idle, healthy session within 3 s.

How long a closed session keeps its socket depends on the engine. On CIO it stays until the far end answers the Close frame or hangs up, because ending it sooner can lose the frame, and a far end that does neither gets two seconds. On Darwin, URLSession hangs up by itself within milliseconds. OkHttp keeps the socket waiting for that answer on a clock of its own, which was still running 15 seconds later. A cancelled caller waits at most two seconds on any of them.

Wrap the returned session in WebSocketConnection to build a seam on it.

Parameters

urlString

The ws:// or wss:// URL to dial.

block

Further request configuration, such as headers.

Throws

IOException

if HttpClient has closed with no session still open on it, or the dial ends in a cancellation nobody asked for.