1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Allow JSON sending/receiving in websocket node

Fixes #105
This commit is contained in:
Nicholas O'Leary 2013-12-08 20:31:56 +00:00
parent f22cd381ee
commit cce5f33a97
2 changed files with 42 additions and 12 deletions

View File

@ -96,7 +96,18 @@
<label for="node-config-input-path"><i class="icon-bookmark"></i> Path</label> <label for="node-config-input-path"><i class="icon-bookmark"></i> Path</label>
<input type="text" id="node-config-input-path" placeholder="/ws/example"> <input type="text" id="node-config-input-path" placeholder="/ws/example">
</div> </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">&nbsp;</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>
<script type="text/x-red" data-help-name="websocket-listener"> <script type="text/x-red" data-help-name="websocket-listener">
@ -107,7 +118,8 @@
RED.nodes.registerType('websocket-listener',{ RED.nodes.registerType('websocket-listener',{
category: 'config', category: 'config',
defaults: { 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, inputs:0,
outputs:0, outputs:0,

View File

@ -28,6 +28,8 @@ function WebSocketListenerNode(n) {
// Store local copies of the node configuration (as defined in the .html) // Store local copies of the node configuration (as defined in the .html)
node.path = n.path; node.path = n.path;
node.wholemsg = (n.wholemsg === "true");
node._inputNodes = []; // collection of nodes that want to receive events node._inputNodes = []; // collection of nodes that want to receive events
var path = RED.settings.httpRoot || "/"; 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){ 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++) { 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.error("Missing server configuration");
} }
this.on("input", function(msg) { this.on("input", function(msg) {
var payload = msg.payload; var payload;
if (Buffer.isBuffer(payload)) { if (this.serverConfig.wholemsg) {
payload = payload.toString(); delete msg._session;
} else if (typeof payload === "object") { payload = JSON.stringify(msg);
payload = JSON.stringify(payload); } else {
} else if (typeof payload !== "string") { payload = msg.payload;
payload = ""+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") { if (msg._session && msg._session.type == "websocket") {
node.serverConfig.send(msg.session.id,payload); node.serverConfig.send(msg._session.id,payload);
} else { } else {
node.serverConfig.broadcast(payload,function(error){ node.serverConfig.broadcast(payload,function(error){
if(!!error){ if(!!error){