Socket.io 1.0.6随机错误请求400错误

时间:2014-07-25 16:44:18

标签: node.js websocket redis socket.io cors

我在端口8082上使用Node.js,在端口80上使用Apache。

一切正常,浏览器开始显示错误消息" 400 Bad Request",CORS错误。

服务器正在设置CORS标头。如您所见,我也使用Redis适配器。

var io = require('socket.io').listen(8082);
io.adapter(redis({ host: '127.0.0.1', port: 6379 }));
io.set('origins', 'domain.com:*');

不能说为什么有些时候一切正常,有时候不行。 当Socket.io尝试从池升级到websocket时,总会发生错误。

当我使用来自https://cdn.socket.io/socket.io-1.0.6.js的客户端时,错误减少了。

当我使用socket.io-1.0.6.js的本地引用时,错误会以更高的频率发生。 无法找到错误模式。

在错误之后,我重新启动Node.js服务器,尝试一些请求,一次又一次地工作,错误。有时无需重新启动服务器即可再次运行。

请求标题样本

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Cookie  io=RjKzZ6Y1OTQeSEsPAAAL; SGM_DESENV=cb5ae798c2f6f5d3f38c6ed16a6e4696
Host    200.238.251.79:8082
Origin  http://200.238.251.79
Referer http://200.238.251.79/maximiliano/sgp/admin/custodiacompartilhada/add
User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0

响应标头示例

Access-Control-Allow-Credentials    true
Access-Control-Allow-Origin http://200.238.251.79
Connection  keep-alive
Content-Length  101
Content-Type    application/octet-stream
Date    Fri, 25 Jul 2014 16:10:26 GMT
Set-Cookie  io=9OYcCmU24IyzrAS3AAAM

错误消息

NetworkError: 400 Bad Request - http://200.238.251.79:8082/socket.io/?EIO=2&transport=polling&t=1406304539856-2&sid=RjKzZ6Y1OTQeSEsPAAAL

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://200.238.251.79:8082/socket.io/?EIO=2&transport=polling&t=1406304539856-2&sid=RjKzZ6Y1OTQeSEsPAAAL This can be fixed by moving the resource to the same domain or enabling CORS.

1 个答案:

答案 0 :(得分:0)

CORS不允许在域名中使用通配符。

您必须允许来自所有域:

io.set('origins', '*');

或指定端口:

io.set('origins', 'http://200.238.251.79:8082');

(在另一个端口上请求CORS)