问题:由于 web session 超时时间为 30 分钟,如用户在 web session 规定时间内没有退出系统,但由于其它原因 用户却断开的 websocket 的连接,如果用户还要聊天或是其它 websocket 方面的操作,那么就只能重新连接...
看代码:
" + msg : $("log").innerHTML = msg; } var initWebSocket = function() { if (window.WebSocket) { socket = new WebSocket("ws://127.0.0.1:8080/websocket"); socket.onmessage = function(event) { var json = JSON.parse(event.data); document.getElementById("message").value = json.message; }; socket.onopen = function(event) { log("Web Socket opened!"); }; socket.onclose = function(event) { log("Web Socket closed."); }; socket.onerror = function(event) { log("Web Socket error."); }; } else { log("Your browser does not support Web Socket."); } } var send = function() { var data = function() { socket.send("{'send':1,'take':2,'message':" + Math.random() + "}"); }; if (socket.readyState !== 1) { socket.close(); initWebSocket(); setTimeout(function() { data(); },250); } else { data(); };}
window.onload = function() {
initWebSocket();
}