gpt4 book ai didi

java - WebSocket 连接在一段时间后无法重新连接

转载 作者:行者123 更新时间:2023-12-02 01:47:55 26 4
gpt4 key购买 nike

我写了一些逻辑,代表近200个websocet连接同时进行交换。我使用第三方api,它基于org.eclipse.jetty.websocket.api。我有这个方法,我必须重写它。

try {
URI uri = new URI(websocketBaseUrl + url);
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setTrustAll(true);
WebSocketClient client = new WebSocketClient(sslContextFactory);
client.setMaxIdleTimeout(0);
client.start();
return client.connect(adapter, uri).get();
} catch (URISyntaxException e) {
throw new BinanceApiException("URL Syntax error: " + e.getMessage());
} catch (Throwable e) {
throw new BinanceApiException("Websocket error: " + e.getMessage());
}

我添加了setIdleTimeout,这样当我长时间没有收到信息时,连接就不会丢失。每天交换一次关闭连接,但 2,3 天有时 4 天它已重新连接。但最后我得到了这个:

java.nio.channels.ClosedChannelException: null at org.eclipse.jetty.io.WriteFlusher.onClose(WriteFlusher.java:507) at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onIncompleteFlush(SslConnection.java:527) at org.eclipse.jetty.io.AbstractEndPoint$2.onIncompleteFlush(AbstractEndPoint.java:54) at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:331) at org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:372) at org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.flush(FrameFlusher.java:153) at org.eclipse.jetty.websocket.common.io.FrameFlusher$Flusher.process(FrameFlusher.java:217) at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241) at org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:224) at org.eclipse.jetty.websocket.common.io.FrameFlusher.enqueue(FrameFlusher.java:382) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.outgoingFrame(AbstractWebSocketConnection.java:614) at org.eclipse.jetty.websocket.client.io.WebSocketClientConnection.outgoingFrame(WebSocketClientConnection.java:72) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onConnectionStateChange(AbstractWebSocketConnection.java:473) at org.eclipse.jetty.websocket.common.io.IOState.notifyStateListeners(IOState.java:184) at org.eclipse.jetty.websocket.common.io.IOState.onReadFailure(IOState.java:498) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:666) at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:511) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104) at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:289) at org.eclipse.jetty.io.ssl.SslConnection$3.succeeded(SslConnection.java:149) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104) at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:243) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:679) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:597) at java.lang.Thread.run(Thread.java:748)

我在 stackoverflow 上找到这个问题但我看不到明确的答案。请帮忙。提前致谢。

最佳答案

如果您希望连接在空闲时保持打开状态,您应该这样配置您的客户端:

client.setMaxIdleTimeout(Long.MAX_VALUE);

将 maxIdleTimeout 设置为 0 会产生相反的效果:连接空闲时立即关闭连接。

关于java - WebSocket 连接在一段时间后无法重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53473604/

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