ktor-websockets / io.ktor.websocket / WebSockets

WebSockets

class WebSockets : CoroutineScope

WebSockets support feature. It is required to be installed first before binding any websocket endpoints

install(WebSockets)

install(Routing) {
    webSocket("/ws") {
         incoming.consumeForEach { ... }
    }
}

Parameters

pingIntervalMillis - duration between pings or null to disable pings.

timeoutMillis - write/ping timeout after that a connection will be closed.

maxFrameSize - maximum frame that could be received or sent.

masking - whether masking need to be enabled (useful for security).

extensionsConfig - is configuration for WebSocket extensions.

Types

Feature

companion object Feature : ApplicationFeature<Application, WebSocketOptions, WebSockets>

Feature installation object.

WebSocketOptions

class WebSocketOptions

Websockets configuration options

Constructors

<init>

WebSockets(pingIntervalMillis: Long, timeoutMillis: Long, maxFrameSize: Long, masking: Boolean)WebSockets(pingIntervalMillis: Long, timeoutMillis: Long, maxFrameSize: Long, masking: Boolean, extensionsConfig: WebSocketExtensionsConfig)

WebSockets support feature. It is required to be installed first before binding any websocket endpoints

Properties

coroutineContext

val coroutineContext: <ERROR CLASS>

extensionsConfig

val extensionsConfig: WebSocketExtensionsConfig

is configuration for WebSocket extensions.

masking

val masking: Boolean

whether masking need to be enabled (useful for security).

maxFrameSize

val maxFrameSize: Long

maximum frame that could be received or sent.

pingIntervalMillis

val pingIntervalMillis: Long

duration between pings or null to disable pings.

timeoutMillis

val timeoutMillis: Long

write/ping timeout after that a connection will be closed.

Companion Object Properties

EXTENSIONS_KEY

val EXTENSIONS_KEY: AttributeKey<List<WebSocketExtension<*>>>

Key for saving configured WebSocket extensions for the specific call.

key

val key: AttributeKey<WebSockets>

Companion Object Functions

install

fun install(pipeline: Application, configure: WebSocketOptions.() -> Unit): WebSockets

Extension Properties

pingInterval

val WebSockets.pingInterval: Duration?

timeout

val WebSockets.timeout: Duration

Extension Functions

pinger

fun CoroutineScope.pinger(outgoing: SendChannel<Frame>, period: Duration, timeout: Duration, pool: ObjectPool<ByteBuffer> = KtorDefaultPool): SendChannel<Pong>

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

pinger

fun CoroutineScope.pinger(outgoing: SendChannel<Frame>, period: <ERROR CLASS>, timeout: <ERROR CLASS>, pool: ObjectPool<ByteBuffer> = KtorDefaultPool): SendChannel<Pong>

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