1

So I've been working with socket.io and NodeJS quite some time now, but I've never tried to use socket.io over an secured socket layer.

Recently I've been trying to get this to work with a valid certificate. However, everytime I try to connect to the socket.io server, the following error message appears in the chrome console;

XMLHttpRequest cannot load domain:12457/socket.io/1/?t=1403871407291. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'domain' is therefore not allowed access.

As far as I can see, the domain has to be in the Access-Control-Allow-Origin header. I've added that specific header to the HTTPS server using the following code;

var https           = require ('https');
var fs              = require ('fs');
var options         = {
                    key: fs.readFileSync('./cert/private.key'),
                    cert: fs.readFileSync('./cert/certificate.crt'),    
                    //requestCert: true,
                    ca: [ fs.readFileSync('./cert/ca.pem'), fs.readFileSync('./cert/sub.class1.server.ca.pem') ]
                };

var server          = https.createServer (options, function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "domain");
res.writeHead (200);
res.end ("Nothing to see here :o");
});

var socketio            = require ('socket.io').listen (server);

server.listen (12457);

When I run the server with the Access-Control-Allow-Origin added, I receive the same error. However, when I request the headers with a separate GET request, I receive the following:

HTTP/1.1 200 OK => 
Access-Control-Allow-Origin => domain
Date => Fri, 27 Jun 2014 12:18:45 GMT
Connection => close

Note that the Access-Control-Allow-Origin is set this time with the right domain.

I somehow got the feeling that the HTTPS server is setting the headers for the HTTPS server it self, but not for the socket.io server. I have no clue how to fix this problem what so ever, so my question is;

Does someone knows what I am doing wrong?

Sam
  • 7,252
  • 16
  • 46
  • 65
  • Try this answer from a related question: http://stackoverflow.com/a/6601067/1671886 – ctlacko Jun 27 '14 at 16:32
  • possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – jww Jun 27 '14 at 22:40
  • I forgot to mention that I use "var socket = io.connect ('domain:12457', {secure: true});" to connect. But the secure parameter doesn't solve the problem. – user3783118 Jun 28 '14 at 16:10

0 Answers0