Hi all, I am failing to use WebSocket in Kotlin/WASM environment.
When using the ktor library, normal server api calls were successful, but websocket throws NullpointExeption.
dependencies {
...
implementation("io.ktor:ktor-client-core-wasm:2.3.3-wasm0")
implementation("io.ktor:ktor-client-websockets-wasm:2.3.3-wasm0")
implementation("io.ktor:ktor-serialization-kotlinx-json-wasm:2.3.3-wasm0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-wasm:1.7.0-RC-wasm0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-wasm:1.5.1-wasm0")
....
}
// test code
suspend fun testWebSocket() {
try {
client.webSocket("ws://localhost:8080") {
webSocketSession = this
var autoMessageIndex = 0
while (true) {
send("message${autoMessageIndex++}")
val othersMessage = incoming.receive() as? Frame.Text
println(othersMessage?.readText())
delay(1000)
}
}
} catch (ex: Exception) {
val closeReason = webSocketSession?.closeReason?.await()
println("testWebSocket(-) closeReason:$closeReason ex:$ex")
closeTestWebSocket()
}
}