Allow tcp request node to accept msg.host and msg.port as inputs

Overrides only allowed if edit setting left blank.
This commit is contained in:
Dave C-J 2014-11-06 10:21:14 +00:00
parent 266a644ca6
commit 9c92eeb9f5
2 changed files with 20 additions and 12 deletions

View File

@ -213,7 +213,6 @@
else { else {
$("#fin-tip2").hide(); $("#fin-tip2").hide();
} }
}; };
updateOptions(); updateOptions();
$("#node-input-beserver").change(updateOptions); $("#node-input-beserver").change(updateOptions);
@ -226,7 +225,7 @@
<div class="form-row"> <div class="form-row">
<label for="node-input-server"><i class="fa fa-globe"></i> Server</label> <label for="node-input-server"><i class="fa fa-globe"></i> Server</label>
<input type="text" id="node-input-server" placeholder="ip.address" style="width:50%"> <input type="text" id="node-input-server" placeholder="ip.address" style="width:50%">
&nbsp;port <input type="text" id="node-input-port" style="width:50px"> &nbsp;port <input type="text" id="node-input-port" placeholder="port" style="width:50px">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-out"><i class="fa fa-sign-out"></i> Return</label> <label for="node-input-out"><i class="fa fa-sign-out"></i> Return</label>
@ -243,7 +242,8 @@
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-tips"><b>Tip:</b> outputs a binary <b>Buffer</b>, so you may want to .toString() it.</div> <div class="form-tips"><b>Tip:</b> Outputs a binary <b>Buffer</b>, so you may want to .toString() it.</br/>
<b>Tip:</b> Leave host and port blank if you want to overide with msg.host and msg.port properties.</div>
<script> <script>
var previous = null; var previous = null;
$("#node-input-out").on('focus', function () { previous = this.value; }).change(function() { $("#node-input-out").on('focus', function () { previous = this.value; }).change(function() {
@ -273,6 +273,7 @@
returned characters into a fixed buffer, match a specified character before returning, returned characters into a fixed buffer, match a specified character before returning,
wait a fixed timeout from first reply and then return, or just sit and wait for data.</p> wait a fixed timeout from first reply and then return, or just sit and wait for data.</p>
<p>The response will be output in <b>msg.payload</b> as a buffer, so you may want to .toString() it.</p> <p>The response will be output in <b>msg.payload</b> as a buffer, so you may want to .toString() it.</p>
<p>If you leave tcp host and port blank you must set them by using the <b>msg.host</b> and <b>msg.port</b> properties.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -280,8 +281,8 @@
category: 'function', category: 'function',
color:"Silver", color:"Silver",
defaults: { defaults: {
server: {value:"",required:true}, server: {value:""},
port: {value:"",required:true,validate:RED.validators.number()}, port: {value:"",validate:RED.validators.regex(/^(\d*|)$/)},
out: {value:"time",required:true}, out: {value:"time",required:true},
splitc: {value:"0",required:true}, splitc: {value:"0",required:true},
name: {value:""} name: {value:""}

View File

@ -377,12 +377,19 @@ module.exports = function(RED) {
client = net.Socket(); client = net.Socket();
client.setTimeout(socketTimeout); client.setTimeout(socketTimeout);
node.status({}); node.status({});
client.connect(node.port, node.server, function() { var host = node.server || msg.host;
var port = node.port || msg.port;
if (host && port) {
client.connect(port, host, function() {
//node.log('client connected'); //node.log('client connected');
node.status({fill:"green",shape:"dot",text:"connected"}); node.status({fill:"green",shape:"dot",text:"connected"});
node.connected = true; node.connected = true;
client.write(msg.payload); client.write(msg.payload);
}); });
}
else {
node.warn("Host and/or port not set");
}
client.on('data', function(data) { client.on('data', function(data) {
//node.log("data:"+ data.length+":"+ data); //node.log("data:"+ data.length+":"+ data);
@ -458,7 +465,7 @@ module.exports = function(RED) {
if (client) { if (client) {
client.end(); client.end();
setTimeout(function() { setTimeout(function() {
client.connect(node.port, node.server, function() { client.connect(port, host, function() {
//node.log('client connected'); //node.log('client connected');
node.connected = true; node.connected = true;
client.write(msg.payload); client.write(msg.payload);