Hello All,
I have created a custom node and I have deployed it successfully it is working fine but now I have some done some changes in my node.js code and I have replaced my previous custom node code with this code but now it is throwing some error
The above image will show the error occurred will running node-red.
module.exports = function(RED) {
var WebSocket = require('ws');
//var Stomp = require('stompjs');
function CustomWebSocketListener(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
let msg =msg.payload;
let urllist = msg.url;
let urls=[]
if(global.websocket!=null || global.websocket!=undefined){
let connections=global.websocket;
console.log("global")
if(isAccessTokenExpired){
for(let k in connections) {
connections[k].close();
connections[k].reconnect=false
console.log('connection closed:::');
}
urls=urllist;
}
else{
for(let i=0;i<urllist.length;i++){
let group=urllist[i].split("/")[7].split("?");
if(!connections[group[0]]){
urls.push(urllist[i])
}
}
}
}
else{
urls=urllist;
}
console.log("urls.length::::"+urls.length)
if(urls.length > 0){
for(let i=0;i<urls.length;i++){
console.log(':::::::for loop:::::::')
console.log('url----'+urls[i])
function connect(urlIndex) {
let ws;
if(urlIndex!=undefined||urlIndex!=null){
ws = new WebSocket(urls[urlIndex]);
}
else{
ws = new WebSocket(urls[i]);
}
ws.index=i;
ws.reconnect=true;
ws.onopen = function() {
};
ws.onmessage = function(e) {
console.log('Message:', e.data);
//var data=JSON.parse(e.data);
//msg.payload=data;
// var data=JSON.stringify(e.data);
//console.log("**********8dataaaaa*****")
//console.log(data)
// var error_1="{\"subscriptionTerminated\":{\"reason\":{\"messageId\":\"POL0014\",\"text\":\"Token Validation Error. Code %1, Required Scopes %2\",\"variables\":[\"TE0004\",\"msi_unsapi_location.watch\"]}}}";
//console.log("***********string***********",+data);
// if(data==error_1)
// {
//console.log("*********if*****")
//node.send({payload:data});
//var token_value="Token Expired";
//node.send({payload:token_value});
//}
//else{
//console.log("*********else*****")
/*msg.payload=e.data;
var log="Connected";
// msg.payload=log;
node.send({payload:log});
node.send(msg);
*/
//}
};
ws.onclose = function(e) {
var dis="Disconnected";
// node.send({payload:dis})
var log_1="Socket is closed. Reconnect will be attempted in 30 second.";
//msg.payload=log_1;
// node.send({payload:log_1});
console.log('Socket is closed. Reconnect will be attempted in 30 second.', e.reason);
setTimeout(function() {
console.log("onclose ----ws reconnect"+ws.reconnect)
if(ws.reconnect){
console.log("inside if reconnect")
connect(ws.index);
}
}, 30000);
};
ws.onerror = function(err) {
console.error('Socket encountered error: ', err.message, 'Closing socket');
ws.close();
};
if(global.websocket==null || global.websocket==undefined){
global.websocket={};
console.log(global.websocket)
global.websocket[urls[i]]=ws;
}
else{
global.websocket[urls[i]]=ws;
}
}
connect();
}
}
});
}
RED.nodes.registerType("custom-websocketlistener",CustomWebSocketListener);
}
The above code is what I have changed
system
Closed
6 June 2020 07:34
3
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.