Spring WebSocket超时设置
我正在使用Spring websocket支持.我的问题是如何设置websocket连接超时.现在,几分钟后,连接将自动关闭.我希望连接永远不会关闭.
I'm using the Spring websocket support. My question is how to set the websocket connection timeout. Now the connection is closed automatically after several minutes. I want the connection never to be closed.
这是我的网络套接字处理程序:
Here is my websocket handler:
public class MyHandler implements WebSocketHandler {
private Logger logger = LoggerFactory.getLogger(this.getClass());
class MyTimerTask extends TimerTask {
private WebSocketSession session;
public MyTimerTask(WebSocketSession session) {
this.session = session;
}
@Override
public void run() {
try {
String msg = ((int)(Math.random()*50)) + "";
this.session.sendMessage(new TextMessage(msg.toString()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Autowired
private UserDao userDao;
@Autowired
private JdbcDaoImpl jdbcDaoImpl;
private Timer timer;
@Override
public void afterConnectionEstablished(WebSocketSession session)
throws Exception {
System.out.println("websocket????");
timer = new Timer();
timer.schedule(new MyTimerTask(session), 0, 1000);
logger.info("logger connection");
}
@Override
public void handleMessage(WebSocketSession session,
WebSocketMessage<?> message) throws Exception { }
@Override
public void handleTransportError(WebSocketSession session,
Throwable exception) throws Exception { }
@Override
public void afterConnectionClosed(WebSocketSession session,
CloseStatus closeStatus) throws Exception {
System.out.println("websocket????");
timer.cancel();
}
@Override
public boolean supportsPartialMessages() {
return false;
}
}
我的websocket配置:
my websocket config:
<websocket:handlers>
<websocket:mapping path="/myHandler" handler="myHandler"/>
</websocket:handlers>
<bean id="myHandler" class="com.sdp.websocket.MyHandler"/>
和javascript客户端:
and javascript client:
var webserver = 'ws://localhost:8080/authtest/myHandler';
var websocket = new WebSocket(webserver);
websocket.onopen = function (evt) { onOpen(evt) };
websocket.onclose = function (evt) { onClose(evt) };
websocket.onmessage = function (evt) { onMessage(evt) };
websocket.onerror = function (evt) { onError(evt) };
function onOpen(evt) {
console.log("Connected to WebSocket server.");
}
function onClose(evt) {
console.log("Disconnected");
}
function onMessage(evt) {
console.log('Retrieved data from server: ' + evt.data);
}
function onError(evt) {
console.log('Error occured: ' + evt.data);
}
debugger;
function sendMsg(){
websocket.send("{msg:'hello'}");
}
Websocket保持打开状态,直到服务器或客户端决定关闭它为止.但是,WebSocket会受到两个超时的影响:
The websocket stays opened until either the server or the client decide to close it. However, websockets are affected by two timeouts:
- HTTP会话超时;
- 代理连接超时;
如果您的客户端和服务器之间只有Websocket连接,并且没有通过HTTP与AJAX交互或未请求其他页面,则HTTP会话将过期,并且某些服务器决定将其与Websocket一起无效(Tomcat7有一个可以做到这一点的错误).其他一些服务器则不这样做,因为它们看到websocket上有活动.请参阅此处进行扩展讨论:需要一些解析或澄清以了解HttpSession的持续时间和方式访问时间得到更新.
If all you have between your client and your server is a websocket connection, and you don't interact over HTTP with AJAX or requesting other pages, the HTTP session expires and some servers decide to invalidate it along with the websocket (Tomcat7 had a bug that did just that). Some other servers don't do that because they see there is activity on the websocket. See here for an extended discussion: Need some resolution or clarification for how and when HttpSession last access time gets updated.
另一个超时与代理有关.他们看到了连接,并且如果长时间没有任何活动,他们只是切断了连接,因为他们认为连接已挂起.为了解决这个问题,虽然您没有发送实际的应用程序数据,但需要不时发出心跳信号或ping/pong消息,以使代理知道连接仍然可以.
The other timeout is with proxies. They see the connection and if there is no activity on the wire for a longer period of time, they just cut it because they think it hanged. To address this, while you don't send actual application data, you need to have a heartbeat or a ping/pong of messages from time to time to let the proxy know that the connection is still OK.
其他问题也可能会介入,例如浏览器对websocket的错误支持,网络的配置方式,防火墙规则等.
Other issues might also intervene, like a buggy browser support for websocket, how your network is configured, firewall rules, etc.
有关Spring中可用的超时选项,请参阅websocket文档:配置WebSocket引擎.
For available timeout options in Spring see the websocket documentation: Configuring the WebSocket Engine.