or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

caching.mdcookies.mdforms-multipart.mdhttp-client.mdindex.mdinterceptors.mdnetworking.mdrequests-responses.mdsecurity.mdurls.mdwebsockets.md

websockets.mddocs/

0

# WebSocket Support

1

2

WebSocket protocol support for real-time bidirectional communication.

3

4

## WebSocket Interface

5

6

```kotlin { .api }

7

interface WebSocket {

8

fun request(): Request

9

fun queueSize(): Long

10

fun send(text: String): Boolean

11

fun send(bytes: ByteString): Boolean

12

fun close(code: Int, reason: String?): Boolean

13

fun cancel()

14

}

15

```

16

17

## WebSocketListener

18

19

```kotlin { .api }

20

abstract class WebSocketListener {

21

open fun onOpen(webSocket: WebSocket, response: Response) {}

22

open fun onMessage(webSocket: WebSocket, text: String) {}

23

open fun onMessage(webSocket: WebSocket, bytes: ByteString) {}

24

open fun onClosing(webSocket: WebSocket, code: Int, reason: String) {}

25

open fun onClosed(webSocket: WebSocket, code: Int, reason: String) {}

26

open fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {}

27

}

28

```

29

30

### WebSocket Example

31

32

```kotlin

33

val listener = object : WebSocketListener() {

34

override fun onMessage(webSocket: WebSocket, text: String) {

35

println("Received: $text")

36

}

37

}

38

39

val request = Request.Builder().url("wss://echo.websocket.org").build()

40

val webSocket = client.newWebSocket(request, listener)

41

webSocket.send("Hello WebSocket!")

42

```