Socket.io/node.js with ssl and Access-Control-Header

I have installed node server and am getting an error when activating ssl (official, NOT assignable certificate).

Error message:

XMLHttpRequest cannot load https://servername:8081/socket.io/... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://servername' is therefore not allowed access. results:1

      

But actually, I included the following code in the .htaccess file:

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin *
  Header set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
</IfModule>

      

The apache header module is installed.

my server:

var fs = require('fs');
var https = require('https');
var express = require ('express');

var options = {
    key: fs.readFileSync('path to key'),
    cert: fs.readFileSync('path to cert'),
    ca: fs.readFileSync('path to ca')
};

app = express()
https.createServer(options,app).listen(8081)
var io = require('socket.io')(https);

io.sockets.on('connection', function (socket) {
...
}

      

my client:

<script language="JavaScript">
    var socket = io.connect('https://servername:8081');
    socket.emit(...);
</script>

      

Does anyone have any idea what is wrong here? Thanks in advance!

+3


source to share


1 answer


Ok, I see a couple of things here:



  • You need to include socket.io lib in your client , for example:

    <script src="https://servername:8081/socket.io/socket.io.js"></script>
    
          

  • Set the safe parameter to true ( on your client )

    var socket = io.connect('https ://servername:8081', {secure: true});
    
          

  • And add these lines to your SSL options:

    agent: false, 
    requestCert: true,
    rejectUnauthorized: false
    
          

  • Then if it still doesn't work, you can try setting up the server using:

    io.set('match origin protocol', true);
    
          

0


source