You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.0 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

var MYWS = null;
var stompClient = null;
var WSReconnectNum = 0;
var WSReconnectNumMax = 500;
var WSReconnectT = null;
var MSGDiv = "div_mqttstate";
var WSSubscribe = "/exchange/exchanges_push_cz/rk_pushmsg";
//服务器的地址
var WS_OBJ = {};
var WSheaders = {};
var WSConnectFlag = false;
function startWebSocket(_wsuri, _user, _password) {
WSConnectFlag = false;
WS_OBJ = {uri: _wsuri, user: _user, password: _password};
// 首先判断是否 支持 WebSocket
if ('WebSocket' in window) {
// MYWS = new WebSocket(WS_OBJ.uri);
MYWS = new ReconnectingWebSocket(WS_OBJ.uri);
} else if ('MozWebSocket' in window) {
MYWS = new MozWebSocket(WS_OBJ.uri);
} else {
MYWS = new SockJS(WS_OBJ.uri);
}
stompClient = Stomp.over(MYWS);
stompClient.heartbeat.outgoing=10000;//若使用stomp1.1默认开启心跳这里设置心跳间隔为20秒
stompClient.heartbeat.incoming=10000;//客户端不从服务端接收心跳包
WSheaders = {
login: _user,
passcode: _password,
"host": "/gt",
// additional header
'client-id': 'MC_' + getRandomDate()
};
stompClient.connect(WSheaders, on_connect, on_error);
}
function on_connect(e) {
var date = new Date();
//console.log("connected to " + WS_OBJ.uri+"开始时间:"+getTimeStrfomart(date));
WSConnectFlag = true;
setWSState();
if (WSReconnectT != null) {
clearInterval(WSReconnectT);
WSReconnectT == null
}
stompClient.subscribe(WSSubscribe, onMessageArrived, onMsgFailed);
// try {
// setTimeout(function () {
// $("#div_main")[0].contentWindow.showMsg("", "成功连接消息服务器");//成功连接消息服务器 20210910 消息服务器断开后也能重新 不会在断开不连了 所以前端页面上不需要显示 暂时注释掉
// }, 2000);
// } catch (e) {
// }
}
function on_error(e) {
var date = new Date();
//console.log("error:" + e+" 报错时间:"+getTimeStrfomart(date));
// try {
// setTimeout(function () {
// $("#div_main")[0].contentWindow.showMsgwebsoket("", "error_icon", "bg-dot");//error_icon 消息服务器断开 断开用图片显示
// }, 1000);
// } catch (e) {
// }
//重连
WSConnectFlag = false;
setWSState();
if (WSReconnectT == null) {
WSReconnectT = setInterval(function () {
startWebSocket(WS_OBJ.uri, WS_OBJ.user, WS_OBJ.password);
}, 10000);
}
}
function getTimeStrfomart(_dateObj, _showSend) {
let _timeStr = "";
try {
if (_dateObj != null) {
var month = _dateObj.getMonth() + 1 < 10 ? "0" + (_dateObj.getMonth() + 1) : _dateObj.getMonth() + 1;
var date = _dateObj.getDate() < 10 ? "0" + _dateObj.getDate() : _dateObj.getDate();
_timeStr += (" " + _dateObj.getFullYear() + "-" + month + "-" +date + " ");
_timeStr += (pad(_dateObj.getHours(), 2) + ":" + pad(_dateObj.getMinutes(), 2) + ":" + pad(_dateObj.getSeconds(), 2));
}
} catch (e) {
}
return _timeStr;
}
function reConnectWS() {
if (WSReconnectNumMax >= WSReconnectNum) {
WSReconnectNum++;
sock = new WebSocket(WS_OBJ.uri);
}
}
//消息接收
function onMessageArrived(message) {
var date = new Date();
//console.log("onMessageArrived ----"+"消息接收开始时间:"+getTimeStrfomart(date));
try {
//console.log("message:" + message);
MessageToFrame(JSON.parse(decodeURIComponent(message.body)));
} catch (e) {
console.log("error:" + e);
}
}
function onMsgFailed() {
console.log("error:" + e);
}
function send(_topic, _header, _msg) {
var msg = document.getElementById('message').value;
stompClient.send(_topic, _header, _msg);
}
function setWSState() {
$("#" + MSGDiv).removeClass("text-main icon-paw text-dot icon-frown-o");
if (WSConnectFlag) {
$("#" + MSGDiv).addClass("text-main icon-paw");
$("#" + MSGDiv).attr("title", "已上线");
} else {
$("#" + MSGDiv).addClass("text-dot icon-frown-o");
$("#" + MSGDiv).attr("title", "未连接服务器");
}
}
function MessageToFrame(_msg) {
var date = new Date();
try {
if (_msg.mqType == 4) {
let _cctvParas = {
ip: "192.168.2.64",
port: 80,
username: "admin",
password: "Gt123456",
arg: 53
}
startCCTVAuto(_cctvParas);
} else {
$("#div_main")[0].contentWindow.showMQMsg(_msg);//菜单左侧围栏报警通知接收
//console.log("MessageToFrame ------"+"目标消息接收开始时间:"+getTimeStrfomart(date));
//console.log("MessageToFrame ------"+"目标消息接收数据:"+JSON.stringify(_msg.data));
$("#div_main")[0].contentWindow.onMQTTmessage(_msg);//目标消息接收
//console.log("目标消息接收:" + JSON.stringify(_msg));
}
} catch (e) {
}
}