node-red-contrib-websocket-server 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node-red-out.log +7 -0
- package/package.json +1 -1
- package/websocket.html +24 -4
- package/websocket.js +14 -2
- package/websocket_old.js +587 -0
package/node-red-out.log
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
4 Dec 01:39:37 - [error] [websocket_in:74d0f57c24d46886] Missing server configuration
|
|
2
|
+
4 Dec 01:39:37 - [error] [websocket_out:6869372869b99a04] Missing server configuration
|
|
3
|
+
4 Dec 01:39:37 - [error] [websocket_Open/Close:623fd1447a52eebe] Missing server configuration
|
|
4
|
+
4 Dec 01:39:37 - [error] [websocket_Drop:a9022ea21938b95d] Missing server configuration
|
|
5
|
+
4 Dec 08:21:06 - [error] [websocket_out:53c713250b4ce90c] Missing server configuration
|
|
6
|
+
4 Dec 08:21:50 - [error] [websocket_out:53c713250b4ce90c] Missing server configuration
|
|
7
|
+
4 Dec 08:22:02 - [error] [websocket_out:53c713250b4ce90c] Missing server configuration
|
package/package.json
CHANGED
package/websocket.html
CHANGED
|
@@ -154,7 +154,12 @@
|
|
|
154
154
|
labelStyle: function() {
|
|
155
155
|
return this.name?"node_label_italic":"";
|
|
156
156
|
},
|
|
157
|
-
label: ws_label
|
|
157
|
+
//label: ws_label
|
|
158
|
+
label : function(){
|
|
159
|
+
var nodeid = (this.client)?this.client:this.server;
|
|
160
|
+
var wsNode = RED.nodes.node(nodeid);
|
|
161
|
+
return this.name||(wsNode?"[ws_Open/Close] "+wsNode.label():"websocket_Open/Close");
|
|
162
|
+
}
|
|
158
163
|
});
|
|
159
164
|
|
|
160
165
|
RED.nodes.registerType('websocket_Drop',{
|
|
@@ -170,7 +175,12 @@
|
|
|
170
175
|
labelStyle: function() {
|
|
171
176
|
return this.name?"node_label_italic":"";
|
|
172
177
|
},
|
|
173
|
-
label: ws_label
|
|
178
|
+
//label: ws_label
|
|
179
|
+
label : function(){
|
|
180
|
+
var nodeid = (this.client)?this.client:this.server;
|
|
181
|
+
var wsNode = RED.nodes.node(nodeid);
|
|
182
|
+
return this.name||(wsNode?"[ws_Drop] "+wsNode.label():"websocket_Drop");
|
|
183
|
+
}
|
|
174
184
|
});
|
|
175
185
|
|
|
176
186
|
RED.nodes.registerType('websocket_in',{
|
|
@@ -186,7 +196,12 @@
|
|
|
186
196
|
labelStyle: function() {
|
|
187
197
|
return this.name?"node_label_italic":"";
|
|
188
198
|
},
|
|
189
|
-
label: ws_label
|
|
199
|
+
//label: ws_label
|
|
200
|
+
label : function(){
|
|
201
|
+
var nodeid = (this.client)?this.client:this.server;
|
|
202
|
+
var wsNode = RED.nodes.node(nodeid);
|
|
203
|
+
return this.name||(wsNode?"[ws_In] "+wsNode.label():"websocket_In");
|
|
204
|
+
}
|
|
190
205
|
});
|
|
191
206
|
|
|
192
207
|
RED.nodes.registerType('websocket_out',{
|
|
@@ -203,7 +218,12 @@
|
|
|
203
218
|
labelStyle: function() {
|
|
204
219
|
return this.name?"node_label_italic":"";
|
|
205
220
|
},
|
|
206
|
-
label: ws_label
|
|
221
|
+
//label: ws_label
|
|
222
|
+
label : function(){
|
|
223
|
+
var nodeid = (this.client)?this.client:this.server;
|
|
224
|
+
var wsNode = RED.nodes.node(nodeid);
|
|
225
|
+
return this.name||(wsNode?"[ws_Out\n] "+wsNode.label():"websocket_Out");
|
|
226
|
+
}
|
|
207
227
|
});
|
|
208
228
|
|
|
209
229
|
RED.nodes.registerType('websocket_server',{
|
package/websocket.js
CHANGED
|
@@ -138,6 +138,7 @@ module.exports = function(RED) {
|
|
|
138
138
|
socket.nrPendingPong = false;
|
|
139
139
|
node._clients[id] = socket;
|
|
140
140
|
node.emit('opened',{count:Object.keys(node._clients).length,id:id});
|
|
141
|
+
//node.handleOpenCloseEvent(id,socket,'open');
|
|
141
142
|
node.handleOpenCloseEvent(id,socket,'open');
|
|
142
143
|
|
|
143
144
|
|
|
@@ -328,6 +329,7 @@ module.exports = function(RED) {
|
|
|
328
329
|
|
|
329
330
|
var msg;
|
|
330
331
|
msg = {"event" : event};
|
|
332
|
+
//msg = {"ip" : socket. };
|
|
331
333
|
msg._session = {type:"websocket",id:id};
|
|
332
334
|
for (var i = 0; i < this._openNodes.length; i++) {
|
|
333
335
|
this._openNodes[i].send(msg);
|
|
@@ -514,8 +516,18 @@ module.exports = function(RED) {
|
|
|
514
516
|
}
|
|
515
517
|
}
|
|
516
518
|
if (payload) {
|
|
517
|
-
if (msg._session
|
|
518
|
-
|
|
519
|
+
if (msg._session) {
|
|
520
|
+
if (Array.isArray(msg._session))
|
|
521
|
+
{
|
|
522
|
+
for (var i = 0; i < msg._session.lenght; i++)
|
|
523
|
+
{
|
|
524
|
+
node.serverConfig.reply(msg._session[i],payload);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
else
|
|
528
|
+
{
|
|
529
|
+
node.serverConfig.reply(msg._session.id,payload);
|
|
530
|
+
}
|
|
519
531
|
} else {
|
|
520
532
|
node.serverConfig.broadcast(payload,function(error) {
|
|
521
533
|
if (!!error) {
|
package/websocket_old.js
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
**/
|
|
16
|
+
|
|
17
|
+
module.exports = function(RED) {
|
|
18
|
+
"use strict";
|
|
19
|
+
var ws = require("ws");
|
|
20
|
+
var inspect = require("util").inspect;
|
|
21
|
+
var url = require("url");
|
|
22
|
+
var HttpsProxyAgent = require('https-proxy-agent');
|
|
23
|
+
const { getProxyForUrl } = require('./lib/proxyHelper');
|
|
24
|
+
|
|
25
|
+
var serverUpgradeAdded = false;
|
|
26
|
+
function handleServerUpgrade(request, socket, head) {
|
|
27
|
+
const pathname = url.parse(request.url).pathname;
|
|
28
|
+
if (listenerNodes.hasOwnProperty(pathname)) {
|
|
29
|
+
listenerNodes[pathname].server.handleUpgrade(request, socket, head, function done(ws) {
|
|
30
|
+
listenerNodes[pathname].server.emit('connection', ws, request);
|
|
31
|
+
});
|
|
32
|
+
} else {
|
|
33
|
+
// Don't destroy the socket as other listeners may want to handle the
|
|
34
|
+
// event.
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
var listenerNodes = {};
|
|
38
|
+
|
|
39
|
+
// A node red node that sets up a local websocket server
|
|
40
|
+
function WebSocketListenerNode(n) {
|
|
41
|
+
// Create a RED node
|
|
42
|
+
RED.nodes.createNode(this,n);
|
|
43
|
+
var node = this;
|
|
44
|
+
|
|
45
|
+
// Store local copies of the node configuration (as defined in the .html)
|
|
46
|
+
node.path = n.path;
|
|
47
|
+
if (typeof n.subprotocol === "string") {
|
|
48
|
+
// Split the string on comma and trim each result
|
|
49
|
+
node.subprotocol = n.subprotocol.split(",").map(v => v.trim())
|
|
50
|
+
} else {
|
|
51
|
+
node.subprotocol = [];
|
|
52
|
+
}
|
|
53
|
+
node.wholemsg = (n.wholemsg === "true");
|
|
54
|
+
|
|
55
|
+
node._inputNodes = []; // collection of nodes that want to receive events
|
|
56
|
+
node._openNodes = [];
|
|
57
|
+
node._clients = {};
|
|
58
|
+
node._intervals = {};
|
|
59
|
+
node.heartbeatInterval = {};
|
|
60
|
+
// match absolute url
|
|
61
|
+
node.isServer = !/^ws{1,2}:\/\//i.test(node.path);
|
|
62
|
+
node.closing = false;
|
|
63
|
+
node.tls = n.tls;
|
|
64
|
+
node.upgradeHeaders = n.headers
|
|
65
|
+
|
|
66
|
+
if (n.hb) {
|
|
67
|
+
var heartbeat = parseInt(n.hb);
|
|
68
|
+
if (heartbeat > 0) {
|
|
69
|
+
node.heartbeat = heartbeat * 1000;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
node.pingaction = n.pingaction;
|
|
73
|
+
|
|
74
|
+
function startconn() { // Connect to remote endpoint
|
|
75
|
+
node.tout = null;
|
|
76
|
+
const prox = getProxyForUrl(node.brokerurl, RED.settings.proxyOptions);
|
|
77
|
+
let agent = undefined;
|
|
78
|
+
if (prox) {
|
|
79
|
+
agent = new HttpsProxyAgent(prox);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
var options = {};
|
|
83
|
+
if (agent) {
|
|
84
|
+
options.agent = agent;
|
|
85
|
+
}
|
|
86
|
+
if (node.tls) {
|
|
87
|
+
var tlsNode = RED.nodes.getNode(node.tls);
|
|
88
|
+
if (tlsNode) {
|
|
89
|
+
tlsNode.addTLSOptions(options);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// We need to check if undefined, to guard against previous installs, that will not have had this property set (applies to 3.1.x setups)
|
|
94
|
+
// Else this will be breaking potentially
|
|
95
|
+
if(node.upgradeHeaders !== undefined && node.upgradeHeaders.length > 0){
|
|
96
|
+
options.headers = {};
|
|
97
|
+
for(let i = 0;i<node.upgradeHeaders.length;i++){
|
|
98
|
+
const header = node.upgradeHeaders[i];
|
|
99
|
+
const keyType = header.keyType;
|
|
100
|
+
const keyValue = header.keyValue;
|
|
101
|
+
const valueType = header.valueType;
|
|
102
|
+
const valueValue = header.valueValue;
|
|
103
|
+
|
|
104
|
+
const headerName = keyType === 'other' ? keyValue : keyType;
|
|
105
|
+
let headerValue;
|
|
106
|
+
|
|
107
|
+
switch(valueType){
|
|
108
|
+
case 'other':
|
|
109
|
+
headerValue = valueValue;
|
|
110
|
+
break;
|
|
111
|
+
|
|
112
|
+
case 'env':
|
|
113
|
+
headerValue = RED.util.evaluateNodeProperty(valueValue,valueType,node);
|
|
114
|
+
break;
|
|
115
|
+
|
|
116
|
+
default:
|
|
117
|
+
headerValue = valueType;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if(headerName && headerValue){
|
|
122
|
+
options.headers[headerName] = headerValue
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
var socket = new ws(node.path,node.subprotocol,options);
|
|
129
|
+
socket.setMaxListeners(0);
|
|
130
|
+
node.server = socket; // keep for closing
|
|
131
|
+
handleConnection(socket);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function handleConnection(/*socket*/socket) {
|
|
135
|
+
var id = RED.util.generateId();
|
|
136
|
+
socket.nrId = id;
|
|
137
|
+
socket.nrPendingPing = true;
|
|
138
|
+
socket.nrPendingPong = false;
|
|
139
|
+
node._clients[id] = socket;
|
|
140
|
+
node.emit('opened',{count:Object.keys(node._clients).length,id:id});
|
|
141
|
+
//node.handleOpenCloseEvent(id,socket,'open');
|
|
142
|
+
node.handleOpenCloseEvent(id,socket,'open');
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
//dorobit reakciu na ping v resp pri kazdom novom pripojeni nastavit casovac ze ma ocakavat ping,
|
|
146
|
+
//ak nepride tak ukonci spojenie
|
|
147
|
+
//ak pride restuje casovac a zacne pocitat odznovu
|
|
148
|
+
|
|
149
|
+
if (node.pingaction !== "none")
|
|
150
|
+
{
|
|
151
|
+
//clearInterval(node.heartbeatInterval);
|
|
152
|
+
node.heartbeatInterval[id] = setInterval(function() {
|
|
153
|
+
if (node.pingaction === "send"){
|
|
154
|
+
if (socket.nrPendingPong){
|
|
155
|
+
var msg = {};
|
|
156
|
+
//node.warn("No pong received " + node.heartbeat);
|
|
157
|
+
msg._session = {"id":id};
|
|
158
|
+
//node.serverConfig.drop(msg._session.id);
|
|
159
|
+
node.drop(msg._session.id);
|
|
160
|
+
socket.nrErrorHandler(new Error("timeout"));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
//node.warn("No pong received " + node.heartbeat);
|
|
164
|
+
socket.nrPendingPong = true;
|
|
165
|
+
try {
|
|
166
|
+
socket.ping();
|
|
167
|
+
} catch(err) {
|
|
168
|
+
node.warn(err);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (node.pingaction === "receive") {
|
|
172
|
+
if (socket.nrPendingPing){
|
|
173
|
+
var msg = {};
|
|
174
|
+
//node.warn("No pong received " + node.heartbeat);
|
|
175
|
+
msg._session = {"id":id};
|
|
176
|
+
//node.serverConfig.drop(msg._session.id);
|
|
177
|
+
node.drop(msg._session.id);
|
|
178
|
+
socket.nrErrorHandler(new Error("timeout"));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
},node.heartbeat);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
socket.on('close',function() {
|
|
186
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
187
|
+
if (node.isServer) {
|
|
188
|
+
delete node._clients[id];
|
|
189
|
+
node.emit('closed',{count:Object.keys(node._clients).length,id:id});
|
|
190
|
+
node.handleOpenCloseEvent(id,socket,'close');
|
|
191
|
+
} else {
|
|
192
|
+
node.emit('closed',{count:'',id:id});
|
|
193
|
+
}
|
|
194
|
+
if (!node.closing && !node.isServer) {
|
|
195
|
+
clearTimeout(node.tout);
|
|
196
|
+
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
socket.on('message',function(data,flags) {
|
|
200
|
+
node.handleEvent(id,socket,'message',data,flags);
|
|
201
|
+
});
|
|
202
|
+
socket.nrErrorHandler = function(err) {
|
|
203
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
204
|
+
node.emit('erro',{err:err,id:id});
|
|
205
|
+
if (!node.closing && !node.isServer) {
|
|
206
|
+
clearTimeout(node.tout);
|
|
207
|
+
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
socket.on('error',socket.nrErrorHandler);
|
|
211
|
+
socket.on('ping', function() {
|
|
212
|
+
socket.nrPendingPing = false;
|
|
213
|
+
})
|
|
214
|
+
socket.on('pong', function() {
|
|
215
|
+
socket.nrPendingPong = false;
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (node.isServer) {
|
|
220
|
+
if (!serverUpgradeAdded) {
|
|
221
|
+
RED.server.on('upgrade', handleServerUpgrade);
|
|
222
|
+
serverUpgradeAdded = true
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
var path = RED.settings.httpNodeRoot || "/";
|
|
226
|
+
path = path + (path.slice(-1) == "/" ? "":"/") + (node.path.charAt(0) == "/" ? node.path.substring(1) : node.path);
|
|
227
|
+
node.fullPath = path;
|
|
228
|
+
|
|
229
|
+
if (listenerNodes.hasOwnProperty(path)) {
|
|
230
|
+
node.error(RED._("Cannot have two WebSocket listeners on the same path: __path__",{path: node.path}));
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
listenerNodes[node.fullPath] = node;
|
|
234
|
+
var serverOptions = {
|
|
235
|
+
noServer: true
|
|
236
|
+
}
|
|
237
|
+
if (RED.settings.webSocketNodeVerifyClient) {
|
|
238
|
+
serverOptions.verifyClient = RED.settings.webSocketNodeVerifyClient;
|
|
239
|
+
}
|
|
240
|
+
// Create a WebSocket Server
|
|
241
|
+
node.server = new ws.Server(serverOptions);
|
|
242
|
+
node.server.setMaxListeners(0);
|
|
243
|
+
node.server.on('connection', handleConnection);
|
|
244
|
+
// Not adding server-initiated heartbeats yet
|
|
245
|
+
// node.heartbeatInterval = setInterval(function() {
|
|
246
|
+
// node.server.clients.forEach(function(ws) {
|
|
247
|
+
// if (ws.nrPendingHeartbeat) {
|
|
248
|
+
// // No pong received
|
|
249
|
+
// ws.terminate();
|
|
250
|
+
// ws.nrErrorHandler(new Error("timeout"));
|
|
251
|
+
// return;
|
|
252
|
+
// }
|
|
253
|
+
// ws.nrPendingHeartbeat = true;
|
|
254
|
+
// ws.ping();
|
|
255
|
+
// });
|
|
256
|
+
// })
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
node.closing = false;
|
|
260
|
+
startconn(); // start outbound connection
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
node.on("close", function(done) {
|
|
264
|
+
/*if (node.heartbeatInterval[id]) {
|
|
265
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
266
|
+
}*/
|
|
267
|
+
try {
|
|
268
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
269
|
+
}
|
|
270
|
+
catch(e) { // swallow any errors
|
|
271
|
+
}
|
|
272
|
+
if (node.isServer) {
|
|
273
|
+
delete listenerNodes[node.fullPath];
|
|
274
|
+
node.server.close();
|
|
275
|
+
node._inputNodes = [];
|
|
276
|
+
node._openNodes = [];
|
|
277
|
+
done();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
RED.nodes.registerType("websocket_server",WebSocketListenerNode);
|
|
283
|
+
|
|
284
|
+
WebSocketListenerNode.prototype.registerInputNode = function(/*Node*/handler) {
|
|
285
|
+
this._inputNodes.push(handler);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
WebSocketListenerNode.prototype.registerOpenNode = function(/*Node*/handler) {
|
|
289
|
+
this._openNodes.push(handler);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
WebSocketListenerNode.prototype.removeInputNode = function(/*Node*/handler) {
|
|
293
|
+
this._inputNodes.forEach(function(node, i, inputNodes) {
|
|
294
|
+
if (node === handler) {
|
|
295
|
+
inputNodes.splice(i, 1);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
this._openNodes.forEach(function(node, i, openNodes) {
|
|
299
|
+
if (node === handler) {
|
|
300
|
+
openNodes.splice(i, 1);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
WebSocketListenerNode.prototype.handleEvent = function(id,/*socket*/socket,/*String*/event,/*Object*/data,/*Object*/flags) {
|
|
306
|
+
var msg;
|
|
307
|
+
if (this.wholemsg) {
|
|
308
|
+
try {
|
|
309
|
+
msg = JSON.parse(data);
|
|
310
|
+
if (typeof msg !== "object" && !Array.isArray(msg) && (msg !== null)) {
|
|
311
|
+
msg = { payload:msg };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
catch(err) {
|
|
315
|
+
msg = { payload:data };
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
msg = {
|
|
319
|
+
payload:data
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
msg._session = {type:"websocket",id:id};
|
|
323
|
+
for (var i = 0; i < this._inputNodes.length; i++) {
|
|
324
|
+
this._inputNodes[i].send(msg);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
WebSocketListenerNode.prototype.handleOpenCloseEvent = function(id,/*socket*/socket,/*String*/event,) {
|
|
329
|
+
|
|
330
|
+
var msg;
|
|
331
|
+
msg = {"event" : event};
|
|
332
|
+
//msg = {"ip" : socket. };
|
|
333
|
+
msg._session = {type:"websocket",id:id};
|
|
334
|
+
for (var i = 0; i < this._openNodes.length; i++) {
|
|
335
|
+
this._openNodes[i].send(msg);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
WebSocketListenerNode.prototype.broadcast = function(data) {
|
|
341
|
+
for (let client in this._clients) {
|
|
342
|
+
if (this._clients.hasOwnProperty(client)) {
|
|
343
|
+
try {
|
|
344
|
+
this._clients[client].send(data);
|
|
345
|
+
} catch(err) {
|
|
346
|
+
this.warn("An error occurred while sending: "+" "+client+" "+err.toString());
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
WebSocketListenerNode.prototype.reply = function(id,data) {
|
|
354
|
+
var session = this._clients[id];
|
|
355
|
+
if (session) {
|
|
356
|
+
try {
|
|
357
|
+
session.send(data);
|
|
358
|
+
}
|
|
359
|
+
catch(e) { // swallow any errors
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
WebSocketListenerNode.prototype.drop = function(id) {
|
|
365
|
+
var session = this._clients[id];
|
|
366
|
+
if (session) {
|
|
367
|
+
try {
|
|
368
|
+
session.close();
|
|
369
|
+
}
|
|
370
|
+
catch(e) { // swallow any errors
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function WebsocketOpenCloseNode(n) {
|
|
376
|
+
RED.nodes.createNode(this,n);
|
|
377
|
+
this.server = (n.client)?n.client:n.server;
|
|
378
|
+
var node = this;
|
|
379
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
380
|
+
if (this.serverConfig) {
|
|
381
|
+
this.serverConfig.registerOpenNode(this);
|
|
382
|
+
// TODO: nls
|
|
383
|
+
this.serverConfig.on('opened', function(event) {
|
|
384
|
+
node.status({
|
|
385
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
386
|
+
event:"connect",
|
|
387
|
+
_session: {type:"websocket",id:event.id}
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
this.serverConfig.on('erro', function(event) {
|
|
391
|
+
node.status({
|
|
392
|
+
fill:"red",shape:"ring",text:"error",
|
|
393
|
+
event:"error",
|
|
394
|
+
_session: {type:"websocket",id:event.id}
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
this.serverConfig.on('closed', function(event) {
|
|
398
|
+
var status;
|
|
399
|
+
if (event.count > 0) {
|
|
400
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
401
|
+
} else {
|
|
402
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
403
|
+
}
|
|
404
|
+
status.event = "disconnect";
|
|
405
|
+
status._session = {type:"websocket",id:event.id}
|
|
406
|
+
node.status(status);
|
|
407
|
+
});
|
|
408
|
+
} else {
|
|
409
|
+
this.error(RED._("Missing server configuration"));
|
|
410
|
+
}
|
|
411
|
+
this.on('close', function() {
|
|
412
|
+
if (node.serverConfig) {
|
|
413
|
+
node.serverConfig.removeInputNode(node);
|
|
414
|
+
}
|
|
415
|
+
node.status({});
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
RED.nodes.registerType("websocket_Open/Close", WebsocketOpenCloseNode);
|
|
419
|
+
|
|
420
|
+
function WebSocketInNode(n) {
|
|
421
|
+
RED.nodes.createNode(this,n);
|
|
422
|
+
this.server = (n.client)?n.client:n.server;
|
|
423
|
+
var node = this;
|
|
424
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
425
|
+
if (this.serverConfig) {
|
|
426
|
+
this.serverConfig.registerInputNode(this);
|
|
427
|
+
// TODO: nls
|
|
428
|
+
this.serverConfig.on('opened', function(event) {
|
|
429
|
+
node.status({
|
|
430
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
431
|
+
event:"connect",
|
|
432
|
+
_session: {type:"websocket",id:event.id}
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
this.serverConfig.on('erro', function(event) {
|
|
436
|
+
node.status({
|
|
437
|
+
fill:"red",shape:"ring",text:"error",
|
|
438
|
+
event:"error",
|
|
439
|
+
_session: {type:"websocket",id:event.id}
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
this.serverConfig.on('closed', function(event) {
|
|
443
|
+
var status;
|
|
444
|
+
if (event.count > 0) {
|
|
445
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
446
|
+
} else {
|
|
447
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
448
|
+
}
|
|
449
|
+
status.event = "disconnect";
|
|
450
|
+
status._session = {type:"websocket",id:event.id}
|
|
451
|
+
node.status(status);
|
|
452
|
+
});
|
|
453
|
+
} else {
|
|
454
|
+
this.error(RED._("Missing server configuration"));
|
|
455
|
+
}
|
|
456
|
+
this.on('close', function() {
|
|
457
|
+
if (node.serverConfig) {
|
|
458
|
+
node.serverConfig.removeInputNode(node);
|
|
459
|
+
}
|
|
460
|
+
node.status({});
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
RED.nodes.registerType("websocket_in",WebSocketInNode);
|
|
464
|
+
|
|
465
|
+
function WebSocketOutNode(n) {
|
|
466
|
+
RED.nodes.createNode(this,n);
|
|
467
|
+
var node = this;
|
|
468
|
+
this.server = (n.client)?n.client:n.server;
|
|
469
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
470
|
+
if (!this.serverConfig) {
|
|
471
|
+
return this.error(RED._("Missing server configuration"));
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
// TODO: nls
|
|
475
|
+
this.serverConfig.on('opened', function(event) {
|
|
476
|
+
node.status({
|
|
477
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
478
|
+
event:"connect",
|
|
479
|
+
_session: {type:"websocket",id:event.id}
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
this.serverConfig.on('erro', function(event) {
|
|
483
|
+
node.status({
|
|
484
|
+
fill:"red",shape:"ring",text:"error",
|
|
485
|
+
event:"error",
|
|
486
|
+
_session: {type:"websocket",id:event.id}
|
|
487
|
+
})
|
|
488
|
+
});
|
|
489
|
+
this.serverConfig.on('closed', function(event) {
|
|
490
|
+
var status;
|
|
491
|
+
if (event.count > 0) {
|
|
492
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
493
|
+
} else {
|
|
494
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
495
|
+
}
|
|
496
|
+
status.event = "disconnect";
|
|
497
|
+
status._session = {type:"websocket",id:event.id}
|
|
498
|
+
node.status(status);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
this.on("input", function(msg, nodeSend, nodeDone) {
|
|
502
|
+
var payload;
|
|
503
|
+
if (this.serverConfig.wholemsg) {
|
|
504
|
+
var sess;
|
|
505
|
+
if (msg._session) { sess = JSON.stringify(msg._session); }
|
|
506
|
+
delete msg._session;
|
|
507
|
+
payload = JSON.stringify(msg);
|
|
508
|
+
if (sess) { msg._session = JSON.parse(sess); }
|
|
509
|
+
}
|
|
510
|
+
else if (msg.hasOwnProperty("payload")) {
|
|
511
|
+
if (!Buffer.isBuffer(msg.payload)) { // if it's not a buffer make sure it's a string.
|
|
512
|
+
payload = RED.util.ensureString(msg.payload);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
payload = msg.payload;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (payload) {
|
|
519
|
+
if (msg._session && msg._session.type == "websocket") {
|
|
520
|
+
node.serverConfig.reply(msg._session.id,payload);
|
|
521
|
+
} else {
|
|
522
|
+
node.serverConfig.broadcast(payload,function(error) {
|
|
523
|
+
if (!!error) {
|
|
524
|
+
node.warn("An error occurred while sending: "+inspect(error));
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
nodeDone();
|
|
530
|
+
});
|
|
531
|
+
this.on('close', function() {
|
|
532
|
+
node.status({});
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
RED.nodes.registerType("websocket_out",WebSocketOutNode);
|
|
536
|
+
|
|
537
|
+
function WebSocketDropNode(n) {
|
|
538
|
+
RED.nodes.createNode(this,n);
|
|
539
|
+
var node = this;
|
|
540
|
+
this.server = (n.client)?n.client:n.server;
|
|
541
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
542
|
+
if (!this.serverConfig) {
|
|
543
|
+
return this.error(RED._("Missing server configuration"));
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
// TODO: nls
|
|
547
|
+
this.serverConfig.on('opened', function(event) {
|
|
548
|
+
node.status({
|
|
549
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
550
|
+
event:"connect",
|
|
551
|
+
_session: {type:"websocket",id:event.id}
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
this.serverConfig.on('erro', function(event) {
|
|
555
|
+
node.status({
|
|
556
|
+
fill:"red",shape:"ring",text:"error",
|
|
557
|
+
event:"error",
|
|
558
|
+
_session: {type:"websocket",id:event.id}
|
|
559
|
+
})
|
|
560
|
+
});
|
|
561
|
+
this.serverConfig.on('closed', function(event) {
|
|
562
|
+
var status;
|
|
563
|
+
if (event.count > 0) {
|
|
564
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
565
|
+
} else {
|
|
566
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
567
|
+
}
|
|
568
|
+
status.event = "disconnect";
|
|
569
|
+
status._session = {type:"websocket",id:event.id}
|
|
570
|
+
node.status(status);
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
this.on("input", function(msg, nodeDone) {
|
|
574
|
+
if (msg._session.id) {
|
|
575
|
+
this.serverConfig.drop(msg._session.id);
|
|
576
|
+
}
|
|
577
|
+
else {
|
|
578
|
+
node.warn("Valid msg._session required.");
|
|
579
|
+
}
|
|
580
|
+
nodeDone();
|
|
581
|
+
});
|
|
582
|
+
this.on('close', function() {
|
|
583
|
+
node.status({});
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
RED.nodes.registerType("websocket_Drop",WebSocketDropNode);
|
|
587
|
+
}
|