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.
175 lines
5.5 KiB
JavaScript
175 lines
5.5 KiB
JavaScript
var MSGDiv = "";
|
|
var MQTTPre = "";
|
|
var MQTTOptions = {};
|
|
var MQTTClent = null;
|
|
var MQTTClientId = "";
|
|
var MQTTC2STopic = "";
|
|
var MQTTS2CTopic = "";
|
|
|
|
var MSGNewNum = 0;
|
|
var MQTTReconnectNum = 0;
|
|
var MQTTReconnectNumMax = 500;
|
|
var MQTTConnectFlag = false;
|
|
var MQTTReconnectT = null;
|
|
|
|
function setMqttState() {
|
|
// $("#" + MSGDiv).removeClass("text-main icon-paw text-dot icon-frown-o");
|
|
if (MQTTConnectFlag) {
|
|
$("#div_main")[0].contentWindow.showMsg("", "云台服务已上线");
|
|
// showH5Alert("setMqttState", "已上线");
|
|
} else {
|
|
$("#div_main")[0].contentWindow.showMsgwebsoket("", "yt_error_icon", "bg-dot");//云台服务已离线
|
|
//showH5Alert("setMqttState", "未连接服务器");
|
|
}
|
|
}
|
|
|
|
function startMQTT(_mqtt) {
|
|
//alert(JSON.stringify(_mqtt));
|
|
MQTTPre = "hkipc";
|
|
//mqtt
|
|
MQTTC2STopic = MQTTPre + "/c2b";
|
|
MQTTS2CTopic = MQTTPre + "/b2c";
|
|
MQTTClientId = MQTTPre + "WEB_" + getRandomDate();
|
|
MQTTClent = new Paho.Client(_mqtt.ip, parseInt(_mqtt.port),"/ws", MQTTClientId);
|
|
// var willMessage = new Paho.Message(JSON.stringify({
|
|
// clienttype: "web",
|
|
// ucid: _mqtt.uinstid + "|web",
|
|
// cid: _mqtt.uinstid + "|web",
|
|
// clientid: MQTTClientId,
|
|
// clientop: "close",
|
|
// token: ""
|
|
// }));
|
|
// willMessage.destinationName = "WILLTOPIC/" + _mqtt.uinstid + "/Web";
|
|
// willMessage.qos = 1;
|
|
// willMessage.retained = true;
|
|
MQTTOptions = {
|
|
invocationContext: {
|
|
host: _mqtt.ip,
|
|
port: _mqtt.port,
|
|
path: MQTTClent.path,
|
|
clientId: MQTTClientId
|
|
},
|
|
timeout: 60,
|
|
keepAliveInterval: 120,
|
|
cleanSession: true,
|
|
useSSL: false,
|
|
//willMessage: willMessage,
|
|
userName: _mqtt.user,
|
|
password: _mqtt.password,
|
|
onSuccess: onMQTTConnect,
|
|
|
|
onFailure: function (e) {
|
|
//showH5Alert("MQTT_ERR", "消息服务器连接失败!");
|
|
$("#div_main")[0].contentWindow.showMsgwebsoket("", "yt_error_icon", "bg-dot");
|
|
//连接失败定时重连
|
|
}
|
|
};
|
|
MQTTClent.onConnectionLost = onMQTTConnectionLost;
|
|
MQTTClent.onMessageArrived = onMQTTMessageArrived;
|
|
setTimeout(function () {
|
|
MQTTClent.connect(MQTTOptions);
|
|
}, 2000);
|
|
|
|
}
|
|
|
|
//连接服务器并注册连接成功处理事件
|
|
function onMQTTConnect() {
|
|
//初始化数据
|
|
closeMQTTTimes();
|
|
setMqttState();
|
|
MQTTClent.subscribe(MQTTC2STopic);
|
|
|
|
//var _msg = "<li class='divlmsg text-main' style='padding:8px;cursor: pointer;font-weight:bold;' onclick=\"setMSGNewNum(false);$(this).css('font-weight','');\">[消息服务器]连接成功!!</li>";
|
|
//$("#div_msglist").prepend(_msg);
|
|
//setMSGNewNum(true);
|
|
}
|
|
|
|
|
|
//注册连接断开处理事件
|
|
function onMQTTConnectionLost(responseObject) {
|
|
console.log("onConnectionLost:" + JSON.stringify(responseObject));
|
|
if (responseObject.errorCode !== 0) {
|
|
//console.log("onConnectionLost:" + responseObject.errorMessage);
|
|
// var _msg = "<li class='divlmsg text-dot' style='padding:8px;cursor: pointer;font-weight:bold;' onclick=\"setMSGNewNum(false);$(this).css('font-weight','');\">[消息服务器]连接断开!!等待重连……</li>";
|
|
// $("#div_msglist").prepend(_msg);
|
|
// setMSGNewNum(true);
|
|
MQTTConnectFlag = false;
|
|
setMqttState();
|
|
if (MQTTReconnectT == null) {
|
|
MQTTReconnectT = setInterval(function () {
|
|
reConnectMQTT();
|
|
}, 20000);
|
|
}
|
|
}
|
|
}
|
|
|
|
//注册消息接收处理事件
|
|
function onMQTTMessageArrived(message) {
|
|
|
|
//console.log("onMQTTMessageArrived:" + JSON.stringify(message));
|
|
if (message != "") {
|
|
var hasDlg = false;
|
|
var _oldD = decodeURIComponent(message.payloadString);
|
|
_oldD = _oldD.replace(/%20/g, " ");
|
|
var _d = JSON.parse(_oldD);
|
|
//$("#opinfo").prepend("收到的消息:" + _oldD + "<br/>");//收到消息 返回到页面上去
|
|
//遍历窗口是否有该消息
|
|
if (_d.opfun == "online") {
|
|
//showH5Alert("onMQTTMessageArrived", "成功连接服务器!", "message", "bottom")
|
|
} else {
|
|
mqttMessageToFrame(_d);
|
|
}
|
|
}
|
|
}
|
|
|
|
function closeMQTTTimes() {
|
|
MQTTConnectFlag = true;
|
|
clearInterval(MQTTReconnectT);
|
|
MQTTReconnectNum = 0;
|
|
MQTTReconnectT = null;
|
|
}
|
|
|
|
function sendMQTT(msg, qos) {
|
|
let isOk = false;
|
|
let retained = false;
|
|
if (MQTTClent.isConnected) {
|
|
MQTTMessage = new Paho.Message(JSON.stringify(msg));
|
|
MQTTMessage.destinationName = MQTTS2CTopic;//topic;
|
|
MQTTMessage.qos = 2;
|
|
MQTTMessage.retained = false;
|
|
MQTTClent.send(MQTTMessage);
|
|
isOk = true;
|
|
}
|
|
return isOk;
|
|
}
|
|
|
|
// function sendMQTT(msg, topic, qos, retained) {
|
|
// if (MQTTClent.isConnected) {
|
|
// if (qos == null) {
|
|
// qos = 1;
|
|
// }
|
|
// if (retained == null) {
|
|
// retained = false;
|
|
// }
|
|
// MQTTMessage = new Paho.Message(msg);
|
|
// MQTTMessage.destinationName = MQTTS2CTopic;//topic;
|
|
// MQTTMessage.qos = qos;
|
|
// MQTTMessage.retained = retained;
|
|
// MQTTClent.send(MQTTMessage);
|
|
// }
|
|
// }
|
|
|
|
|
|
function reConnectMQTT() {
|
|
if (MQTTReconnectNumMax >= MQTTReconnectNum) {
|
|
MQTTReconnectNum++;
|
|
MQTTClent.connect(MQTTOptions);
|
|
}
|
|
}
|
|
|
|
function mqttMessageToFrame(_msg) {
|
|
try {
|
|
$("#div_main")[0].contentWindow.onMQTTMessage(_msg);
|
|
} catch (e) {
|
|
}
|
|
} |