WebSocket

预计阅读时间: 1 分钟

本特性在构件 io.ktor:ktor-client-websockets:$ktor_version,io.ktor:ktor-client-cio:$ktor_version,io.ktor:ktor-client-js:$ktor_version,io.ktor:ktor-client-okhttp:$ktor_version 中的 io.ktor.client.features.websocket.WebSockets 类中定义
dependencies { implementation "io.ktor:ktor-client-websockets:$ktor_version" implementation "io.ktor:ktor-client-cio:$ktor_version" implementation "io.ktor:ktor-client-js:$ktor_version" implementation "io.ktor:ktor-client-okhttp:$ktor_version" }
dependencies { implementation("io.ktor:ktor-client-websockets:$ktor_version") implementation("io.ktor:ktor-client-cio:$ktor_version") implementation("io.ktor:ktor-client-js:$ktor_version") implementation("io.ktor:ktor-client-okhttp:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-websockets</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-cio</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-js</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-okhttp</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>

Ktor 为 CIO、OkHttp、Js 这几个引擎提供了 WebSocket 客户端。如需了解服务端的相关信息,请参见这一节

一旦连接后,客户端与服务器的 WebSocket 就共享相同的 WebSocketSession 接口进行通信。

创建支持 WebSocket 的 HTTP 客户端的基本用法非常简单:

val client = HttpClient {
    install(WebSockets)
}

一旦创建后就可以执行请求,启动一个 WebSocketSession

client.ws(
    method = HttpMethod.Get,
    host = "127.0.0.1",
    port = 8080, path = "/route/path/to/ws"
) { // this: DefaultClientWebSocketSession

    // 发送文本帧。
    send("Hello, Text frame")

    // 发送文本帧。
    send(Frame.Text("Hello World"))

    // 发送二进制帧。
    send(Frame.Binary(……))

    // 接收帧。
    val frame = incoming.receive()
    when (frame) {
        is Frame.Text -> println(frame.readText())
        is Frame.Binary -> println(frame.readBytes())
    }
}

关于 WebSocketSession 的更多信息请参见 WebSocketSession 页及其 API 参考