Socket.io通过apache服务器聊天(也运行php主站点)

时间:2012-12-25 08:03:29

标签: apache node.js socket.io cors

我一直在尝试为我的网站提供一个解决方案,以便与socket.io很好地配合。现在我有apache服务我的网站(主要是在PHP),我正在尝试使用socket.io(节点)实现实时聊天支持功能。我能够单独使用聊天功能(使用快递和去wundertutor.com:8000)但是当我去wundertutor.com(其中包括聊天功能)时,我遇到了CORS(Cross)的问题-origin资源共享)。这是我的错误(在客户端):

XMLHttpRequest cannot load http://chat.wundertutor.com/socket.io/1/?t=1356421805823. Origin http://wundertutor.com is not allowed by Access-Control-Allow-Origin.

OR

XMLHttpRequest cannot load http://chat.wundertutor.com/socket.io/1/?t=1356422229000. Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true.

取决于我是否在我的app.js中包含这行代码

io.set("origins","http://wundertutor.com");

基本上,我只是想让我的节点应用程序成为我的主站点的补充,但我真的不知道在哪里指定CORS参数。我已尝试在我的.htaccess文件,app.js以及客户端javascript中执行此操作,但没有一个证明是成功的。

(另外,我觉得奇怪的是,当我去wundertutor.com:8000时,我的节点应用程序正确打开了一个websocket,但是当我尝试去wundertutor.com时,它试图使用XHR轮询最终导致这个CORS一团糟)

真的很感激任何建议!

1 个答案:

答案 0 :(得分:2)

如果其他人遇到同样的问题,我想出了我的问题。虽然我仍然认为我想要做的是使用CORS,但我不必在Apache或node.js端进行任何特定的Access-Allow-Origin-Control设置。我为解决我的问题所做的是从node.js服务器而不是apache服务器提供socket.io javascript文件。这使得来自socket.io javascript文件的请求与加载javascript文件的地址具有相同的来源。这是一个例子:

<script src="http://wundertutor.com:8000/socket.io/socket.io.js"> </script> 
(node server - correct)

VS

<script src="/javascripts/socket.io.js"></script>
(apache server - wrong)
相关问题