gpt4 book ai didi

java - WebSocket session 在一段时间未接收到输入后关闭

转载 作者:搜寻专家 更新时间:2023-10-30 23:02:39 24 4
gpt4 key购买 nike

所以我有一个从服务器读取信息的 Websocket session 。但是,如果我完全切断它正在接收的信息,大约一分钟后它将停止接收新信息,并且不会做任何事情,即使服务器的输出重新打开也是如此。

我以为是WebSocketContainer方法

setDefaultMaxSessionIdleTimeout(Long time)

会解决我的问题,所以我将其设置为

container.setDefaultMaxSessionIdleTimeout(86400000L);

我认为这意味着它将继续运行长达 1 天的不活动状态。

然而事实并非如此,它会在闲置一分钟后停止。下面是我正在使用的代码,也许有人可以让我知道我做错了什么:

public void run(String... args) throws Exception {
log.info("Starting...");
log.info("-- API URL: {}", apiUrl);
log.info("-- API Token: {}", apiToken);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.setDefaultMaxSessionIdleTimeout(86400000L);
ClientEndpointConfig config = ClientEndpointConfig
.Builder.create()
.configurator(new CustomConfigurator(apiToken))
.build();
try {
session = container.connectToServer(ConsumerClient.class, config, URI.create(apiUrl));
} catch (DeploymentException de) {
log.error("Failed to connect - DeploymentException:", de);
} catch (IOException ioe) {
log.error("IOException:", ioe);
}
if (this.session == null) {
throw new RuntimeException("Unable to connect to endpoint.");
}

log.info("Max Idle Timeout: " + session.getMaxIdleTimeout());

log.info("Connected.");
log.info("Type \"exit\" to cancel.");
log.info("...Waiting for data...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
try {
do {
input = br.readLine();
if (!input.equals("exit")) {
this.session.getBasicRemote().sendText(input);
}
} while (!input.equals("exit"));
} catch (IOException e) {
log.error("IOException:", e);
}
}

我是 websockets 的新手,所以我可能完全误解了一些东西,但我希望有人能够指出我正确的方向。提前致谢!

最佳答案

你能试试吗setMaxIdleTimeout(0)session 上?

根据 setDefaultMaxSessionIdleTimeout 文档:

The value may be overridden on a per session basis using Session.setMaxIdleTimeout(long)

关于java - WebSocket session 在一段时间未接收到输入后关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31121866/

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