G
G
German Zvonchuk2015-10-20 14:06:05
linux
German Zvonchuk, 2015-10-20 14:06:05

How to allow cross domain connection with Node.js and Socket.io server?

Good afternoon
I installed NodeJS v4.2.1 on Debian Jessie, connected the Socket.IO v1.3.7 framework to deliver data via web socket to the browser and ran into the following problem.
I have a site valyuta.com/uploads/wss.html which connects to NodeJS which is installed on a new server wss://q.investaz.net:4000
When I connect I get an error:
socket.io-1.3.7.js: 3 WebSocket connection to 'wss://q.investaz.net:4000/socket.io/?EIO=3&transport=websocket' failed: WebSocket opening handshake was canceled
and get response from server "(Opcode -1)"
f54fca21e73643f9b252a83002ec57e5.JPGdd4eb272665d4ed0a8b65f2a1a6329c3.JPG
Server configuration is as follows :

var fs = require('fs');
var httpsOptions = {
        key: fs.readFileSync('/home/quotes/ssl/q.investaz.net.key'),
        cert: fs.readFileSync('/home/quotes/ssl/q.investaz.net.chained.crt'),
        ca: fs.readFileSync('/home/quotes/ssl/gd_bundle-g2-g1.crt'),
        requestCert: true,
        rejectUnauthorized: false
}

var app = require('https').createServer(httpsOptions, handler);
io = require('socket.io').listen(app);
app.listen(4000);

function handler (req, res) {
    fs.readFile(__dirname + '/index.html',
        function (err, data) {
            if (err) {
                res.writeHead(500);
                return res.end('Error loading index.html');
            }

            res.writeHead(200);
            res.end(data);
        });
}

io.on('connection', function (socket) {
    console.log('Server Listening: ' + 4000);
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});

This problem is solved in the following way, you need to contact https://q.investaz.net:4000/ after that, wss://q.investaz.net:4000 starts working normally.
So I want to note that this problem exists only in Google Chrome.
To verify this, just go to this site https://www.websocket.org/echo.html from different browsers and enter the following URL:
wss://q.investaz.net:4000/socket.io/?EIO= 3&transport=websocket Works
in Firefox, but not in Google Chrome.
I would be very grateful if you tell me how to solve this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2015-10-20
@Sanasol

Now nothing works anywhere.
It looks like the server is down.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question