75 lines
2.2 KiB
JavaScript
75 lines
2.2 KiB
JavaScript
|
|
// WebSocket接收消息推送
|
|||
|
|
var ws_url="ws://"+tailIp+"/vmw/webSocket"
|
|||
|
|
|
|||
|
|
function openSocket(){
|
|||
|
|
$.ajax({
|
|||
|
|
type : 'post',
|
|||
|
|
url : dataUrl + 'abnormal/getAbnormalData',
|
|||
|
|
success : function(data) {
|
|||
|
|
console.log("数据",data);
|
|||
|
|
$("#outText").text(data[0].name+"于"+data[0].time+data[0].content);
|
|||
|
|
// location.href = ctxPath + '/index.html';
|
|||
|
|
},
|
|||
|
|
error : function(xhr, textStatus, errorThrown) {
|
|||
|
|
var msg = xhr.responseText;
|
|||
|
|
var response = JSON.parse(msg);
|
|||
|
|
var code = response.code;
|
|||
|
|
var message = response.message;
|
|||
|
|
if (code == 401) {
|
|||
|
|
localStorage.removeItem("token");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function sleep(numberMillis) {
|
|||
|
|
var now = new Date();
|
|||
|
|
var exitTime = now.getTime() + numberMillis;
|
|||
|
|
while (true) {
|
|||
|
|
now = new Date();
|
|||
|
|
if (now.getTime() > exitTime)
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function oo(userId) {
|
|||
|
|
console.log(userId);
|
|||
|
|
if (typeof WebSocket == "undefined") {
|
|||
|
|
console.log("您的浏览器不支持WebSocket");
|
|||
|
|
} else {
|
|||
|
|
var socketUrl = ws_url+"/"+userId;
|
|||
|
|
if (this.socket != null) {
|
|||
|
|
this.socket.close();
|
|||
|
|
this.socket = null;
|
|||
|
|
}
|
|||
|
|
try{
|
|||
|
|
this.socket = new WebSocket(socketUrl);
|
|||
|
|
}catch (e) {
|
|||
|
|
console.log(e.message)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 打开事件
|
|||
|
|
this.socket.onopen = function () {
|
|||
|
|
console.log("websocket已打开");
|
|||
|
|
};
|
|||
|
|
// 获得消息事件
|
|||
|
|
const _this = this
|
|||
|
|
this.socket.onmessage = function (msg) {
|
|||
|
|
$("#outText").text(msg.data);
|
|||
|
|
// console.log(msg.data);//输出从后台接收的信息:“发送测试信息”
|
|||
|
|
// _this.todoSomeThing(55)//接收后台发送的请求,主动调用相应的业务接口
|
|||
|
|
};
|
|||
|
|
// 关闭事件
|
|||
|
|
this.socket.onclose = function (e) {
|
|||
|
|
console.log("websocket已关闭");
|
|||
|
|
};
|
|||
|
|
// 发生了错误事件
|
|||
|
|
this.socket.onerror = function (e) {
|
|||
|
|
console.log(e.message)
|
|||
|
|
console.log(e)
|
|||
|
|
console.log('websocket发生了错误,错误: ')
|
|||
|
|
console.log(e)
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|