Socket.io 未设置 CORS 标头 [英] Socket.io doesn't set CORS header(s)
问题描述
我知道这个问题已经被问过几次了.
但是,我无法让任何这些解决方案发挥作用.
我正在运行 node.js 和 socket.io 的标准安装.(来自 Amazon EC2 上的 yum)
问题是 Chrome 正在回退到 xhr 轮询,而这些请求需要有效的 CORS 配置.但是,我似乎无法让它发挥作用.我的 Web 服务器在端口 80 上运行,而 node.js(socket.io) 在端口 81 上运行.如您所见,我试图让 socket.io 使用源策略.我也尝试使用*:*"作为来源,但没有运气.
这是我的代码:
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");
});
非常感谢!
推荐答案
这是我必须使用的语法来让 CORS 与 socket.io 一起工作:
This is the syntax I had to use to get CORS working with socket.io:
io.set( 'origins', '*domain.com*:*' );
如果涉及到它,请使用 console.log
以确保您在 中的
:Manager.prototype.handleHandshake
中输入此代码块./lib/manager.js
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';
}
}
这篇关于Socket.io 未设置 CORS 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!