1、增加websocket保活
This commit is contained in:
parent
9d4a4d4c27
commit
9f570d9da1
@ -116,7 +116,12 @@
|
||||
|
||||
<!-- Web socket operations-->
|
||||
<script type="text/javascript">
|
||||
var intervalId;
|
||||
var websocket;
|
||||
var rxWin;
|
||||
|
||||
layui.$(function(){
|
||||
rxWin = layui.$('#rx_win');
|
||||
if(typeof(WebSocket) == "undefined"){
|
||||
console.log("WebSocket not support for this WebBrowser!")
|
||||
}
|
||||
@ -140,22 +145,28 @@
|
||||
//建立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); // keepalive
|
||||
|
||||
}
|
||||
websocket.onerror = function (){
|
||||
console.log("Socket error");
|
||||
layui.layer.alert("Open web socket failed!");
|
||||
}
|
||||
websocket.onclose = function (){
|
||||
rxWin.append("websocket closed!")
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
//接收信息
|
||||
websocket.onmessage = function (event) {
|
||||
console.log(event.data);
|
||||
var rxWin = layui.$('#rx_win');
|
||||
rxWin.val(rxWin.val()+event.data + "\r\n");
|
||||
var obj = document.getElementById("rx_win");
|
||||
rxWin.scrollTop = rxWin.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user