Skip to content

Commit 95faaac

Browse files
committed
TLS support
1 parent 683feb6 commit 95faaac

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

nodes/network/websocketnodes/websocketnodes.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Moddable Tech, Inc.
2+
* Copyright (c) 2022-2024 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*
@@ -42,10 +42,11 @@ class WebSocketClient extends Node {
4242
#reconnect;
4343
#options;
4444
#nodes;
45+
#tls;
4546

4647
onStart(config) {
4748
if (config.tls)
48-
throw new Error("unimplemented");
49+
this.#tls = RED.nodes.getNode(config.tls);
4950

5051
this.#options = {
5152
path: config.path,
@@ -55,7 +56,8 @@ class WebSocketClient extends Node {
5556
};
5657

5758
this.status(disconnected);
58-
this.#connect();
59+
60+
Timer.set(() => this.#connect());
5961
}
6062
onMessage(msg) {
6163
if (1 !== this.#ws?.readyState)
@@ -67,7 +69,34 @@ class WebSocketClient extends Node {
6769
this.#ws.send(msg.payload);
6870
}
6971
#connect() {
72+
let wss;
73+
if (this.#tls) {
74+
wss = {
75+
...device.network.ws,
76+
socket: {
77+
io: Modules.importNow("embedded:io/socket/tcp/tls"),
78+
TCP: {
79+
io: device.network.ws.socket.io
80+
},
81+
secure: {
82+
verify: this.#tls.options?.verifyservercert ?? true
83+
}
84+
}
85+
};
86+
const servername = this.#tls.options?.servername;
87+
if (servername) wss.socket.secure.serverName = servername;
88+
const ca = this.#tls.options?.ca;
89+
if (ca) wss.socket.secure.certificate = ca;
90+
const cert = this.#tls.options?.cert;
91+
if (cert) {
92+
wss.socket.secure.clientCertificates = [cert];
93+
const key = this.#tls.options?.key;
94+
if (key) wss.socket.secure.clientKey = key;
95+
}
96+
}
97+
7098
const options = this.#options, o = {};
99+
if (wss) o.wss = wss;
71100
({path: o.url, subprotocol: o.subprotocol, keepalive: o.keepalive} = options);
72101
this.#ws = new WebSocket(o);
73102
this.#ws.binaryType = "arraybuffer";

0 commit comments

Comments
 (0)