1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/nodes/core/io/22-websocket.html

276 lines
10 KiB
HTML
Raw Normal View History

<!--
Copyright 2013 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- WebSocket Input Node -->
<script type="text/x-red" data-template-name="websocket in">
<div class="form-row">
<label for="node-input-mode"><i class="fa fa-dot-circle-o"></i> <span data-i18n="websocket.label.type"></span></label>
<select id="node-input-mode">
<option value="server" data-i18n="websocket.listenon"></option>
<option value="client" data-i18n="websocket.connectto"></option>
</select>
</div>
<div class="form-row" id="websocket-server-row">
<label for="node-input-server"><i class="fa fa-bookmark"></i> <span data-i18n="websocket.label.path"></span></label>
<input type="text" id="node-input-server">
</div>
<div class="form-row" id="websocket-client-row">
<label for="node-input-client"><i class="fa fa-bookmark"></i> <span data-i18n="websocket.label.url"></span></label>
<input type="text" id="node-input-client">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
2015-05-26 22:11:14 +02:00
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
</script>
<script type="text/x-red" data-help-name="websocket in">
<p>WebSocket input node.</p>
2016-02-12 14:15:53 +01:00
<p>By default, the data received from the WebSocket will be in <code>msg.payload</code>.
The socket can be configured to expect a properly formed JSON string, in which
case it will parse the JSON and send on the resulting object as the entire message.</p>
</script>
<script type="text/javascript">
2015-05-28 00:07:31 +02:00
(function() {
function ws_oneditprepare() {
2016-02-12 14:15:53 +01:00
$("#websocket-client-row").hide();
2016-06-04 10:04:58 +02:00
$("#node-input-mode").change(function() {
if ( $("#node-input-mode").val() === 'client') {
2015-05-28 00:07:31 +02:00
$("#websocket-server-row").hide();
$("#websocket-client-row").show();
}
else {
$("#websocket-server-row").show();
$("#websocket-client-row").hide();
}
});
2016-02-12 14:15:53 +01:00
2016-06-04 10:04:58 +02:00
if (this.client) {
2015-05-28 00:07:31 +02:00
$("#node-input-mode").val('client').change();
}
else {
$("#node-input-mode").val('server').change();
}
}
2016-02-12 14:15:53 +01:00
2015-05-28 00:07:31 +02:00
function ws_oneditsave() {
2016-06-04 10:04:58 +02:00
if ($("#node-input-mode").val() === 'client') {
2015-05-28 00:07:31 +02:00
$("#node-input-server").append('<option value="">Dummy</option>');
2016-02-12 14:15:53 +01:00
$("#node-input-server").val('');
2015-05-28 00:07:31 +02:00
}
else {
$("#node-input-client").append('<option value="">Dummy</option>');
2016-02-12 14:15:53 +01:00
$("#node-input-client").val('');
2015-05-28 00:07:31 +02:00
}
}
2016-02-12 14:15:53 +01:00
2015-05-28 00:07:31 +02:00
function ws_label() {
var nodeid = (this.client)?this.client:this.server;
var wsNode = RED.nodes.node(nodeid);
return this.name||(wsNode?"[ws] "+wsNode.label():"websocket");
}
2016-02-12 14:15:53 +01:00
2015-05-28 00:07:31 +02:00
function ws_validateserver() {
2016-06-04 10:04:58 +02:00
if ($("#node-input-mode").val() === 'client' || (this.client && !this.server)) {
2015-05-28 00:07:31 +02:00
return true;
}
else {
return RED.nodes.node(this.server) != null;
}
}
2016-02-12 14:15:53 +01:00
2015-05-28 00:07:31 +02:00
function ws_validateclient() {
2016-06-04 10:04:58 +02:00
if ($("#node-input-mode").val() === 'client' || (this.client && !this.server)) {
2015-05-28 00:07:31 +02:00
return RED.nodes.node(this.client) != null;
}
else {
return true;
}
}
RED.nodes.registerType('websocket in',{
category: 'input',
defaults: {
name: {value:""},
server: {type:"websocket-listener", validate: ws_validateserver},
client: {type:"websocket-client", validate: ws_validateclient}
},
color:"rgb(215, 215, 160)",
inputs:0,
outputs:1,
icon: "white-globe.png",
labelStyle: function() {
return this.name?"node_label_italic":"";
},
label: ws_label,
oneditsave: ws_oneditsave,
oneditprepare: ws_oneditprepare
});
2016-02-12 14:15:53 +01:00
2015-05-28 00:07:31 +02:00
RED.nodes.registerType('websocket out',{
category: 'output',
defaults: {
name: {value:""},
server: {type:"websocket-listener", validate: ws_validateserver},
client: {type:"websocket-client", validate: ws_validateclient}
},
color:"rgb(215, 215, 160)",
inputs:1,
outputs:0,
icon: "white-globe.png",
align: "right",
labelStyle: function() {
return this.name?"node_label_italic":"";
},
label: ws_label,
oneditsave: ws_oneditsave,
oneditprepare: ws_oneditprepare
});
2016-02-12 14:15:53 +01:00
2015-05-28 00:07:31 +02:00
RED.nodes.registerType('websocket-listener',{
category: 'config',
defaults: {
path: {value:"",required:true,validate:RED.validators.regex(/^((?!\/debug\/ws).)*$/),format:"filepath" },
2015-05-28 00:07:31 +02:00
wholemsg: {value:"false"}
},
inputs:0,
outputs:0,
label: function() {
var root = RED.settings.httpNodeRoot;
2016-02-12 14:15:53 +01:00
if (root.slice(-1) != "/") {
2015-05-28 00:07:31 +02:00
root = root+"/";
}
if (this.path.charAt(0) == "/") {
root += this.path.slice(1);
} else {
root += this.path;
}
return root;
},
oneditprepare: function() {
var root = RED.settings.httpNodeRoot;
2016-02-12 14:15:53 +01:00
if (root.slice(-1) == "/") {
2015-05-28 00:07:31 +02:00
root = root.slice(0,-1);
}
2016-06-04 10:04:58 +02:00
if (root === "") {
2015-05-28 00:07:31 +02:00
$("#node-config-ws-tip").hide();
} else {
$("#node-config-ws-path").html(root);
$("#node-config-ws-tip").show();
}
}
});
RED.nodes.registerType('websocket-client',{
category: 'config',
defaults: {
path: {value:"",required:true,validate:RED.validators.regex(/^((?!\/debug\/ws).)*$/),format:"filepath" },
2015-05-28 00:07:31 +02:00
wholemsg: {value:"false"}
},
inputs:0,
outputs:0,
label: function() {
return this.path;
}
});
2016-02-12 14:15:53 +01:00
2015-05-28 00:07:31 +02:00
})();
</script>
<!-- WebSocket out Node -->
<script type="text/x-red" data-template-name="websocket out">
<div class="form-row">
<label for="node-input-mode"><i class="fa fa-dot-circle-o"></i> <span data-i18n="websocket.label.type"></span></label>
<select id="node-input-mode">
<option value="server" data-i18n="websocket.listenon"></option>
<option value="client" data-i18n="websocket.connectto"></option>
</select>
</div>
<div class="form-row" id="websocket-server-row">
<label for="node-input-server"><i class="fa fa-bookmark"></i> <span data-i18n="websocket.label.path"></span></label>
<input type="text" id="node-input-server">
</div>
<div class="form-row" id="websocket-client-row">
<label for="node-input-client"><i class="fa fa-bookmark"></i> <span data-i18n="websocket.label.url"></span></label>
<input type="text" id="node-input-client">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
2015-05-26 22:11:14 +02:00
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
</script>
<script type="text/x-red" data-help-name="websocket out">
<p>WebSocket out node.</p>
2016-02-12 14:15:53 +01:00
<p>By default, <code>msg.payload</code> will be sent over the WebSocket. The socket
can be configured to encode the entire <code>msg</code> object as a JSON string and send that
over the WebSocket.</p>
<p>If the message arriving at this node started at a WebSocket In node, the message
will be sent back to the client that triggered the flow. Otherwise, the message
will be broadcast to all connected clients.</p>
<p>If you want to broadcast a message that started at a WebSocket In node, you
2016-02-12 14:15:53 +01:00
should delete the <code>msg._session</code> property within the flow.</p>
</script>
<!-- WebSocket Server configuration node -->
<script type="text/x-red" data-template-name="websocket-listener">
<div class="form-row">
<label for="node-config-input-path"><i class="fa fa-bookmark"></i> <span data-i18n="websocket.label.path"></span></label>
<div id="node-config-input-path" contenteditable="true" placeholder="/ws/example"></div>
</div>
<div class="form-row">
<label for="node-config-input-wholemsg">&nbsp;</label>
<select type="text" id="node-config-input-wholemsg" style="width: 70%;">
<option value="false" data-i18n="websocket.payload"></option>
<option value="true" data-i18n="websocket.message"></option>
</select>
</div>
<div class="form-tips">
2015-05-26 22:11:14 +02:00
<span data-i18n="[html]websocket.tip.path1"></span>
<p id="node-config-ws-tip"><span data-i18n="[html]websocket.tip.path2"></span><code><span id="node-config-ws-path"></span></code>.</p>
</div>
</script>
<script type="text/x-red" data-help-name="websocket-listener">
2016-06-04 10:04:58 +02:00
<p>This configuration node creates a WebSocket Server endpoint using the specified path.</p>
</script>
<!-- WebSocket Client configuration node -->
<script type="text/x-red" data-template-name="websocket-client">
<div class="form-row">
<label for="node-config-input-path"><i class="fa fa-bookmark"></i> <span data-i18n="websocket.label.url"></span></label>
<div id="node-config-input-path" contenteditable="true" placeholder="ws://example.com/ws"></div>
</div>
<div class="form-row">
<label for="node-config-input-wholemsg">&nbsp;</label>
<select type="text" id="node-config-input-wholemsg" style="width: 70%;">
<option value="false" data-i18n="websocket.payload"></option>
<option value="true" data-i18n="websocket.message"></option>
</select>
</div>
<div class="form-tips">
2015-05-26 22:11:14 +02:00
<p><span data-i18n="[html]websocket.tip.url1"></span></p>
<span data-i18n="[html]websocket.tip.url2"></span>
</div>
</script>
<script type="text/x-red" data-help-name="websocket-client">
<p>This configuration node connects a WebSocket client to the specified URL.</p>
</script>