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