Socket.IO客户端无法与服务器通信

时间:2022-01-03 15:48:30

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.

所以我目前正在将Node.js应用程序从本地计算机移动到生产服务器。 Socket.IO(版本0.6.17)似乎在我的服务器上工作正常,但是在尝试与服务器通信时客户端出错。

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):

以下是Google Chrome控制台每隔几秒(连续)输出的内容:

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.

有人提到它可能是由于客户端和socket.io版本之间的版本差异,但我不知道如何检查客户端版本。

2 个解决方案

#1


3  

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).

为了安全起见,我将使用随服务器一起分发的socket.io客户端脚本。将脚本标记更改为引用“/socket.io/socket.io.js”(我认为)并让您的应用程序为其提供服务。这也将使Flash套接字工作,而不必将其置于不安全模式(因为跨域问题)。

#2


0  

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.

结果证明问题是WebSockets不发送正常的HTTP请求,导致它们被nginx破坏。通过与我的托管公司的Q / A讨论,他们说解决方案是购买专用于侦听WebSocket连接的私有IP。

I ended up going with Nodester to host my Node.js app, though.

不过,我最终还是和Nodester一起主持了我的Node.js应用程序。

#1


3  

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).

为了安全起见,我将使用随服务器一起分发的socket.io客户端脚本。将脚本标记更改为引用“/socket.io/socket.io.js”(我认为)并让您的应用程序为其提供服务。这也将使Flash套接字工作,而不必将其置于不安全模式(因为跨域问题)。

#2


0  

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.

结果证明问题是WebSockets不发送正常的HTTP请求,导致它们被nginx破坏。通过与我的托管公司的Q / A讨论,他们说解决方案是购买专用于侦听WebSocket连接的私有IP。

I ended up going with Nodester to host my Node.js app, though.

不过,我最终还是和Nodester一起主持了我的Node.js应用程序。