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

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

一切工作正常,并比浏览器开始显示错误消息“400错误请求”,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使用客户端时,我得到了更less的错误。

当我使用socket.io-1.0.6.js的本地参考时,错误发生频率更高。 找不到错误模式。

在erros之后,我重新启动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. 

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

您必须允许从所有域:

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

或者指定端口:

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

(请求另一个端口意味着CORS)