完美解决spring websocket自动断开连接再创建引发的问题
问题:由于 web session 超时时间为 30 分钟,如用户在 web session 规定时间内没有退出系统,但由于其它原因 用户却断开的 websocket 的连接,如果用户还要聊天或是其它 websocket 方面的操作,那么就只能重新连接...
看代码:
var socket; var $ = function() { return document.getElementById(arguments[0]); } var log = function(msg) { $("log").innerHTML ? $("log").innerHTML += "<br />" + 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(); }
相关推荐
jonychen 2020-04-22
shufen0 2020-04-14
guozewei0 2020-03-06
柳木木的IT 2020-11-04
joynet00 2020-09-23
wenf00 2020-09-14
蓝色深海 2020-08-16
wuychn 2020-08-16
取个好名字真难 2020-08-06
darylove 2020-06-26
shufen0 2020-06-20
Lovexinyang 2020-06-14
WangBowen 2020-06-14
firejq 2020-06-14
hjhmpl 2020-06-14
水痕 2020-06-07
guozewei0 2020-06-06
woniyu 2020-06-02