gpt4 book ai didi

websocket - Ktor CIO wss 套接字立即关闭

转载 作者:行者123 更新时间:2023-12-02 12:12:05 33 4
gpt4 key购买 nike

使用 ktor CIO ws 时,它按预期工作,但使用 wss 时,它会立即关闭。任何帮助是极大的赞赏。现在卡了一天。

这是我为 wss 获取的堆栈跟踪

kotlinx.coroutines.experimental.channels.ClosedReceiveChannelException: Channel was closed
at kotlinx.coroutines.experimental.channels.Closed.getReceiveException(AbstractChannel.kt:1067)
at kotlinx.coroutines.experimental.channels.AbstractChannel$ReceiveElement.resumeReceiveClosed(AbstractChannel.kt:907)
at kotlinx.coroutines.experimental.channels.AbstractSendChannel.helpClose(AbstractChannel.kt:317)
at kotlinx.coroutines.experimental.channels.AbstractSendChannel.close(AbstractChannel.kt:254)
at kotlinx.coroutines.experimental.channels.ChannelCoroutine.close(ChannelCoroutine.kt)
at kotlinx.coroutines.experimental.channels.SendChannel$DefaultImpls.close$default(Channel.kt:84)
at io.ktor.network.tls.TLSClientHandshake$input$1.doResume(TLSClientHandshake.kt:96)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resumeWithException(CoroutineImpl.kt:48)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resumeWithException(CoroutineImpl.kt:47)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:41)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:149)
at kotlinx.coroutines.experimental.io.internal.MutableDelegateContinuation.run(MutableDelegateContinuation.kt:14)
at io.ktor.network.util.IOCoroutineDispatcher$IODispatchedTask.run(IOCoroutineDispatcher.kt)
at io.ktor.network.util.IOCoroutineDispatcher$IOThread$run$1.doResume(IOCoroutineDispatcher.kt:73)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:149)
at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:13)
at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:140)
at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:70)
at kotlinx.coroutines.experimental.BuildersKt__BuildersKt.runBlocking(Builders.kt:46)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Unknown Source)
at io.ktor.network.util.IOCoroutineDispatcher$IOThread.run(IOCoroutineDispatcher.kt:68)

这是代码:
httpClient.ws(host = "echo.websocket.org") {
send(Frame.Text("Hello World"))

for (message in incoming.map { it as? Frame.Text }.filterNotNull()) {
println(message.readText())
}
}

httpClient.wss(host = "echo.websocket.org") {
send(Frame.Text("Hello World"))

for (message in incoming.map { it as? Frame.Text }.filterNotNull()) {
println(message.readText())
}
}

最佳答案

我遇到了安全 WebSockets 的问题,它已在 ktor 1.3.2 中得到修复.

我还想提一下 echo.websocket.org不会关闭连接,因此 for 循环将永远暂停(期待下一个 pong 消息)。

为避免这种情况,您可以重写此

for (message in incoming.map { it as? Frame.Text }.filterNotNull()) {
println(message.readText())
}

作为

val pong = incoming.receive() as Frame.Text
println(pong.readText())

调用 close()也应该没问题在发送第一条消息后明确显示,但目前不支持。我为此提交了单独的问题: https://github.com/ktorio/ktor/issues/1946

关于websocket - Ktor CIO wss 套接字立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52623482/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com