Package io.ktor.http.cio.websocket

Types

CloseReason

data class CloseReason

Websocket close reason

DefaultWebSocketSession

expect interface DefaultWebSocketSession : WebSocketSession

Default websocket session with ping-pong and timeout processing and built-in closeReason population

DefaultWebSocketSessionImpl

class DefaultWebSocketSessionImpl : 
    DefaultWebSocketSession,
    WebSocketSession

Default web socket session implementation that handles ping-pongs, close sequence and frame fragmentation

Frame

sealed expect class Frame

A frame received or ready to be sent. It is not reusable and not thread-safe

FrameParser

class FrameParser

FrameType

enum class FrameType

Frame types enum

RawWebSocket

class RawWebSocket : WebSocketSession

Represents a RAW web socket session

Serializer

class Serializer

SimpleFrameCollector

class SimpleFrameCollector

WebSocketReader

class WebSocketReader : CoroutineScope

Class that continuously reads a byteChannel and converts into Websocket Frame exposing them in incoming.

WebSocketSession

expect interface WebSocketSession : CoroutineScope

Represents a web socket session between two peers

WebSocketWriter

class WebSocketWriter : CoroutineScope

Class that processes written outgoing Websocket Frame, serializes them and writes the bits into the writeChannel.

Annotations

WebSocketInternalAPI

annotation class WebSocketInternalAPI

API marked with this annotation is internal and not intended to be used outside of ktor It is not recommended to use it as it may be changed in the future without notice or it may be too low-level so could damage your data.

Extensions for External Classes

kotlinx.coroutines.CoroutineScope

Properties

pingInterval

var DefaultWebSocketServerSession.pingInterval: Duration?

Ping interval or null to disable pinger. Please note that pongs will be handled despite of this setting.

timeout

var DefaultWebSocketServerSession.timeout: Duration

A timeout to wait for pong reply to ping otherwise the session will be terminated immediately. It doesn’t have any effect if pingInterval is null (pinger is disabled).

Functions

DefaultWebSocketSession

expect fun DefaultWebSocketSession(
    session: WebSocketSession,
    pingInterval: Long,
    timeoutMillis: Long
): DefaultWebSocketSession

Create DefaultWebSocketSession from session.

close

suspend fun WebSocketSession.close(reason: CloseReason): Unit

Send a close frame with the specified reason. May suspend if outgoing channel is full or may throw an exception if it is already closed. The specified reason could be ignored if there was already close frame sent (for example in reply to a peer close frame).

pinger

fun pinger(
    session: WebSocketSession,
    period: Duration,
    timeout: Duration,
    out: SendChannel<Frame>,
    pool: ObjectPool<ByteBuffer> = KtorDefaultPool
): SendChannel<Pong>

ponger

fun ponger(
    session: WebSocketSession,
    pool: ObjectPool<ByteBuffer> = KtorDefaultPool
): SendChannel<Ping>

readBytes

fun Frame.readBytes(): ByteArray

Read binary content from a frame. For fragmented frames only returns this fragment.

readReason

fun Close.readReason(): CloseReason?

Read close reason from close frame or null if no close reason provided

readText

fun Text.readText(): String

Read text content from text frame. Shouldn’t be used for fragmented frames: such frames need to be reassembled first

run

suspend fun DefaultWebSocketSession.run(
    handler: suspend DefaultWebSocketSession.() -> Unit
): Unit

send

suspend fun WebSocketSession.send(content: String): Unit

Enqueues a text frame for sending with the specified content.

suspend fun WebSocketSession.send(content: ByteArray): Unit

Enqueues a final binary frame for sending with the specified content.

start

suspend fun RawWebSocket.start(
    handler: suspend WebSocketSession.() -> Unit
): Unit