SparkJava Web 套接字将无法工作。每当我尝试使用 websocket 测试器连接到它时,在“ws://localhost:4567/echo”处,它都会收到“未定义”错误,并且永远不会连接,也不会调用任何 sout 或 printStackTrace。
@WebSocket
public class EchoWebSocket {
private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>();
@OnWebSocketConnect
public void connected(Session session) {
System.out.println("Client connected");
//sessions.add(session);
}
@OnWebSocketClose
public void closed(Session session, int statusCode, String reason) {
System.out.println("Client disconnected");
//sessions.remove(session);
}
@OnWebSocketMessage
public void message(Session session, String message) throws IOException {
System.out.println("Got: ");// + message); // Print message
//session.getRemote().sendString(message); // and send it back
}
@OnWebSocketError
public void throwError(Throwable error) {
error.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它
Spark.webSocket("/echo", …Run Code Online (Sandbox Code Playgroud) 使用 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)
Run Code Online (Sandbox Code Playgroud)
这是代码:
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 …Run Code Online (Sandbox Code Playgroud)