node-red-contrib-websocket-server 0.0.1
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/lib/proxyHelper.js +219 -0
- package/package.json +25 -0
- package/websocket.html +388 -0
- package/websocket.js +585 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2016-2018 Rob Wu <rob@robwu.nl>
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
7
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
8
|
+
the Software without restriction, including without limitation the rights to
|
|
9
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
10
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
11
|
+
so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
This proxy helper is heavily based on the proxy helper from Rob Wu as detailed above.
|
|
19
|
+
It has been modified to work with the Node-RED runtime environment.
|
|
20
|
+
The license for the original code is reproduced above.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Parse a URL into its components.
|
|
26
|
+
* @param {String} url The URL to parse
|
|
27
|
+
* @returns {URL}
|
|
28
|
+
*/
|
|
29
|
+
const parseUrl = (url) => {
|
|
30
|
+
let parsedUrl = {
|
|
31
|
+
protocol: null,
|
|
32
|
+
host: null,
|
|
33
|
+
port: null,
|
|
34
|
+
hostname: null,
|
|
35
|
+
query: null,
|
|
36
|
+
href: null
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
if (!url) { return parsedUrl }
|
|
40
|
+
parsedUrl = new URL(url)
|
|
41
|
+
} catch (error) {
|
|
42
|
+
// dont throw error
|
|
43
|
+
}
|
|
44
|
+
return parsedUrl
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const DEFAULT_PORTS = {
|
|
48
|
+
ftp: 21,
|
|
49
|
+
gopher: 70,
|
|
50
|
+
http: 80,
|
|
51
|
+
https: 443,
|
|
52
|
+
ws: 80,
|
|
53
|
+
wss: 443,
|
|
54
|
+
mqtt: 1880,
|
|
55
|
+
mqtts: 8883
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const modeOverride = getEnv('NR_PROXY_MODE', {})
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @typedef {Object} ProxyOptions
|
|
62
|
+
* @property {'strict'|'legacy'} [mode] - Legacy mode is for non-strict previous proxy determination logic (for node-red <= v3.1 compatibility) (default 'strict')
|
|
63
|
+
* @property {boolean} [favourUpperCase] - Favour UPPER_CASE *_PROXY env vars (default false)
|
|
64
|
+
* @property {boolean} [lowerCaseOnly] - Prevent UPPER_CASE *_PROXY env vars being used. (default false)
|
|
65
|
+
* @property {boolean} [excludeNpm] - Prevent npm_config_*_proxy env vars being used. (default false)
|
|
66
|
+
* @property {object} [env] - The environment object to use (defaults to process.env)
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get the proxy URL for a given URL.
|
|
71
|
+
* @param {string|URL} url - The URL, or the result from url.parse.
|
|
72
|
+
* @param {ProxyOptions} [options] - The options object (optional)
|
|
73
|
+
* @return {string} The URL of the proxy that should handle the request to the
|
|
74
|
+
* given URL. If no proxy is set, this will be an empty string.
|
|
75
|
+
*/
|
|
76
|
+
function getProxyForUrl(url, options) {
|
|
77
|
+
url = url || ''
|
|
78
|
+
const defaultOptions = {
|
|
79
|
+
mode: 'strict',
|
|
80
|
+
lowerCaseOnly: false,
|
|
81
|
+
favourUpperCase: false,
|
|
82
|
+
excludeNpm: false,
|
|
83
|
+
}
|
|
84
|
+
options = Object.assign({}, defaultOptions, options)
|
|
85
|
+
|
|
86
|
+
if (modeOverride === 'legacy' || modeOverride === 'strict') {
|
|
87
|
+
options.mode = modeOverride
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (options.mode === 'legacy') {
|
|
91
|
+
return legacyGetProxyForUrl(url, options.env || process.env)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {}
|
|
95
|
+
let proto = parsedUrl.protocol
|
|
96
|
+
let hostname = parsedUrl.host
|
|
97
|
+
let port = parsedUrl.port
|
|
98
|
+
if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') {
|
|
99
|
+
return '' // Don't proxy URLs without a valid scheme or host.
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
proto = proto.split(':', 1)[0]
|
|
103
|
+
// Stripping ports in this way instead of using parsedUrl.hostname to make
|
|
104
|
+
// sure that the brackets around IPv6 addresses are kept.
|
|
105
|
+
hostname = hostname.replace(/:\d*$/, '')
|
|
106
|
+
port = parseInt(port) || DEFAULT_PORTS[proto] || 0
|
|
107
|
+
if (!shouldProxy(hostname, port, options)) {
|
|
108
|
+
return '' // Don't proxy URLs that match NO_PROXY.
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let proxy =
|
|
112
|
+
getEnv('npm_config_' + proto + '_proxy', options) ||
|
|
113
|
+
getEnv(proto + '_proxy', options) ||
|
|
114
|
+
getEnv('npm_config_proxy', options) ||
|
|
115
|
+
getEnv('all_proxy', options)
|
|
116
|
+
if (proxy && proxy.indexOf('://') === -1) {
|
|
117
|
+
// Missing scheme in proxy, default to the requested URL's scheme.
|
|
118
|
+
proxy = proto + '://' + proxy
|
|
119
|
+
}
|
|
120
|
+
return proxy
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Get the proxy URL for a given URL.
|
|
125
|
+
* For node-red < v3.1 or compatibility mode
|
|
126
|
+
* @param {string} url The URL to check for proxying
|
|
127
|
+
* @param {object} [env] The environment object to use (default process.env)
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
function legacyGetProxyForUrl(url, env) {
|
|
131
|
+
env = env || process.env
|
|
132
|
+
let prox, noprox;
|
|
133
|
+
if (env.http_proxy) { prox = env.http_proxy; }
|
|
134
|
+
if (env.HTTP_PROXY) { prox = env.HTTP_PROXY; }
|
|
135
|
+
if (env.no_proxy) { noprox = env.no_proxy.split(","); }
|
|
136
|
+
if (env.NO_PROXY) { noprox = env.NO_PROXY.split(","); }
|
|
137
|
+
|
|
138
|
+
let noproxy = false;
|
|
139
|
+
if (noprox) {
|
|
140
|
+
for (let i in noprox) {
|
|
141
|
+
if (url.indexOf(noprox[i].trim()) !== -1) { noproxy=true; }
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (prox && !noproxy) {
|
|
145
|
+
return prox
|
|
146
|
+
}
|
|
147
|
+
return ""
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Determines whether a given URL should be proxied.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} hostname - The host name of the URL.
|
|
154
|
+
* @param {number} port - The effective port of the URL.
|
|
155
|
+
* @returns {boolean} Whether the given URL should be proxied.
|
|
156
|
+
* @private
|
|
157
|
+
*/
|
|
158
|
+
function shouldProxy(hostname, port, options) {
|
|
159
|
+
const NO_PROXY =
|
|
160
|
+
(getEnv('npm_config_no_proxy', options) || getEnv('no_proxy', options)).toLowerCase()
|
|
161
|
+
if (!NO_PROXY) {
|
|
162
|
+
return true // Always proxy if NO_PROXY is not set.
|
|
163
|
+
}
|
|
164
|
+
if (NO_PROXY === '*') {
|
|
165
|
+
return false // Never proxy if wildcard is set.
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return NO_PROXY.split(/[,\s]/).every(function (proxy) {
|
|
169
|
+
if (!proxy) {
|
|
170
|
+
return true // Skip zero-length hosts.
|
|
171
|
+
}
|
|
172
|
+
const parsedProxy = proxy.match(/^(.+):(\d+)$/)
|
|
173
|
+
let parsedProxyHostname = parsedProxy ? parsedProxy[1] : proxy
|
|
174
|
+
const parsedProxyPort = parsedProxy ? parseInt(parsedProxy[2]) : 0
|
|
175
|
+
if (parsedProxyPort && parsedProxyPort !== port) {
|
|
176
|
+
return true // Skip if ports don't match.
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!/^[.*]/.test(parsedProxyHostname)) {
|
|
180
|
+
// No wildcards, so stop proxying if there is an exact match.
|
|
181
|
+
return hostname !== parsedProxyHostname
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (parsedProxyHostname.charAt(0) === '*') {
|
|
185
|
+
// Remove leading wildcard.
|
|
186
|
+
parsedProxyHostname = parsedProxyHostname.slice(1)
|
|
187
|
+
}
|
|
188
|
+
// Stop proxying if the hostname ends with the no_proxy host.
|
|
189
|
+
return !hostname.endsWith(parsedProxyHostname)
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Get the value for an environment constiable.
|
|
195
|
+
*
|
|
196
|
+
* @param {string} key - The name of the environment constiable.
|
|
197
|
+
* @param {ProxyOptions} options - The name of the environment constiable.
|
|
198
|
+
* @return {string} The value of the environment constiable.
|
|
199
|
+
* @private
|
|
200
|
+
*/
|
|
201
|
+
function getEnv(key, options) {
|
|
202
|
+
const env = (options && options.env) || process.env
|
|
203
|
+
if (options && options.excludeNpm === true) {
|
|
204
|
+
if (key.startsWith('npm_config_')) {
|
|
205
|
+
return ''
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (options && options.lowerCaseOnly === true) {
|
|
209
|
+
return env[key.toLowerCase()] || ''
|
|
210
|
+
} else if (options && options.favourUpperCase === true) {
|
|
211
|
+
return env[key.toUpperCase()] || env[key.toLowerCase()] || ''
|
|
212
|
+
}
|
|
213
|
+
return env[key.toLowerCase()] || env[key.toUpperCase()] || ''
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
module.exports = {
|
|
217
|
+
getProxyForUrl,
|
|
218
|
+
parseUrl
|
|
219
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-red-contrib-websocket-server",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "websocket test with websocket open/close event information",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"websocket",
|
|
7
|
+
"open/close",
|
|
8
|
+
"server"
|
|
9
|
+
],
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"author": "Peter Durfina",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/peterko18/node-red-contrib-websocket-server"
|
|
15
|
+
},
|
|
16
|
+
"main": "websocket.js",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
19
|
+
},
|
|
20
|
+
"node-red" : {
|
|
21
|
+
"nodes": {
|
|
22
|
+
"websockets": "websocket.js"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
package/websocket.html
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
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
|
+
<!-- WebSocket Input Node -->
|
|
17
|
+
<script type="text/html" data-template-name="websocket_in">
|
|
18
|
+
<div class="form-row" id="websocket_server-row">
|
|
19
|
+
<label for="node-input-server"><i class="fa fa-bookmark"></i> <span>Path</span></label>
|
|
20
|
+
<input type="text" id="node-input-server">
|
|
21
|
+
</div>
|
|
22
|
+
<div class="form-row">
|
|
23
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label>
|
|
24
|
+
<input type="text" id="node-input-name" name="Name">
|
|
25
|
+
</div>
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<script type="text/javascript">
|
|
29
|
+
|
|
30
|
+
(function() {
|
|
31
|
+
|
|
32
|
+
const headerTypes = [
|
|
33
|
+
/*
|
|
34
|
+
{ value: "Accept", label: "Accept", hasValue: false },
|
|
35
|
+
{ value: "Accept-Encoding", label: "Accept-Encoding", hasValue: false },
|
|
36
|
+
{ value: "Accept-Language", label: "Accept-Language", hasValue: false },
|
|
37
|
+
*/
|
|
38
|
+
{ value: "Authorization", label: "Authorization", hasValue: false },
|
|
39
|
+
/*
|
|
40
|
+
{ value: "Content-Type", label: "Content-Type", hasValue: false },
|
|
41
|
+
{ value: "Cache-Control", label: "Cache-Control", hasValue: false },
|
|
42
|
+
*/
|
|
43
|
+
{ value: "User-Agent", label: "User-Agent", hasValue: false },
|
|
44
|
+
/*
|
|
45
|
+
{ value: "Location", label: "Location", hasValue: false },
|
|
46
|
+
*/
|
|
47
|
+
{ value: "other", label: RED._("other"),
|
|
48
|
+
hasValue: true, icon: "red/images/typedInput/az.svg" },
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
const headerOptions = {};
|
|
52
|
+
const defaultOptions = [
|
|
53
|
+
{ value: "other", label: RED._("other"),
|
|
54
|
+
hasValue: true, icon: "red/images/typedInput/az.svg" },
|
|
55
|
+
"env",
|
|
56
|
+
];
|
|
57
|
+
/*
|
|
58
|
+
headerOptions["accept"] = [
|
|
59
|
+
{ value: "text/plain", label: "text/plain", hasValue: false },
|
|
60
|
+
{ value: "text/html", label: "text/html", hasValue: false },
|
|
61
|
+
{ value: "application/json", label: "application/json", hasValue: false },
|
|
62
|
+
{ value: "application/xml", label: "application/xml", hasValue: false },
|
|
63
|
+
...defaultOptions,
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
headerOptions["accept-encoding"] = [
|
|
67
|
+
{ value: "gzip", label: "gzip", hasValue: false },
|
|
68
|
+
{ value: "deflate", label: "deflate", hasValue: false },
|
|
69
|
+
{ value: "compress", label: "compress", hasValue: false },
|
|
70
|
+
{ value: "br", label: "br", hasValue: false },
|
|
71
|
+
{ value: "gzip, deflate", label: "gzip, deflate", hasValue: false },
|
|
72
|
+
{ value: "gzip, deflate, br", label: "gzip, deflate, br", hasValue: false },
|
|
73
|
+
...defaultOptions,
|
|
74
|
+
];
|
|
75
|
+
headerOptions["accept-language"] = [
|
|
76
|
+
{ value: "*", label: "*", hasValue: false },
|
|
77
|
+
{ value: "en-GB, en-US, en;q=0.9", label: "en-GB, en-US, en;q=0.9", hasValue: false },
|
|
78
|
+
{ value: "de-AT, de-DE;q=0.9, en;q=0.5", label: "de-AT, de-DE;q=0.9, en;q=0.5", hasValue: false },
|
|
79
|
+
{ value: "es-mx,es,en;q=0.5", label: "es-mx,es,en;q=0.5", hasValue: false },
|
|
80
|
+
{ value: "fr-CH, fr;q=0.9, en;q=0.8", label: "fr-CH, fr;q=0.9, en;q=0.8", hasValue: false },
|
|
81
|
+
{ value: "zh-CN, zh-TW; q = 0.9, zh-HK; q = 0.8, zh; q = 0.7, en; q = 0.6", label: "zh-CN, zh-TW; q = 0.9, zh-HK; q = 0.8, zh; q = 0.7, en; q = 0.6", hasValue: false },
|
|
82
|
+
{ value: "ja-JP, jp", label: "ja-JP, jp", hasValue: false },
|
|
83
|
+
...defaultOptions,
|
|
84
|
+
];
|
|
85
|
+
headerOptions["content-type"] = [
|
|
86
|
+
{ value: "text/css", label: "text/css", hasValue: false },
|
|
87
|
+
{ value: "text/plain", label: "text/plain", hasValue: false },
|
|
88
|
+
{ value: "text/html", label: "text/html", hasValue: false },
|
|
89
|
+
{ value: "application/json", label: "application/json", hasValue: false },
|
|
90
|
+
{ value: "application/octet-stream", label: "application/octet-stream", hasValue: false },
|
|
91
|
+
{ value: "application/pdf", label: "application/pdf", hasValue: false },
|
|
92
|
+
{ value: "application/xml", label: "application/xml", hasValue: false },
|
|
93
|
+
{ value: "application/zip", label: "application/zip", hasValue: false },
|
|
94
|
+
{ value: "multipart/form-data", label: "multipart/form-data", hasValue: false },
|
|
95
|
+
{ value: "audio/aac", label: "audio/aac", hasValue: false },
|
|
96
|
+
{ value: "audio/ac3", label: "audio/ac3", hasValue: false },
|
|
97
|
+
{ value: "audio/basic", label: "audio/basic", hasValue: false },
|
|
98
|
+
{ value: "audio/mp4", label: "audio/mp4", hasValue: false },
|
|
99
|
+
{ value: "audio/ogg", label: "audio/ogg", hasValue: false },
|
|
100
|
+
{ value: "image/bmp", label: "image/bmp", hasValue: false },
|
|
101
|
+
{ value: "image/gif", label: "image/gif", hasValue: false },
|
|
102
|
+
{ value: "image/jpeg", label: "image/jpeg", hasValue: false },
|
|
103
|
+
{ value: "image/png", label: "image/png", hasValue: false },
|
|
104
|
+
{ value: "image/tiff", label: "image/tiff", hasValue: false },
|
|
105
|
+
...defaultOptions,
|
|
106
|
+
];
|
|
107
|
+
headerOptions["cache-control"] = [
|
|
108
|
+
{ value: "max-age=0", label: "max-age=0", hasValue: false },
|
|
109
|
+
{ value: "max-age=86400", label: "max-age=86400", hasValue: false },
|
|
110
|
+
{ value: "no-cache", label: "no-cache", hasValue: false },
|
|
111
|
+
...defaultOptions,
|
|
112
|
+
];
|
|
113
|
+
*/
|
|
114
|
+
headerOptions["user-agent"] = [
|
|
115
|
+
{ value: "Mozilla/5.0", label: "Mozilla/5.0", hasValue: false },
|
|
116
|
+
...defaultOptions,
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
function getHeaderOptions(headerName) {
|
|
120
|
+
const lc = (headerName || "").toLowerCase();
|
|
121
|
+
let opts = headerOptions[lc];
|
|
122
|
+
return opts || defaultOptions;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function ws_label() {
|
|
126
|
+
var nodeid = (this.client)?this.client:this.server;
|
|
127
|
+
var wsNode = RED.nodes.node(nodeid);
|
|
128
|
+
return this.name||(wsNode?"[ws] "+wsNode.label():"websocket");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function ws_validateserver() {
|
|
132
|
+
if ($("#node-input-mode").val() === 'client' || (this.client && !this.server)) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
if (RED.nodes.node(this.server) != null) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
return RED._("Missing server configuration");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
RED.nodes.registerType('websocket_Open/Close',{
|
|
145
|
+
category: 'websocket server',
|
|
146
|
+
defaults: {
|
|
147
|
+
name: {value:""},
|
|
148
|
+
server: {type:"websocket_server", validate: ws_validateserver}
|
|
149
|
+
},
|
|
150
|
+
color:"rgb(252, 93, 53)",
|
|
151
|
+
inputs:0,
|
|
152
|
+
outputs:1,
|
|
153
|
+
icon: "white-globe.svg",
|
|
154
|
+
labelStyle: function() {
|
|
155
|
+
return this.name?"node_label_italic":"";
|
|
156
|
+
},
|
|
157
|
+
label: ws_label
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
RED.nodes.registerType('websocket_Drop',{
|
|
161
|
+
category: 'websocket server',
|
|
162
|
+
defaults: {
|
|
163
|
+
name: {value:""},
|
|
164
|
+
server: {type:"websocket_server", validate: ws_validateserver}
|
|
165
|
+
},
|
|
166
|
+
color:"rgb(252, 93, 53)",
|
|
167
|
+
inputs:1,
|
|
168
|
+
outputs:0,
|
|
169
|
+
icon: "white-globe.svg",
|
|
170
|
+
labelStyle: function() {
|
|
171
|
+
return this.name?"node_label_italic":"";
|
|
172
|
+
},
|
|
173
|
+
label: ws_label
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
RED.nodes.registerType('websocket_in',{
|
|
177
|
+
category: 'websocket server',
|
|
178
|
+
defaults: {
|
|
179
|
+
name: {value:""},
|
|
180
|
+
server: {type:"websocket_server", validate: ws_validateserver}
|
|
181
|
+
},
|
|
182
|
+
color:"rgb(252, 93, 53)",
|
|
183
|
+
inputs:0,
|
|
184
|
+
outputs:1,
|
|
185
|
+
icon: "white-globe.svg",
|
|
186
|
+
labelStyle: function() {
|
|
187
|
+
return this.name?"node_label_italic":"";
|
|
188
|
+
},
|
|
189
|
+
label: ws_label
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
RED.nodes.registerType('websocket_out',{
|
|
193
|
+
category: 'websocket server',
|
|
194
|
+
defaults: {
|
|
195
|
+
name: {value:""},
|
|
196
|
+
server: {type:"websocket_server", validate: ws_validateserver}
|
|
197
|
+
},
|
|
198
|
+
color:"rgb(252, 93, 53)",
|
|
199
|
+
inputs:1,
|
|
200
|
+
outputs:0,
|
|
201
|
+
icon: "white-globe.svg",
|
|
202
|
+
align: "right",
|
|
203
|
+
labelStyle: function() {
|
|
204
|
+
return this.name?"node_label_italic":"";
|
|
205
|
+
},
|
|
206
|
+
label: ws_label,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
RED.nodes.registerType('websocket_server',{
|
|
210
|
+
category: 'config',
|
|
211
|
+
defaults: {
|
|
212
|
+
path: {value:"",required:true,
|
|
213
|
+
label:RED._("Path"),
|
|
214
|
+
validate:RED.validators.regex(/^((?!\/debug\/ws).)*$/)},
|
|
215
|
+
wholemsg: {value:"false"},
|
|
216
|
+
pingaction: {value:"none"},
|
|
217
|
+
hb: {
|
|
218
|
+
value: "1",
|
|
219
|
+
label:RED._("Send heartbeat"),
|
|
220
|
+
validate: RED.validators.number() }
|
|
221
|
+
},
|
|
222
|
+
inputs:0,
|
|
223
|
+
outputs:0,
|
|
224
|
+
label: function() {
|
|
225
|
+
var root = RED.settings.httpNodeRoot;
|
|
226
|
+
if (root.slice(-1) != "/") {
|
|
227
|
+
root = root+"/";
|
|
228
|
+
}
|
|
229
|
+
if (this.path) {
|
|
230
|
+
if (this.path.charAt(0) == "/") {
|
|
231
|
+
root += this.path.slice(1);
|
|
232
|
+
} else {
|
|
233
|
+
root += this.path;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return root;
|
|
237
|
+
},
|
|
238
|
+
oneditprepare: function() {
|
|
239
|
+
var root = RED.settings.httpNodeRoot;
|
|
240
|
+
if (root.slice(-1) == "/") {
|
|
241
|
+
root = root.slice(0,-1);
|
|
242
|
+
}
|
|
243
|
+
if (root === "") {
|
|
244
|
+
$("#node-config-ws-tip").hide();
|
|
245
|
+
} else {
|
|
246
|
+
$("#node-config-ws-path").html(RED._("This path will be relative to <code>__path__</code>.", { path: root }));
|
|
247
|
+
$("#node-config-ws-tip").show();
|
|
248
|
+
}
|
|
249
|
+
var ping = this.pingaction.value;
|
|
250
|
+
if (ping === 'none'){
|
|
251
|
+
$("#node-config-pingoption").hide();
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
if (ping === 'send'){
|
|
255
|
+
$("#node-config-input-pingaction option[value=send]").prop('selected', true)
|
|
256
|
+
}
|
|
257
|
+
else if (ping === 'receive'){
|
|
258
|
+
$("#node-config-input-pingaction option[value=receive]").prop('selected', true)
|
|
259
|
+
}
|
|
260
|
+
$("#node-config-pingoption").show();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
$("#node-config-input-pingaction").on("change", function() {
|
|
264
|
+
if ( $("#node-config-input-pingaction").val() === 'none') {
|
|
265
|
+
$("#node-config-pingoption").hide();
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
if ($("#node-config-input-pingaction").val() === 'send'){
|
|
269
|
+
$("#node-config-input-hb-row").text("Send ping interval");
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
$("#node-config-input-hb-row").text("Receive ping timeout");
|
|
273
|
+
}
|
|
274
|
+
$("#node-config-pingoption").show();
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
if (this.pingaction !== "none"){
|
|
279
|
+
if (this.pingaction == "send"){
|
|
280
|
+
$("#node-config-input-hb-row").text("Send ping interval");
|
|
281
|
+
}
|
|
282
|
+
else{
|
|
283
|
+
$("#node-config-input-hb-row").text("Receive ping timeout");
|
|
284
|
+
}
|
|
285
|
+
$("#node-config-pingoption").show();
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
$("#node-config-input-hb").on("change", function() {
|
|
289
|
+
if ( $("#node-config-input-hb").val() == "0") {
|
|
290
|
+
$("#node-config-input-hb").val("1");
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
oneditresize: function(size) {
|
|
296
|
+
const dlg = $("#dialog-form");
|
|
297
|
+
const expandRow = dlg.find('.node-input-headers-container-row');
|
|
298
|
+
let height = dlg.height() - 5;
|
|
299
|
+
if(expandRow && expandRow.length){
|
|
300
|
+
const siblingRows = dlg.find('> .form-row:not(.node-input-headers-container-row)');
|
|
301
|
+
for (let i = 0; i < siblingRows.size(); i++) {
|
|
302
|
+
const cr = $(siblingRows[i]);
|
|
303
|
+
if(cr.is(":visible"))
|
|
304
|
+
height -= cr.outerHeight(true);
|
|
305
|
+
}
|
|
306
|
+
$("#node-input-headers-container").editableList('height',height);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
})();
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
</script>
|
|
314
|
+
|
|
315
|
+
<!-- WebSocket out Node -->
|
|
316
|
+
<script type="text/html" data-template-name="websocket_out">
|
|
317
|
+
<div class="form-row" id="websocket_server-row">
|
|
318
|
+
<label for="node-input-server"><i class="fa fa-bookmark"></i> <span>Path</span></label>
|
|
319
|
+
<input type="text" id="node-input-server">
|
|
320
|
+
</div>
|
|
321
|
+
<div class="form-row">
|
|
322
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label>
|
|
323
|
+
<input type="text" id="node-input-name" name="Name">
|
|
324
|
+
</div>
|
|
325
|
+
</script>
|
|
326
|
+
|
|
327
|
+
<!-- WebSocket Server configuration node -->
|
|
328
|
+
<script type="text/html" data-template-name="websocket_server">
|
|
329
|
+
<div class="form-row">
|
|
330
|
+
<label for="node-config-input-path"><i class="fa fa-bookmark"></i> <span>Path</span></label>
|
|
331
|
+
<input id="node-config-input-path" type="text" placeholder="/ws/example">
|
|
332
|
+
</div>
|
|
333
|
+
<div class="form-row">
|
|
334
|
+
<label for="node-config-input-wholemsg">Send/Receive</label>
|
|
335
|
+
<select type="text" id="node-config-input-wholemsg" style="width: 70%;">
|
|
336
|
+
<option value="false">Payload</option>
|
|
337
|
+
<option value="true">Entire message</option>
|
|
338
|
+
</select>
|
|
339
|
+
</div>
|
|
340
|
+
<div class="form-row">
|
|
341
|
+
<label for="node-config-input-pingaction">Ping Action</label>
|
|
342
|
+
<select type="text" id="node-config-input-pingaction" style="width: 70%;">
|
|
343
|
+
<option value="none">None</option>
|
|
344
|
+
<option value="send">Send ping</option>
|
|
345
|
+
<option value="receive">Receive ping</option>
|
|
346
|
+
</select>
|
|
347
|
+
</div>
|
|
348
|
+
<div class="form-row" id="node-config-pingoption" style="display: flex; align-items: center; min-height: 34px">
|
|
349
|
+
<span id="node-config-input-hb-row" style="margin: 0 8px; width:auto">Send ping</span>
|
|
350
|
+
<span id="node-config-input-hb-row">
|
|
351
|
+
<input type="number" style="width: 70px; margin-right: 3px" id="node-config-input-hb" min="1" value="1">
|
|
352
|
+
<span>seconds</span>
|
|
353
|
+
</span>
|
|
354
|
+
</div>
|
|
355
|
+
<div class="form-tips">
|
|
356
|
+
<span>By 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.</span>
|
|
357
|
+
<p id="node-config-ws-tip"><span id="node-config-ws-path"></span></p>
|
|
358
|
+
<span id="test"></span>
|
|
359
|
+
</div>
|
|
360
|
+
</script>
|
|
361
|
+
|
|
362
|
+
<!-- WebSocket open Node -->
|
|
363
|
+
<script type="text/html" data-template-name="websocket_Open/Close">
|
|
364
|
+
|
|
365
|
+
<div class="form-row" id="websocket_server-row">
|
|
366
|
+
<label for="node-input-server"><i class="fa fa-bookmark"></i> <span>Path</span></label>
|
|
367
|
+
<input type="text" id="node-input-server">
|
|
368
|
+
</div>
|
|
369
|
+
|
|
370
|
+
<div class="form-row">
|
|
371
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label>
|
|
372
|
+
<input type="text" id="node-input-name" name="Name">
|
|
373
|
+
</div>
|
|
374
|
+
</script>
|
|
375
|
+
|
|
376
|
+
<script type="text/html" data-template-name="websocket_Drop">
|
|
377
|
+
|
|
378
|
+
<div class="form-row" id="websocket_server-row">
|
|
379
|
+
<label for="node-input-server"><i class="fa fa-bookmark"></i> <span>Path</span></label>
|
|
380
|
+
<input type="text" id="node-input-server">
|
|
381
|
+
</div>
|
|
382
|
+
|
|
383
|
+
<div class="form-row">
|
|
384
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label>
|
|
385
|
+
<input type="text" id="node-input-name" name="Name">
|
|
386
|
+
</div>
|
|
387
|
+
</script>
|
|
388
|
+
|
package/websocket.js
ADDED
|
@@ -0,0 +1,585 @@
|
|
|
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
|
+
|
|
143
|
+
|
|
144
|
+
//dorobit reakciu na ping v resp pri kazdom novom pripojeni nastavit casovac ze ma ocakavat ping,
|
|
145
|
+
//ak nepride tak ukonci spojenie
|
|
146
|
+
//ak pride restuje casovac a zacne pocitat odznovu
|
|
147
|
+
|
|
148
|
+
if (node.pingaction !== "none")
|
|
149
|
+
{
|
|
150
|
+
//clearInterval(node.heartbeatInterval);
|
|
151
|
+
node.heartbeatInterval[id] = setInterval(function() {
|
|
152
|
+
if (node.pingaction === "send"){
|
|
153
|
+
if (socket.nrPendingPong){
|
|
154
|
+
var msg = {};
|
|
155
|
+
//node.warn("No pong received " + node.heartbeat);
|
|
156
|
+
msg._session = {"id":id};
|
|
157
|
+
//node.serverConfig.drop(msg._session.id);
|
|
158
|
+
node.drop(msg._session.id);
|
|
159
|
+
socket.nrErrorHandler(new Error("timeout"));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
//node.warn("No pong received " + node.heartbeat);
|
|
163
|
+
socket.nrPendingPong = true;
|
|
164
|
+
try {
|
|
165
|
+
socket.ping();
|
|
166
|
+
} catch(err) {
|
|
167
|
+
node.warn(err);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else if (node.pingaction === "receive") {
|
|
171
|
+
if (socket.nrPendingPing){
|
|
172
|
+
var msg = {};
|
|
173
|
+
//node.warn("No pong received " + node.heartbeat);
|
|
174
|
+
msg._session = {"id":id};
|
|
175
|
+
//node.serverConfig.drop(msg._session.id);
|
|
176
|
+
node.drop(msg._session.id);
|
|
177
|
+
socket.nrErrorHandler(new Error("timeout"));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
},node.heartbeat);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
socket.on('close',function() {
|
|
185
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
186
|
+
if (node.isServer) {
|
|
187
|
+
delete node._clients[id];
|
|
188
|
+
node.emit('closed',{count:Object.keys(node._clients).length,id:id});
|
|
189
|
+
node.handleOpenCloseEvent(id,socket,'close');
|
|
190
|
+
} else {
|
|
191
|
+
node.emit('closed',{count:'',id:id});
|
|
192
|
+
}
|
|
193
|
+
if (!node.closing && !node.isServer) {
|
|
194
|
+
clearTimeout(node.tout);
|
|
195
|
+
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
socket.on('message',function(data,flags) {
|
|
199
|
+
node.handleEvent(id,socket,'message',data,flags);
|
|
200
|
+
});
|
|
201
|
+
socket.nrErrorHandler = function(err) {
|
|
202
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
203
|
+
node.emit('erro',{err:err,id:id});
|
|
204
|
+
if (!node.closing && !node.isServer) {
|
|
205
|
+
clearTimeout(node.tout);
|
|
206
|
+
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
socket.on('error',socket.nrErrorHandler);
|
|
210
|
+
socket.on('ping', function() {
|
|
211
|
+
socket.nrPendingPing = false;
|
|
212
|
+
})
|
|
213
|
+
socket.on('pong', function() {
|
|
214
|
+
socket.nrPendingPong = false;
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (node.isServer) {
|
|
219
|
+
if (!serverUpgradeAdded) {
|
|
220
|
+
RED.server.on('upgrade', handleServerUpgrade);
|
|
221
|
+
serverUpgradeAdded = true
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
var path = RED.settings.httpNodeRoot || "/";
|
|
225
|
+
path = path + (path.slice(-1) == "/" ? "":"/") + (node.path.charAt(0) == "/" ? node.path.substring(1) : node.path);
|
|
226
|
+
node.fullPath = path;
|
|
227
|
+
|
|
228
|
+
if (listenerNodes.hasOwnProperty(path)) {
|
|
229
|
+
node.error(RED._("Cannot have two WebSocket listeners on the same path: __path__",{path: node.path}));
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
listenerNodes[node.fullPath] = node;
|
|
233
|
+
var serverOptions = {
|
|
234
|
+
noServer: true
|
|
235
|
+
}
|
|
236
|
+
if (RED.settings.webSocketNodeVerifyClient) {
|
|
237
|
+
serverOptions.verifyClient = RED.settings.webSocketNodeVerifyClient;
|
|
238
|
+
}
|
|
239
|
+
// Create a WebSocket Server
|
|
240
|
+
node.server = new ws.Server(serverOptions);
|
|
241
|
+
node.server.setMaxListeners(0);
|
|
242
|
+
node.server.on('connection', handleConnection);
|
|
243
|
+
// Not adding server-initiated heartbeats yet
|
|
244
|
+
// node.heartbeatInterval = setInterval(function() {
|
|
245
|
+
// node.server.clients.forEach(function(ws) {
|
|
246
|
+
// if (ws.nrPendingHeartbeat) {
|
|
247
|
+
// // No pong received
|
|
248
|
+
// ws.terminate();
|
|
249
|
+
// ws.nrErrorHandler(new Error("timeout"));
|
|
250
|
+
// return;
|
|
251
|
+
// }
|
|
252
|
+
// ws.nrPendingHeartbeat = true;
|
|
253
|
+
// ws.ping();
|
|
254
|
+
// });
|
|
255
|
+
// })
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
node.closing = false;
|
|
259
|
+
startconn(); // start outbound connection
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
node.on("close", function(done) {
|
|
263
|
+
/*if (node.heartbeatInterval[id]) {
|
|
264
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
265
|
+
}*/
|
|
266
|
+
try {
|
|
267
|
+
clearInterval(node.heartbeatInterval[id]);
|
|
268
|
+
}
|
|
269
|
+
catch(e) { // swallow any errors
|
|
270
|
+
}
|
|
271
|
+
if (node.isServer) {
|
|
272
|
+
delete listenerNodes[node.fullPath];
|
|
273
|
+
node.server.close();
|
|
274
|
+
node._inputNodes = [];
|
|
275
|
+
node._openNodes = [];
|
|
276
|
+
done();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
RED.nodes.registerType("websocket_server",WebSocketListenerNode);
|
|
282
|
+
|
|
283
|
+
WebSocketListenerNode.prototype.registerInputNode = function(/*Node*/handler) {
|
|
284
|
+
this._inputNodes.push(handler);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
WebSocketListenerNode.prototype.registerOpenNode = function(/*Node*/handler) {
|
|
288
|
+
this._openNodes.push(handler);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
WebSocketListenerNode.prototype.removeInputNode = function(/*Node*/handler) {
|
|
292
|
+
this._inputNodes.forEach(function(node, i, inputNodes) {
|
|
293
|
+
if (node === handler) {
|
|
294
|
+
inputNodes.splice(i, 1);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
this._openNodes.forEach(function(node, i, openNodes) {
|
|
298
|
+
if (node === handler) {
|
|
299
|
+
openNodes.splice(i, 1);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
WebSocketListenerNode.prototype.handleEvent = function(id,/*socket*/socket,/*String*/event,/*Object*/data,/*Object*/flags) {
|
|
305
|
+
var msg;
|
|
306
|
+
if (this.wholemsg) {
|
|
307
|
+
try {
|
|
308
|
+
msg = JSON.parse(data);
|
|
309
|
+
if (typeof msg !== "object" && !Array.isArray(msg) && (msg !== null)) {
|
|
310
|
+
msg = { payload:msg };
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
catch(err) {
|
|
314
|
+
msg = { payload:data };
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
msg = {
|
|
318
|
+
payload:data
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
msg._session = {type:"websocket",id:id};
|
|
322
|
+
for (var i = 0; i < this._inputNodes.length; i++) {
|
|
323
|
+
this._inputNodes[i].send(msg);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
WebSocketListenerNode.prototype.handleOpenCloseEvent = function(id,/*socket*/socket,/*String*/event,) {
|
|
328
|
+
|
|
329
|
+
var msg;
|
|
330
|
+
msg = {"event" : event};
|
|
331
|
+
msg._session = {type:"websocket",id:id};
|
|
332
|
+
for (var i = 0; i < this._openNodes.length; i++) {
|
|
333
|
+
this._openNodes[i].send(msg);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
WebSocketListenerNode.prototype.broadcast = function(data) {
|
|
339
|
+
for (let client in this._clients) {
|
|
340
|
+
if (this._clients.hasOwnProperty(client)) {
|
|
341
|
+
try {
|
|
342
|
+
this._clients[client].send(data);
|
|
343
|
+
} catch(err) {
|
|
344
|
+
this.warn("An error occurred while sending: "+" "+client+" "+err.toString());
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
WebSocketListenerNode.prototype.reply = function(id,data) {
|
|
352
|
+
var session = this._clients[id];
|
|
353
|
+
if (session) {
|
|
354
|
+
try {
|
|
355
|
+
session.send(data);
|
|
356
|
+
}
|
|
357
|
+
catch(e) { // swallow any errors
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
WebSocketListenerNode.prototype.drop = function(id) {
|
|
363
|
+
var session = this._clients[id];
|
|
364
|
+
if (session) {
|
|
365
|
+
try {
|
|
366
|
+
session.close();
|
|
367
|
+
}
|
|
368
|
+
catch(e) { // swallow any errors
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function WebsocketOpenCloseNode(n) {
|
|
374
|
+
RED.nodes.createNode(this,n);
|
|
375
|
+
this.server = (n.client)?n.client:n.server;
|
|
376
|
+
var node = this;
|
|
377
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
378
|
+
if (this.serverConfig) {
|
|
379
|
+
this.serverConfig.registerOpenNode(this);
|
|
380
|
+
// TODO: nls
|
|
381
|
+
this.serverConfig.on('opened', function(event) {
|
|
382
|
+
node.status({
|
|
383
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
384
|
+
event:"connect",
|
|
385
|
+
_session: {type:"websocket",id:event.id}
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
this.serverConfig.on('erro', function(event) {
|
|
389
|
+
node.status({
|
|
390
|
+
fill:"red",shape:"ring",text:"error",
|
|
391
|
+
event:"error",
|
|
392
|
+
_session: {type:"websocket",id:event.id}
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
this.serverConfig.on('closed', function(event) {
|
|
396
|
+
var status;
|
|
397
|
+
if (event.count > 0) {
|
|
398
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
399
|
+
} else {
|
|
400
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
401
|
+
}
|
|
402
|
+
status.event = "disconnect";
|
|
403
|
+
status._session = {type:"websocket",id:event.id}
|
|
404
|
+
node.status(status);
|
|
405
|
+
});
|
|
406
|
+
} else {
|
|
407
|
+
this.error(RED._("Missing server configuration"));
|
|
408
|
+
}
|
|
409
|
+
this.on('close', function() {
|
|
410
|
+
if (node.serverConfig) {
|
|
411
|
+
node.serverConfig.removeInputNode(node);
|
|
412
|
+
}
|
|
413
|
+
node.status({});
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
RED.nodes.registerType("websocket_Open/Close", WebsocketOpenCloseNode);
|
|
417
|
+
|
|
418
|
+
function WebSocketInNode(n) {
|
|
419
|
+
RED.nodes.createNode(this,n);
|
|
420
|
+
this.server = (n.client)?n.client:n.server;
|
|
421
|
+
var node = this;
|
|
422
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
423
|
+
if (this.serverConfig) {
|
|
424
|
+
this.serverConfig.registerInputNode(this);
|
|
425
|
+
// TODO: nls
|
|
426
|
+
this.serverConfig.on('opened', function(event) {
|
|
427
|
+
node.status({
|
|
428
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
429
|
+
event:"connect",
|
|
430
|
+
_session: {type:"websocket",id:event.id}
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
this.serverConfig.on('erro', function(event) {
|
|
434
|
+
node.status({
|
|
435
|
+
fill:"red",shape:"ring",text:"error",
|
|
436
|
+
event:"error",
|
|
437
|
+
_session: {type:"websocket",id:event.id}
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
this.serverConfig.on('closed', function(event) {
|
|
441
|
+
var status;
|
|
442
|
+
if (event.count > 0) {
|
|
443
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
444
|
+
} else {
|
|
445
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
446
|
+
}
|
|
447
|
+
status.event = "disconnect";
|
|
448
|
+
status._session = {type:"websocket",id:event.id}
|
|
449
|
+
node.status(status);
|
|
450
|
+
});
|
|
451
|
+
} else {
|
|
452
|
+
this.error(RED._("Missing server configuration"));
|
|
453
|
+
}
|
|
454
|
+
this.on('close', function() {
|
|
455
|
+
if (node.serverConfig) {
|
|
456
|
+
node.serverConfig.removeInputNode(node);
|
|
457
|
+
}
|
|
458
|
+
node.status({});
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
RED.nodes.registerType("websocket_in",WebSocketInNode);
|
|
462
|
+
|
|
463
|
+
function WebSocketOutNode(n) {
|
|
464
|
+
RED.nodes.createNode(this,n);
|
|
465
|
+
var node = this;
|
|
466
|
+
this.server = (n.client)?n.client:n.server;
|
|
467
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
468
|
+
if (!this.serverConfig) {
|
|
469
|
+
return this.error(RED._("Missing server configuration"));
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
// TODO: nls
|
|
473
|
+
this.serverConfig.on('opened', function(event) {
|
|
474
|
+
node.status({
|
|
475
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
476
|
+
event:"connect",
|
|
477
|
+
_session: {type:"websocket",id:event.id}
|
|
478
|
+
});
|
|
479
|
+
});
|
|
480
|
+
this.serverConfig.on('erro', function(event) {
|
|
481
|
+
node.status({
|
|
482
|
+
fill:"red",shape:"ring",text:"error",
|
|
483
|
+
event:"error",
|
|
484
|
+
_session: {type:"websocket",id:event.id}
|
|
485
|
+
})
|
|
486
|
+
});
|
|
487
|
+
this.serverConfig.on('closed', function(event) {
|
|
488
|
+
var status;
|
|
489
|
+
if (event.count > 0) {
|
|
490
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
491
|
+
} else {
|
|
492
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
493
|
+
}
|
|
494
|
+
status.event = "disconnect";
|
|
495
|
+
status._session = {type:"websocket",id:event.id}
|
|
496
|
+
node.status(status);
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
this.on("input", function(msg, nodeSend, nodeDone) {
|
|
500
|
+
var payload;
|
|
501
|
+
if (this.serverConfig.wholemsg) {
|
|
502
|
+
var sess;
|
|
503
|
+
if (msg._session) { sess = JSON.stringify(msg._session); }
|
|
504
|
+
delete msg._session;
|
|
505
|
+
payload = JSON.stringify(msg);
|
|
506
|
+
if (sess) { msg._session = JSON.parse(sess); }
|
|
507
|
+
}
|
|
508
|
+
else if (msg.hasOwnProperty("payload")) {
|
|
509
|
+
if (!Buffer.isBuffer(msg.payload)) { // if it's not a buffer make sure it's a string.
|
|
510
|
+
payload = RED.util.ensureString(msg.payload);
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
payload = msg.payload;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (payload) {
|
|
517
|
+
if (msg._session && msg._session.type == "websocket") {
|
|
518
|
+
node.serverConfig.reply(msg._session.id,payload);
|
|
519
|
+
} else {
|
|
520
|
+
node.serverConfig.broadcast(payload,function(error) {
|
|
521
|
+
if (!!error) {
|
|
522
|
+
node.warn("An error occurred while sending: "+inspect(error));
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
nodeDone();
|
|
528
|
+
});
|
|
529
|
+
this.on('close', function() {
|
|
530
|
+
node.status({});
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
RED.nodes.registerType("websocket_out",WebSocketOutNode);
|
|
534
|
+
|
|
535
|
+
function WebSocketDropNode(n) {
|
|
536
|
+
RED.nodes.createNode(this,n);
|
|
537
|
+
var node = this;
|
|
538
|
+
this.server = (n.client)?n.client:n.server;
|
|
539
|
+
this.serverConfig = RED.nodes.getNode(this.server);
|
|
540
|
+
if (!this.serverConfig) {
|
|
541
|
+
return this.error(RED._("Missing server configuration"));
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
// TODO: nls
|
|
545
|
+
this.serverConfig.on('opened', function(event) {
|
|
546
|
+
node.status({
|
|
547
|
+
fill:"green",shape:"dot",text:RED._("connected " + event.count),
|
|
548
|
+
event:"connect",
|
|
549
|
+
_session: {type:"websocket",id:event.id}
|
|
550
|
+
});
|
|
551
|
+
});
|
|
552
|
+
this.serverConfig.on('erro', function(event) {
|
|
553
|
+
node.status({
|
|
554
|
+
fill:"red",shape:"ring",text:"error",
|
|
555
|
+
event:"error",
|
|
556
|
+
_session: {type:"websocket",id:event.id}
|
|
557
|
+
})
|
|
558
|
+
});
|
|
559
|
+
this.serverConfig.on('closed', function(event) {
|
|
560
|
+
var status;
|
|
561
|
+
if (event.count > 0) {
|
|
562
|
+
status = {fill:"green",shape:"dot",text:RED._("connected " + event.count)};
|
|
563
|
+
} else {
|
|
564
|
+
status = {fill:"red",shape:"ring",text:"disconnected"};
|
|
565
|
+
}
|
|
566
|
+
status.event = "disconnect";
|
|
567
|
+
status._session = {type:"websocket",id:event.id}
|
|
568
|
+
node.status(status);
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
this.on("input", function(msg, nodeDone) {
|
|
572
|
+
if (msg._session.id) {
|
|
573
|
+
this.serverConfig.drop(msg._session.id);
|
|
574
|
+
}
|
|
575
|
+
else {
|
|
576
|
+
node.warn("Valid msg._session required.");
|
|
577
|
+
}
|
|
578
|
+
nodeDone();
|
|
579
|
+
});
|
|
580
|
+
this.on('close', function() {
|
|
581
|
+
node.status({});
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
RED.nodes.registerType("websocket_Drop",WebSocketDropNode);
|
|
585
|
+
}
|