-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttps-client.js
92 lines (68 loc) · 2.04 KB
/
https-client.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"use strict";
// A secure (TLS) https client
var https = require('https');
var fs = require('fs');
/*
var options = {
hostname: 'agent1',
port: 8443,
path: '/',
method: 'GET',
ca: [
fs.readFileSync('ssl/root-cert.pem'),
fs.readFileSync('ssl/ca1-cert.pem'),
fs.readFileSync('ssl/ca2-cert.pem'),
fs.readFileSync('ssl/ca3-cert.pem'),
fs.readFileSync('ssl/ca4-cert.pem')
],
key: fs.readFileSync('ssl/agent2-key.pem'),
cert: fs.readFileSync('ssl/agent2-cert.pem'),
rejectUnauthorized: true
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.log("##############################################");
console.log(d.toString());
console.log("##############################################");
});
});
req.end();
req.on('error', function(e) {
console.error("Error: ", e);
});
*/
// A secure websocket client.
https.globalAgent.options = {
ca: [
fs.readFileSync('ssl/root-cert.pem')
// fs.readFileSync('ssl/ca1-cert.pem'),
// fs.readFileSync('ssl/ca2-cert.pem'),
// fs.readFileSync('ssl/ca3-cert.pem'),
// fs.readFileSync('ssl/ca4-cert.pem')
],
key: fs.readFileSync('ssl/agent2-key.pem'),
cert: fs.readFileSync('ssl/agent2-cert.pem'),
rejectUnauthorized: true,
};
var io = require('socket.io-client');
var chatUrl = 'https://agent1:8443/chat';
var newsUrl = 'https://agent1:8443/news';
var chat = io.connect(chatUrl, {secure: true});
var news = io.connect(newsUrl, {secure: true});
chat.on('connect', function () {
console.log('Chat connected');
chat.emit('hi!');
});
chat.on('chat message', function (data) {
console.log('Chat:', data);
chat.emit('hi!');
});
news.on('connect', function () {
console.log('Chat connected');
});
news.on('item', function (data) {
console.log('news item:', data);
news.emit('woot');
});