Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

http - Socket.io doesn't set CORS header(s)


I know this question has been asked a couple of times.
However, I can't get any those solutions to work.
I'm running a standard install of node.js and socket.io. (From yum on Amazon EC2)
The problem is that Chrome is falling back to xhr polling, and those requests require a working CORS configuration. However, I can't seem to get it to work. My web server is running on port 80, and node.js(socket.io) is running on port 81. I have tried to get socket.io to use a origin policy as you can see. I have also tried to use "* : *" as origin with no luck.
Here's my code:

var http = require('http');
var io = require('socket.io').listen(81, {origins: '*'});

io.configure( function(){
    io.set('origin', '*');
});
io.set("origins","*");

var server = http.createServer(function(req, res) {
    io.sockets.emit("message", "test");
res.writeHead(200);
    res.end('Hello Http');
    console.log("Message recieved!");
});
server.listen(82);

io.sockets.on('connection', function(client)  {
    console.log("New Connection");
});

Thank you very much!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the syntax I had to use to get CORS working with socket.io:

io.set( 'origins', '*domain.com*:*' );

If it comes to it, use console.log to make sure you're entering this block of code in Manager.prototype.handleHandshake inside ./lib/manager.js:

 if (origin) {
    // https://developer.mozilla.org/En/HTTP_Access_Control
    headers['Access-Control-Allow-Origin'] = '*';

    if (req.headers.cookie) {
      headers['Access-Control-Allow-Credentials'] = 'true';
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...