1、增加websocket保活
This commit is contained in:
parent
8a4fe22d0f
commit
22894c69c8
@ -38,7 +38,7 @@ public class WebSocketServer {
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(String msg, Session session){
|
||||
log.info("received msg from "+session.getBasicRemote());
|
||||
log.info("websocket: "+msg);
|
||||
}
|
||||
|
||||
@OnError
|
||||
|
||||
@ -116,7 +116,12 @@
|
||||
|
||||
<!-- Web socket operations-->
|
||||
<script type="text/javascript">
|
||||
var websocket;
|
||||
var intervalId;
|
||||
var rxWin;
|
||||
|
||||
layui.$(function(){
|
||||
rxWin = layui.$('#rx_win');
|
||||
if(typeof(WebSocket) == "undefined"){
|
||||
console.log("WebSocket not support for this WebBrowser!")
|
||||
}
|
||||
@ -129,10 +134,8 @@
|
||||
function connectWebSocket() {
|
||||
//获取当前网址,如: http://localhost:8080/ems/Pages/Basic/Person.jsp
|
||||
var curPath = window.document.location.href;
|
||||
console.log(curPath);
|
||||
//获取主机地址之后的目录,如: /ems/Pages/Basic/Person.jsp
|
||||
var pathName = window.document.location.pathname;
|
||||
console.log(pathName);
|
||||
var pos = curPath.indexOf(pathName);
|
||||
//获取主机地址,如: http://localhost:8080
|
||||
var basePath = curPath.substring(0, pos);
|
||||
@ -140,24 +143,33 @@
|
||||
//建立webSocket连接
|
||||
var webSocktPath;
|
||||
webSocktPath = (basePath+"/websocket").replace("http","ws");
|
||||
var websocket = new WebSocket(webSocktPath);
|
||||
websocket = new WebSocket(webSocktPath);
|
||||
|
||||
//打开webSokcet连接时,回调该函数
|
||||
websocket.onopen = function () {
|
||||
console.log("Socket open");
|
||||
intervalId = setInterval(function() {
|
||||
websocket.send("hello");
|
||||
}, 60000);// keep alive
|
||||
}
|
||||
websocket.onerror = function (){
|
||||
console.log("Socket error");
|
||||
layui.layer.alert("Open web socket failed!");
|
||||
}
|
||||
websocket.onclose = function (){
|
||||
rxWin.append("websocket closed!\r\n");
|
||||
// 如果需要停止重复执行的定时任务
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
//接收信息
|
||||
websocket.onmessage = function (event) {
|
||||
var rxWin = layui.$('#rx_win');
|
||||
rxWin.val(rxWin.val()+event.data + "\r\n");
|
||||
var obj = document.getElementById("rx_win");
|
||||
obj.scrollTop = obj.scrollHeight;
|
||||
rxWin.append(event.data + "\r\n");
|
||||
rxWin.scrollTop = rxWin.scrollHeight;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user