Socket.IO 客户端未与服务器通信
因此,我目前正在将 Node.js 应用程序从本地计算机移动到生产服务器。 Socket.IO(版本 0.6.17)似乎在我的服务器上工作正常,但客户端在尝试与服务器通信时出错。
这是一些代码:
<script src='http://cdn.socket.io/stable/socket.io.js'>
// ...
var socket = new io.Socket(null, { port: 47556 }).connect();
// ...
</script>
这是 Google Chrome 的控制台每隔几秒(连续)输出的内容:
GET http://mysite.com:47556/socket.io/xhr-polling//1304476733618 undefined (undefined)
有人提到这可能是由于客户端和 socket.io 版本之间的版本差异造成的,但我不知道如何检查客户端版本。
So I'm currently moving a Node.js application from my local computer to a production server. Socket.IO (version 0.6.17) seems to work fine on my server, but the client errors out when trying to communicate to the server.
Here's some code:
<script src='http://cdn.socket.io/stable/socket.io.js'>
// ...
var socket = new io.Socket(null, { port: 47556 }).connect();
// ...
</script>
And here's what Google Chrome's console outputs after every few seconds (continuously):
GET http://mysite.com:47556/socket.io/xhr-polling//1304476733618 undefined (undefined)
Someone mentioned that it may be due to a version difference between the client and socket.io versions, but I don't know how I'd check the client side version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了安全起见,我将使用与服务器一起分发的 socket.io 客户端脚本。更改您的脚本标记以引用“/socket.io/socket.io.js”(我认为)并让您的应用程序提供服务。这也将使 Flash Sockets 无需将其置于不安全模式(由于跨域问题)即可工作。
To be safe, I would use the socket.io client script distributed with the server. Change your script tag to reference "/socket.io/socket.io.js" (I think) and let your app serve it. This will also Flash Sockets work without having to put it in insecure mode (because of the cross-domain issue).
事实证明,问题在于 WebSocket 不发送正常的 HTTP 请求,导致它们被 nginx 丢弃。来自问答讨论我的托管公司,他们说解决方案是购买一个专用 IP,专门用于监听 WebSocket 连接。
不过,我最终还是使用 Nodester 来托管我的 Node.js 应用程序。
Turns out the problem was that WebSockets don't send normal HTTP requests, resulting in their getting trashed by nginx. From a Q/A discussion with my hosting company, they said the solution would be to buy a private IP which would be specially used to listen for WebSocket connections.
I ended up going with Nodester to host my Node.js app, though.