-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
ssl.js
41 lines (32 loc) · 1.04 KB
/
ssl.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
'use strict';
const https = require('https');
const fs = require('fs');
const { WebSocket, WebSocketServer } = require('..');
const server = https.createServer({
cert: fs.readFileSync('../test/fixtures/certificate.pem'),
key: fs.readFileSync('../test/fixtures/key.pem')
});
const wss = new WebSocketServer({ server });
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
ws.on('message', function message(msg) {
console.log(msg.toString());
});
});
server.listen(function listening() {
//
// If the `rejectUnauthorized` option is not `false`, the server certificate
// is verified against a list of well-known CAs. An 'error' event is emitted
// if verification fails.
//
// The certificate used in this example is self-signed so `rejectUnauthorized`
// is set to `false`.
//
const ws = new WebSocket(`wss://localhost:${server.address().port}`, {
rejectUnauthorized: false
});
ws.on('error', console.error);
ws.on('open', function open() {
ws.send('All glory to WebSockets!');
});
});