1、增加websocket保活
This commit is contained in:
parent
b99ac8e11b
commit
f3b036fd52
@ -68,21 +68,21 @@
|
|||||||
</div>
|
</div>
|
||||||
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
|
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
|
||||||
<script>
|
<script>
|
||||||
layui.use(['form'], function () {
|
layui.use(['form'], function () {
|
||||||
var form = layui.form
|
var form = layui.form
|
||||||
, $ = layui.$;
|
, $ = layui.$;
|
||||||
var rxWin = $("#rx_win");
|
var rxWin = $("#rx_win");
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化表单,要加上,不然刷新部分组件可能会不加载
|
* 初始化表单,要加上,不然刷新部分组件可能会不加载
|
||||||
*/
|
*/
|
||||||
form.render();
|
form.render();
|
||||||
|
|
||||||
//监听提交
|
//监听提交
|
||||||
form.on('submit(send_btn)', function (data) {
|
form.on('submit(send_btn)', function (data) {
|
||||||
//console.log((data.field));
|
//console.log((data.field));
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type:"POST",
|
type:"POST",
|
||||||
url:"/gnss/config_cmd",
|
url:"/gnss/config_cmd",
|
||||||
data: data.field,
|
data: data.field,
|
||||||
@ -104,24 +104,19 @@
|
|||||||
console.log("ajax error");
|
console.log("ajax error");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
form.on('submit(clear_btn)', function (data) {
|
form.on('submit(clear_btn)', function (data) {
|
||||||
rxWin.val("");
|
rxWin.val("");
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- Web socket operations-->
|
<!-- Web socket operations-->
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var websocket;
|
|
||||||
var intervalId;
|
|
||||||
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!")
|
||||||
}
|
}
|
||||||
@ -134,8 +129,10 @@
|
|||||||
function connectWebSocket() {
|
function connectWebSocket() {
|
||||||
//获取当前网址,如: http://localhost:8080/ems/Pages/Basic/Person.jsp
|
//获取当前网址,如: http://localhost:8080/ems/Pages/Basic/Person.jsp
|
||||||
var curPath = window.document.location.href;
|
var curPath = window.document.location.href;
|
||||||
|
console.log(curPath);
|
||||||
//获取主机地址之后的目录,如: /ems/Pages/Basic/Person.jsp
|
//获取主机地址之后的目录,如: /ems/Pages/Basic/Person.jsp
|
||||||
var pathName = window.document.location.pathname;
|
var pathName = window.document.location.pathname;
|
||||||
|
console.log(pathName);
|
||||||
var pos = curPath.indexOf(pathName);
|
var pos = curPath.indexOf(pathName);
|
||||||
//获取主机地址,如: http://localhost:8080
|
//获取主机地址,如: http://localhost:8080
|
||||||
var basePath = curPath.substring(0, pos);
|
var basePath = curPath.substring(0, pos);
|
||||||
@ -143,34 +140,24 @@
|
|||||||
//建立webSocket连接
|
//建立webSocket连接
|
||||||
var webSocktPath;
|
var webSocktPath;
|
||||||
webSocktPath = (basePath+"/websocket").replace("http","ws");
|
webSocktPath = (basePath+"/websocket").replace("http","ws");
|
||||||
websocket = new WebSocket(webSocktPath);
|
var 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);// keep alive
|
|
||||||
}
|
}
|
||||||
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.val("websocket closed!\r\n");
|
|
||||||
// 如果需要停止重复执行的定时任务
|
|
||||||
clearInterval(intervalId);
|
|
||||||
}
|
|
||||||
//接收信息
|
//接收信息
|
||||||
websocket.onmessage = function (event) {
|
websocket.onmessage = function (event) {
|
||||||
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");
|
||||||
rxWin.scrollTop = rxWin.scrollHeight;
|
var obj = document.getElementById("rx_win");
|
||||||
|
obj.scrollTop = obj.scrollHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user