websocket自动关闭连接 [英] websocket closing connection automatically
问题描述
我正在用Java构建一个基于jetty的嵌入式websocket服务器的应用程序。客户端是谷歌浏览器中默认的websocket实现。只有在连接关闭一段时间后,服务器和客户端之间没有传输时,所有事情都可以正常工作。
我不确定谁在关闭连接:jetty服务器或chrome浏览器。
我想这个解决方案是每x秒,但我打开更好的解决方案。
所以......我的问题是:
-
这是websocket协议所要求的,在这种情况下,chrome浏览器正在关闭我的连接?
这是更多的码头相关,并与websocket协议有关或多或少?在这种情况下,我该如何在码头中禁用此功能? -
是否还有其他问题? >
感谢
Is this something that the websocket protocol requires and in this case the chrome browser is closing my connection?
Is this something that is more jetty related and has more or less to do with the websocket protocol? In this case how do I disable this in jetty?
Is there another problem??
更新:即使我发送1封邮件/秒仍然关闭连接
在回答你的第三个问题时:你的客户希望能够处理暂时的网络问题,例如假设用户在休眠的会议之间关闭笔记本电脑,或者网络暂时关闭。
解决方案是听<
function setupWebSocket(){
this.ws = new WebSocket('wss:// host:port / path');
this.ws.onerror = ...;
this.ws.onopen = ...;
this.ws.onmessage = ...;
this.ws.onclose = function(){
setTimeout(setupWebSocket,1000);
};
}
I'm building an application in java that has an embedded websocket server based on jetty. The client is the default websocket implementation in google chrome. Everything is working ok, only if there is no transfer between server and client after a certain time the connection is closed. I'm not sure who's closing the connection: the jetty server or the chrome browser.
The solution to this I think is to send a message every x seconds, but I'm opened to better solutions.
SO... my questions are:
Thanks
UPDATE: even if I send 1 message/second still the connection is closed
In answer to your third question: your client wants to be able to cope with temporary network problems anyway, e.g. let's say the user closes their laptop between meetings which hibernates it, or the network simply goes down temporarily.
The solution is to listen to onclose
events on the web socket client and when they occur, set a client side timeout to re-open the connection, say in a second:
function setupWebSocket(){
this.ws = new WebSocket('wss://host:port/path');
this.ws.onerror = ...;
this.ws.onopen = ...;
this.ws.onmessage = ...;
this.ws.onclose = function(){
setTimeout(setupWebSocket, 1000);
};
}
这篇关于websocket自动关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!