dailpad_cti
Version:
60 lines (54 loc) • 1.7 kB
JavaScript
/**
*@Author Nishant Tiwari
*Websocket implementation
**/
const { Client, Message } = require('@stomp/stompjs');
const fs = require('fs');
var client = undefined;
let websocket = require("ws");
Object.assign(global,websocket);
var wsrootTopic;
const initWebsocket = () =>{
let data = "";
try{
data = fs.readFileSync('../../../messaging-config.xml',{ encoding: 'utf8', flag: 'r' });
let wsURL= getTagValue(data,"webSocketURL");
wsrootTopic = getTagValue(data,"rootTopic");
if(wsURL && wsrootTopic){
client = new Client({
brokerURL: wsURL,
connectHeaders: {
username:"ivisionClientCore"
},
onConnect: () => {
console.log("Connected : ",wsURL,wsrootTopic);
//client.subscribe(wsrootTopic, message =>
//console.log(`Received: ${message.body}`)
//);
},
});
}else{
console.error("Invalid or Empty WebSocket URL/RootTopic");
}
}catch(err){
console.error("Error while initiating Websocket:"+err.code,err);
throw err;
}
client.activate();
}
const sendMessageToWs = (topic,payload)=>{
console.log("Im here",wsrootTopic);
if (!client.connected) {
console.error("Broker disconnected, can't send message.");
return false;
}
if (payload && topic) {
let headers = {sender:process.env.APP_NAME,destination:topic,messageType:"Dialpad_Webhook_Events"};
client.publish({destination: wsrootTopic+"/"+topic, body: JSON.stringify({header:headers,messageData:payload})});
}
return true;
}
function getTagValue(data,tag){
return data.match("(?<=<"+tag+">).*(?=<\/"+tag+">)")[0];
}
module.exports = {client,initWebsocket,sendMessageToWs}