mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
parent
f22cd381ee
commit
cce5f33a97
@ -96,7 +96,18 @@
|
||||
<label for="node-config-input-path"><i class="icon-bookmark"></i> Path</label>
|
||||
<input type="text" id="node-config-input-path" placeholder="/ws/example">
|
||||
</div>
|
||||
<div id="node-config-ws-tip" class="form-tips">This path will be relative to <code><span id="node-config-ws-path"></span></code>.</div>
|
||||
<div class="form-row">
|
||||
<label for="node-config-input-wholemsg"> </label>
|
||||
<select type="text" id="node-config-input-wholemsg" style="width: 70%;">
|
||||
<option value="false">Send/Receive payload</option>
|
||||
<option value="true">Send/Receive entire message</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-tips">
|
||||
Be default, <code>payload</code> will contain the data to be sent over, or received from a websocket.
|
||||
The listener can be configured to send or receive the entire message object as a JSON formatted string.
|
||||
<p id="node-config-ws-tip">This path will be relative to <code><span id="node-config-ws-path"></span></code>.</p>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="websocket-listener">
|
||||
@ -107,7 +118,8 @@
|
||||
RED.nodes.registerType('websocket-listener',{
|
||||
category: 'config',
|
||||
defaults: {
|
||||
path: {value:"",required:true,validate:RED.validators.regex(/^((?!\/debug\/ws).)*$/) }
|
||||
path: {value:"",required:true,validate:RED.validators.regex(/^((?!\/debug\/ws).)*$/) },
|
||||
wholemsg: {value:"false"}
|
||||
},
|
||||
inputs:0,
|
||||
outputs:0,
|
||||
|
@ -28,6 +28,8 @@ function WebSocketListenerNode(n) {
|
||||
|
||||
// Store local copies of the node configuration (as defined in the .html)
|
||||
node.path = n.path;
|
||||
node.wholemsg = (n.wholemsg === "true");
|
||||
|
||||
node._inputNodes = []; // collection of nodes that want to receive events
|
||||
|
||||
var path = RED.settings.httpRoot || "/";
|
||||
@ -89,8 +91,18 @@ WebSocketListenerNode.prototype.registerInputNode = function(/*Node*/handler){
|
||||
}
|
||||
|
||||
WebSocketListenerNode.prototype.handleEvent = function(id,/*socket*/socket,/*String*/event,/*Object*/data,/*Object*/flags){
|
||||
var msg;
|
||||
if (this.wholemsg) {
|
||||
msg = JSON.parse(data);
|
||||
} else {
|
||||
msg = {
|
||||
payload:data
|
||||
};
|
||||
}
|
||||
msg._session = {type:"websocket",id:id};
|
||||
|
||||
for (var i = 0; i < this._inputNodes.length; i++) {
|
||||
this._inputNodes[i].send({session:{type:"websocket",id:id},payload:data});
|
||||
this._inputNodes[i].send(msg);
|
||||
};
|
||||
}
|
||||
|
||||
@ -129,16 +141,22 @@ function WebSocketOutNode(n) {
|
||||
this.error("Missing server configuration");
|
||||
}
|
||||
this.on("input", function(msg) {
|
||||
var payload = msg.payload;
|
||||
if (Buffer.isBuffer(payload)) {
|
||||
payload = payload.toString();
|
||||
} else if (typeof payload === "object") {
|
||||
payload = JSON.stringify(payload);
|
||||
} else if (typeof payload !== "string") {
|
||||
payload = ""+payload;
|
||||
var payload;
|
||||
if (this.serverConfig.wholemsg) {
|
||||
delete msg._session;
|
||||
payload = JSON.stringify(msg);
|
||||
} else {
|
||||
payload = msg.payload;
|
||||
if (Buffer.isBuffer(payload)) {
|
||||
payload = payload.toString();
|
||||
} else if (typeof payload === "object") {
|
||||
payload = JSON.stringify(payload);
|
||||
} else if (typeof payload !== "string") {
|
||||
payload = ""+payload;
|
||||
}
|
||||
}
|
||||
if (msg.session && msg.session.type == "websocket") {
|
||||
node.serverConfig.send(msg.session.id,payload);
|
||||
if (msg._session && msg._session.type == "websocket") {
|
||||
node.serverConfig.send(msg._session.id,payload);
|
||||
} else {
|
||||
node.serverConfig.broadcast(payload,function(error){
|
||||
if(!!error){
|
||||
|
Loading…
Reference in New Issue
Block a user