Есть сервер (взят с официальной документации).
const https = require('https');
const fs = require('fs');
const WebSocket = require('ws');
const server = https.createServer({
cert: fs.readFileSync('webhook_cert.pem'),
key: fs.readFileSync('webhook_pkey.pem')
});
const wss = new WebSocket.Server({server});
wss.on('connection', function connection(ws) {
ws.on('message', function message(msg) {
console.log(msg);
});
});
server.listen(3000, function listening() {
const ws = new WebSocket(`wss://localhost:${server.address().port}`, {
rejectUnauthorized: false
});
ws.on('open', function open() {
ws.send('All glory to WebSockets!');
});
});
Есть клиент:
websocketClient: function () {
const ws = new WebSocket('wss://site.ru:3000/')
ws.onopen = () => console.log('ONLINE')
ws.onclose = () => console.log('DISCONNECT')
ws.onmessage = response => console.log(response.data)
}
При подключении вылазит ошибка:
WebSocket connection to 'wss://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
Как правильно настроить WebSocket'ы?