-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTLSSocketClient.js
More file actions
38 lines (37 loc) · 1.22 KB
/
TLSSocketClient.js
File metadata and controls
38 lines (37 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var tls = require('tls');
var fs = require('fs');
const PORT = 56565;
const HOST = 'localhost'
// Pass the certs to the server and let it know to process even unauthorized certs.
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert'),
rejectUnauthorized: false
};
var client = tls.connect(PORT, HOST, options, function () {
// Check if the authorization worked
if (client.authorized) {
console.log("Connection authorized by a Certificate Authority.");
} else {
console.log("Connection not authorized: " + client.authorizationError)
}
// Send a friendly message
client.write("I am the client sending you a message.");
});
client.on("data", function (data) {
console.log('Received: %s [it is %d bytes long]',
data.toString().replace(/(\n)/gm, ""),
data.length);
// Close the connection after receiving the message
client.end();
});
client.on('close', function () {
console.log("Connection closed");
});
// When an error ocoures, show it.
client.on('error', function (error) {
console.error(error);
// Close the connection after the error occurred.
client.destroy();
});