【问题标题】:Switching socket.io to port 80 causing transport to switch to xhr polling将 socket.io 切换到端口 80 导致传输切换到 xhr 轮询
【发布时间】:2012-06-06 06:33:06
【问题描述】:

我的 3000 端口上的 socket.io 服务器运行良好。所有 socket.emit 和其他操作都通过 websocket 连接工作。

但是,一旦我尝试在端口 80 上运行服务器,更改

app.listen(3000) to app.listen(80)

并进行了所有必要的客户端更改,socket.io 停止使用 websocket 并切换到 xhr 轮询

warn  - websocket connection invalid
info  - transport end
debug - set close timeout for client 1830609591747620770
debug - cleared close timeout for client 1830609591747620770
debug - cleared heartbeat interval for client 1830609591747620770
debug - setting request GET /socket.io/1/websocket/1830609591747620770
debug - set heartbeat interval for client 1830609591747620770
warn  - websocket connection invalid
info  - transport end
debug - set close timeout for client 1830609591747620770
debug - cleared close timeout for client 1830609591747620770
debug - cleared heartbeat interval for client 1830609591747620770
debug - setting request GET /socket.io/1/xhr-polling/1830609591747620770?t=1338511205390
debug - setting poll timeout
debug - client authorized for 
debug - clearing poll timeout
debug - xhr-polling writing 1::
debug - set close timeout for client 1830609591747620770
debug - setting request GET /socket.io/1/xhr-polling/1830609591747620770?t=1338511205500
debug - setting poll timeout
debug - discarding transport

关于为什么它切换到 xhr 轮询以及我可以尝试什么的任何想法?

谢谢

编辑:: 我也在运行快速 Web 服务器和 Web 套接字服务器这一事实是否会成为问题?因为在我的代码中,我已经启动并运行了。如果这导致了问题,我该如何解决?

【问题讨论】:

    标签: node.js socket.io


    【解决方案1】:

    我还没有弄清楚为什么它切换到 XHR 轮询,但为了我的目的只是希望 websocket 服务器在端口 80 上工作,我解决问题的方法是使用 node-http-proxy 包https://github.com/nodejitsu/node-http-proxy

    我保持我的代码不变,但让代理在端口 80 上侦听,并将其路由到我的 websocket 正在侦听的端口 3000。

    【讨论】:

    • 这似乎在 ec2 为我解决了问题。 node-http-proxy 页面有一个紧凑的 ws 代理示例,非常简单。
    【解决方案2】:

    警告“websocket 连接无效”似乎表明防火墙/防病毒安全阻止端口 80 上的 web 套接字存在问题。有关此类问题的完整列表,请查看 Socket.io and firewall software

    使用http://websocketstest.com/ 的测试/诊断页面检查您的网络兼容性。

    希望这会有所帮助。

    【讨论】:

    • 我运行了测试,一切都通过了测试:websocketstest.com/result/164620 我有这个 websocket 服务器/web 服务器在 AWS EC2 上运行,这会是服务器端的问题吗?
    • 刚刚在 AWS EC2 的 80 端口上运行了一个 socket.io 应用程序。它使用 websockets 进行通信。所以AWS似乎不是问题。最初它抛出错误“无法调用未定义的方法'on'”。更新为 socket.io@0.8.7。不是它有效。只需检查您是否在 AWS 上运行了合适版本的 socket.io。
    • 我在 socket.io@0.9.6 所以应该没问题。
    猜你喜欢
    • 2013-10-09
    • 2016-03-10
    • 2013-09-12
    • 2014-07-18
    • 1970-01-01
    • 2020-12-30
    相关资源
    最近更新 更多