Scoop API

scoop:scoop.http.client:0.1.0

src/websocket/result.scoop

Selected production source · src/websocket/result.scoop

1package scoop.http.client
3import scoop.asyncio.AsyncCloseable
5/**
6 * An opened session or a consumable rejection response plus handshake error. Both variants require
7 * close.
8 */
9public enum HttpWebSocketResult: AsyncCloseable {
10 /**
11 * The handshake succeeded and owns an active WebSocket session.
12 */
13 Opened(HttpWebSocketSession),
14 /**
15 * The handshake was rejected; the HTTP response body remains available together with the
16 * validation error.
17 */
18 Rejected(HttpResponse, Throwable)
20 /**
21 * Closes the opened session or the rejected handshake response.
22 */
23 public override suspend fun close() {
24 when (this) {
25 case Opened(val session) -> session.close();
26 case Rejected(val response, _) -> response.close()
27 }
28 }
31internal suspend fun openClientWebSocket(
32 client: ClientState,
33 request: HttpWebSocketRequest,
34 options: HttpWebSocketOptions
35): HttpWebSocketResult {
36 clientJob(client.loop, true)
37 validateClientWebSocket(request, options)
38 val http =
39 httpRequest(request.httpUrl) {
40 this.fields = request.fields
41 this.options = clientWebSocketOptions(request.options)
42 }
43 val sequence = ClientRequestSequence(client.config, http, webSocket = Some(options))
44 return when (interceptClientSequence(client, http, sequence)) {
45 case WebSocket(val result) -> result
46 case Response(_) -> throw IllegalStateException(
47 Some("WebSocket request returned an ordinary HTTP result")
48 )
49 }