var mqClient;
var mqSubscribeIDObj = { scada: "scada" };
var connectCallback = function (msg) {
for (let topicName in mqSubscribeIDObj) {
addSubscribeTopic(topicName);
}
console.log("websocket is connected");
updateAMQStatusLight("connect");
};
var errorCallback = function (errMsg) {
console.log("websocket error");
updateAMQStatusLight("error");
};
var disconnectCallback = function (msg) {
console.log("websocket is closed");
updateAMQStatusLight("disconnect");
};
var checkConnectionInterval = setInterval(function () {
if (mqClient == undefined) {
conActiveMQ();
} else if (mqClient.ws.readyState == mqClient.ws.CONNECTING) {
console.log("ActiveMQ websocket is connecting");
updateAMQStatusLight("reconnect");
} else if (mqClient.ws.readyState == mqClient.ws.CLOSED) {
// show notification
showToast(
"ActiveMQ websocket is disconnected and reconnection is attempting...",
"error"
);
// disconnect activeMQ
updateAMQStatusLight("disconnect");
mqClient.ws.close();
for (let topicName in mqSubscribeIDObj) {
mqClient.unsubscribe(mqSubscribeIDObj[topicName]);
}
mqClient.disconnect();
// reconnect activeMQ
conActiveMQ();
}
}, 500);
// connect activeMQ - using websocket
function conActiveMQ() {
var hostname = window.location.hostname
? window.location.hostname
: "127.0.0.1";
var url = `ws://${hostname}:61614`;
var login = "admin";
var passcode = "admin";
mqClient = Stomp.client(url);
mqClient.heartbeat.outgoing = 3000;
mqClient.heartbeat.incoming = 3000;
mqClient.reconnect_delay = 500;
mqClient.connect(login, passcode, connectCallback, errorCallback);
// mqClient.connect(login, passcode, connectCallback, errorCallback, disconnectCallback);
}
// subscribe topic
function addSubscribeTopic(topicName) {
const destination = `/topic/${topicName}`;
let mqSubscribeID = mqClient.subscribe(destination, function (msg) {
var jsonObj = JSON.parse(msg.body);
debugger
if (topicName.includes("_ALARM")) {
alarm(jsonObj);
} else {
TagValueOperation(jsonObj);
}
});
mqSubscribeIDObj[topicName] = mqSubscribeID;
console.log(`Subscribe Topic: ${topicName}`);
}
// unsubscribe topic
function removeSubscribeTopic(topicName) {
let id = mqSubscribeIDObj[topicName];
mqClient.unsubscribe(id);
delete mqSubscribeIDObj[topicName];
console.log(`Unsubscribe Topic: ${topicName}`);
}
// send msg to ActiveMQ Topic - REST API
function sendTopicMsg(TopicName, msg) {
var clientId = "user";
$.ajax({
url: `/mq/${TopicName}?type=topic&clientId=${clientId}`,
type: "POST",
async: true,
data: { body: JSON.stringify(msg) },
success: function () {
console.log(`Message has been sent to Topic: ${TopicName}`);
},
});
}
// send msg to ActiveMQ Queue - STOMP WebSocket, REST API
function sendQueueMsg(QueName, msg) {
// send msg to ActiveMQ queue - STOMP WebSocket
// var destination = `/queue/${QueName}`;
// mqClient.send(destination, {}, JSON.stringify(msg));
// console.log(`Message has been sent to Queue: ${QueName}`);
// send msg to activeMQ queue - REST API
var clientId = "user";
$.ajax({
url: `/mq/${QueName}?type=queue&clientId=${clientId}`,
type: "POST",
async: true,
data: { body: JSON.stringify(msg) },
success: function () {
console.log(`Message has been sent to Queue: ${QueName}`);
},
});
}
// receive msg from activeMQ (queue) - REST API
function recvQueueMsg(QueName) {
let clientId = "user";
$.ajax({
url: `/mq/` + QueName + `?type=queue&clientId=${clientId}`,
type: "GET",
success: function (data) {
let jsonData = JSON.parse(data);
console.log("get queue message from ActiveMQ");
},
});
}