Answer the question
In order to leave comments, you need to log in
How to connect to WebSocket'y?
I'm trying to connect to a socket with the line:
WebSocket connection to 'wss://kulonful.ru:8002/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED
var socket = new WebSocket("wss://kulonful.ru:8002/");
socket.onopen = function() {
console.log('- соединение с сокетом установлено');
};
socket.onerror = function(error) {
console.log('Произошла ошибка: ' + error.message);
};
socket.onmessage = function(event) {
console.log('Получение данных: ' + event.data);
};
var app = require('express')(),
http = require('http'),
https = require('https'),
fs = require('fs'),
bodyParser = require('body-parser'),
request = require('request'),
sql = require('mysql'),
WebSocketLib = new require('ws');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
var optionssl = {
key: fs.readFileSync('privatekey.pem'),
cert: fs.readFileSync('certificate.pem')
},
clients = {};
http.createServer(app).listen(8001);
var SecureServer = https.createServer(optionssl, app).listen(8000);
var WebSocket = new WebSocketLib.Server({
port: 8002,
httpsServer: SecureServer,
verifyClient: true
});
console.log(WebSocket);
WebSocket.on('connection', function(ws) {
var id = Math.random();
clients[id] = ws;
console.log('Новое соединение ' + id);
ws.on('message', function(message) {
console.log('Получены данные: ' + message);
});
ws.on('close', function() {
console.log('соединение закрыто ' + id);
delete clients[id];
});
})
Answer the question
In order to leave comments, you need to log in
if the verifyClient option is set, then it must be a function that either returns true/false (accept the handshake or not) or calls a 2-argument callback
https://github.com/websockets/ws/blob/master/doc/ws.md
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question