Package io.ktor.http.cio.websocket

Types

Link copied to clipboard
data class CloseReason(code: Short, message: String)

Websocket close reason

Link copied to clipboard
expect interface DefaultWebSocketSession : WebSocketSession

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

actual interface DefaultWebSocketSession : WebSocketSession

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

actual interface DefaultWebSocketSession : WebSocketSession

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

Link copied to clipboard
class DefaultWebSocketSessionImpl(raw: WebSocketSession, pingInterval: Long, timeoutMillis: Long, pool: ObjectPool<ByteBuffer>) : DefaultWebSocketSession, WebSocketSession

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

Link copied to clipboard
annotation class ExperimentalWebSocketExtensionApi

WebSocket extensions API is experimental according to KTOR-688 To get more information about Ktor experimental guarantees consult with KTOR-1035.

Link copied to clipboard
expect sealed class Frame

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

actual sealed class Frame

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

actual sealed class Frame

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

Link copied to clipboard
class FrameParser
Link copied to clipboard
enum FrameType : Enum<FrameType>

Frame types enum

Link copied to clipboard
class RawWebSocket(input: ByteReadChannel, output: ByteWriteChannel, maxFrameSize: Long, masking: Boolean, coroutineContext: CoroutineContext, pool: ObjectPool<ByteBuffer>) : WebSocketSession

Represents a RAW web socket session

Link copied to clipboard
class Serializer
Link copied to clipboard
class SimpleFrameCollector
Link copied to clipboard

Compress and decompress WebSocket frames to reduce amount of transferred bytes.

Link copied to clipboard
interface WebSocketExtension<ConfigType : Any>

WebSocket extension instance. This instance is created for each WebSocket request, for every installed extension by WebSocketExtensionFactory.

Link copied to clipboard

Factory that defines WebSocket extension. The factory is used in pair with WebSocketExtensionsConfig.install method to install WebSocket extension in client or server.

Link copied to clipboard
class WebSocketExtensionHeader(name: String, parameters: List<String>)

Parsed Sec-WebSocket-Accept header item representation.

Link copied to clipboard
class WebSocketExtensionsConfig

Extensions configuration for WebSocket client and server features.

Link copied to clipboard
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.

Link copied to clipboard
class WebSocketReader(byteChannel: ByteReadChannel, coroutineContext: CoroutineContext, maxFrameSize: Long, pool: ObjectPool<ByteBuffer>) : CoroutineScope

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

Link copied to clipboard
expect interface WebSocketSession : CoroutineScope

Represents a web socket session between two peers

actual interface WebSocketSession : CoroutineScope

Represents a web socket session between two peers

actual interface WebSocketSession : CoroutineScope

Represents a web socket session between two peers

Link copied to clipboard
class WebSocketWriter(writeChannel: ByteWriteChannel, coroutineContext: CoroutineContext, masking: Boolean, pool: ObjectPool<ByteBuffer>) : CoroutineScope

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

Functions

Link copied to clipboard
suspend fun WebSocketSession.close(reason: CloseReason = CloseReason(CloseReason.Codes.NORMAL, ""))

Send a close frame with the specified reason. May suspend if outgoing channel is full. The specified reason could be ignored if there was already close frame sent (for example in reply to a peer close frame). It also may do nothing when a session or an outgoing channel is already closed due to any reason.

suspend fun WebSocketSession.close(cause: Throwable?)

Closes with reason depending on cause or normally if cause is null. This is going to be removed. Close with a particular reason or terminate instead.

Link copied to clipboard
suspend fun WebSocketSession.closeExceptionally(cause: Throwable)

Closes session with normal or error close reason, depending on whether cause is cancellation or not.

Link copied to clipboard
expect fun DefaultWebSocketSession(session: WebSocketSession, pingInterval: Long, timeoutMillis: Long): DefaultWebSocketSession

Create DefaultWebSocketSession from session.

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

Create DefaultWebSocketSession from session.

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

Create DefaultWebSocketSession from session.

Link copied to clipboard
fun <T : WebSocketExtension<*>> WebSocketSession.extension(extension: WebSocketExtensionFactory<*, T>): T

Find the extensions using WebSocketExtensionFactory.

Link copied to clipboard
fun <T : WebSocketExtension<*>> WebSocketSession.extensionOrNull(extension: WebSocketExtensionFactory<*, T>): T?

Search the extensions using WebSocketExtensionFactory.

Link copied to clipboard
fun parseWebSocketExtensions(value: String): List<WebSocketExtensionHeader>

Parse Sec-WebSocket-Accept header.

Link copied to clipboard
fun CoroutineScope.pinger(outgoing: SendChannel<Frame>, periodMillis: Long, timeoutMillis: Long, pool: ObjectPool<ByteBuffer> = KtorDefaultPool): SendChannel<Frame.Pong>

Launch pinger coroutine on CoroutineScope that is sending ping every specified periodMillis to outgoing channel, waiting for and verifying client's pong frames. It is also handling timeoutMillis and sending timeout close frame

Link copied to clipboard
fun CoroutineScope.ponger(outgoing: SendChannel<Frame.Pong>, pool: ObjectPool<ByteBuffer> = KtorDefaultPool): SendChannel<Frame.Ping>

Launch a ponger actor job on the CoroutineScope sending pongs to outgoing channel. It is acting for every client's ping frame and replying with corresponding pong

Link copied to clipboard
fun Frame.readBytes(): ByteArray

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

Link copied to clipboard
fun Frame.Close.readReason(): CloseReason?

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

Link copied to clipboard
@JvmName(name = "readReason")
fun Frame.Close.readReason0(): CloseReason?
Link copied to clipboard
fun Frame.Text.readText(): String

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

Link copied to clipboard
suspend fun DefaultWebSocketSession.run(handler: suspend DefaultWebSocketSession.() -> Unit)
Link copied to clipboard
suspend fun WebSocketSession.send(content: ByteArray)

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

suspend fun WebSocketSession.send(content: String)

Enqueues a text frame for sending with the specified content.

Link copied to clipboard
suspend fun RawWebSocket.start(handler: suspend WebSocketSession.() -> Unit)