2014-05-19 22:07:20 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var StompClient = require('stomp-client');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
|
|
|
|
function StompServerNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
|
|
|
this.port = n.port;
|
2015-12-17 18:15:01 +01:00
|
|
|
this.protocolversion = n.protocolversion;
|
|
|
|
this.vhost = n.vhost;
|
2018-08-15 23:17:32 +02:00
|
|
|
this.reconnectretries = n.reconnectretries || 999999;
|
|
|
|
this.reconnectdelay = (n.reconnectdelay || 15) * 1000;
|
2014-05-19 22:07:20 +02:00
|
|
|
this.name = n.name;
|
2015-02-06 22:10:14 +01:00
|
|
|
this.username = this.credentials.user;
|
|
|
|
this.password = this.credentials.password;
|
2014-05-19 22:07:20 +02:00
|
|
|
}
|
2015-02-06 22:10:14 +01:00
|
|
|
RED.nodes.registerType("stomp-server",StompServerNode,{
|
|
|
|
credentials: {
|
|
|
|
user: {type:"text"},
|
|
|
|
password: {type: "password"}
|
2014-05-19 22:07:20 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function StompInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
|
|
|
this.topic = n.topic;
|
|
|
|
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.server);
|
2015-12-17 18:15:01 +01:00
|
|
|
this.stompClientOpts = {
|
|
|
|
address: this.serverConfig.server,
|
|
|
|
port: this.serverConfig.port * 1,
|
|
|
|
user: this.serverConfig.username,
|
|
|
|
pass: this.serverConfig.password,
|
|
|
|
protocolVersion: this.serverConfig.protocolversion,
|
|
|
|
reconnectOpts: {
|
|
|
|
retries: this.serverConfig.reconnectretries * 1,
|
|
|
|
delay: this.serverConfig.reconnectdelay * 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (this.serverConfig.vhost) {
|
2017-01-29 18:45:44 +01:00
|
|
|
this.stompClientOpts.vhost = this.serverConfig.vhost;
|
2015-12-17 18:15:01 +01:00
|
|
|
}
|
2014-05-19 22:07:20 +02:00
|
|
|
|
|
|
|
var node = this;
|
|
|
|
var msg = {topic:this.topic};
|
2015-12-17 18:15:01 +01:00
|
|
|
node.client = new StompClient(node.stompClientOpts);
|
2014-05-26 11:07:20 +02:00
|
|
|
|
2015-12-17 18:15:01 +01:00
|
|
|
node.client.on("connect", function() {
|
2017-01-29 18:45:44 +01:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2015-12-17 18:15:01 +01:00
|
|
|
});
|
2014-05-26 11:07:20 +02:00
|
|
|
|
2015-12-17 18:15:01 +01:00
|
|
|
node.client.on("reconnecting", function() {
|
|
|
|
node.status({fill:"red",shape:"ring",text:"reconnecting"});
|
|
|
|
node.warn("reconnecting");
|
2014-05-26 11:07:20 +02:00
|
|
|
});
|
2014-05-19 22:07:20 +02:00
|
|
|
|
2023-04-07 09:26:05 +02:00
|
|
|
node.client.on("reconnect", function() {
|
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
|
|
|
});
|
|
|
|
|
2014-05-19 22:07:20 +02:00
|
|
|
node.client.on("error", function(error) {
|
2014-05-31 21:08:15 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"error"});
|
2015-12-17 18:15:01 +01:00
|
|
|
node.warn(error);
|
2014-05-19 22:07:20 +02:00
|
|
|
});
|
|
|
|
|
2015-12-17 18:15:01 +01:00
|
|
|
node.status({fill:"grey",shape:"ring",text:"connecting"});
|
|
|
|
node.client.connect(function(sessionId) {
|
|
|
|
node.log('subscribing to: '+node.topic);
|
|
|
|
node.client.subscribe(node.topic, function(body, headers) {
|
2018-11-28 19:39:52 +01:00
|
|
|
var newmsg={"headers":headers,"topic":node.topic}
|
2015-12-17 18:15:01 +01:00
|
|
|
try {
|
2018-11-28 19:39:52 +01:00
|
|
|
newmsg.payload = JSON.parse(body);
|
2015-12-17 18:15:01 +01:00
|
|
|
}
|
|
|
|
catch(e) {
|
2018-11-28 19:39:52 +01:00
|
|
|
newmsg.payload = body;
|
2015-12-17 18:15:01 +01:00
|
|
|
}
|
2018-11-28 19:39:52 +01:00
|
|
|
node.send(newmsg);
|
2015-12-17 18:15:01 +01:00
|
|
|
});
|
|
|
|
}, function(error) {
|
|
|
|
node.status({fill:"grey",shape:"dot",text:"error"});
|
|
|
|
node.warn(error);
|
|
|
|
});
|
2014-05-26 11:07:20 +02:00
|
|
|
|
2014-05-19 22:07:20 +02:00
|
|
|
node.on("close", function(done) {
|
|
|
|
if (node.client) {
|
2015-12-17 18:15:01 +01:00
|
|
|
// disconnect can accept a callback - but it is not always called.
|
|
|
|
node.client.disconnect();
|
2015-04-17 22:06:27 +02:00
|
|
|
}
|
2015-12-17 18:15:01 +01:00
|
|
|
done();
|
2014-05-19 22:07:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("stomp in",StompInNode);
|
|
|
|
|
|
|
|
|
|
|
|
function StompOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.server = n.server;
|
|
|
|
this.topic = n.topic;
|
|
|
|
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.server);
|
2015-12-17 18:15:01 +01:00
|
|
|
this.stompClientOpts = {
|
|
|
|
address: this.serverConfig.server,
|
|
|
|
port: this.serverConfig.port * 1,
|
|
|
|
user: this.serverConfig.username,
|
|
|
|
pass: this.serverConfig.password,
|
|
|
|
protocolVersion: this.serverConfig.protocolversion,
|
|
|
|
reconnectOpts: {
|
|
|
|
retries: this.serverConfig.reconnectretries * 1,
|
|
|
|
delay: this.serverConfig.reconnectdelay * 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (this.serverConfig.vhost) {
|
2017-01-29 18:45:44 +01:00
|
|
|
this.stompClientOpts.vhost = this.serverConfig.vhost;
|
2015-12-17 18:15:01 +01:00
|
|
|
}
|
2014-05-19 22:07:20 +02:00
|
|
|
|
|
|
|
var node = this;
|
2015-12-17 18:15:01 +01:00
|
|
|
node.client = new StompClient(node.stompClientOpts);
|
2014-05-26 11:07:20 +02:00
|
|
|
|
2015-12-17 18:15:01 +01:00
|
|
|
node.client.on("connect", function() {
|
2017-01-29 18:45:44 +01:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2014-05-26 11:07:20 +02:00
|
|
|
});
|
2014-05-19 22:07:20 +02:00
|
|
|
|
2015-12-17 18:15:01 +01:00
|
|
|
node.client.on("reconnecting", function() {
|
|
|
|
node.status({fill:"red",shape:"ring",text:"reconnecting"});
|
|
|
|
node.warn("reconnecting");
|
2014-05-26 11:07:20 +02:00
|
|
|
});
|
2014-05-19 22:07:20 +02:00
|
|
|
|
|
|
|
node.client.on("error", function(error) {
|
2015-12-17 18:15:01 +01:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"error"});
|
|
|
|
node.warn(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"connecting"});
|
|
|
|
node.client.connect(function(sessionId) {
|
|
|
|
}, function(error) {
|
|
|
|
node.status({fill:"grey",shape:"dot",text:"error"});
|
|
|
|
node.warn(error);
|
2014-05-19 22:07:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
node.on("input", function(msg) {
|
2015-04-28 22:04:50 +02:00
|
|
|
node.client.publish(node.topic || msg.topic, msg.payload, msg.headers);
|
2014-05-19 22:07:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
node.on("close", function(done) {
|
2015-04-17 22:06:27 +02:00
|
|
|
if (node.client) {
|
2015-12-17 18:15:01 +01:00
|
|
|
// disconnect can accept a callback - but it is not always called.
|
|
|
|
node.client.disconnect();
|
2015-04-17 22:06:27 +02:00
|
|
|
}
|
2015-12-17 18:15:01 +01:00
|
|
|
done();
|
2014-05-19 22:07:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("stomp out",StompOutNode);
|
|
|
|
|
2015-12-17 18:15:01 +01:00
|
|
|
};
|