node.js 意外的响应代码:将 socket.io 与 AppFog 一起使用时出现 502 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12569451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:21:03  来源:igfitidea点击:

Unexpected response code: 502 error when using socket.io with AppFog

node.jssocket.ioappfog

提问by Andrew Liu

The error

错误

When the user opens http://sync.aws.af.cm/, the Google Chrome JavaScript Console shows (within a few seconds):

当用户打开http://sync.aws.af.cm/ 时,Google Chrome JavaScript 控制台显示(在几秒钟内):

Unexpected response code: 502 error

The app should remember the contents of the text area even after the user refreshes the page. The app works locally, but not when I upload it to AppFog, and I suspect the problem is the 502 error.

即使在用户刷新页面之后,应用程序也应该记住文本区域的内容。该应用程序可在本地运行,但当我将其上传到 AppFog 时则无法运行,我怀疑问题出在 502 错误上。

Do you know what might cause this error in this case?

您知道在这种情况下可能导致此错误的原因是什么吗?

What I've tried

我试过的

I tried changing the script src=<path_to_socket_io>path in my client from /socket.io/socket.io.jsto node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.jsas described here, but it did not help.

我尝试按照此处所述将script src=<path_to_socket_io>客户端中的路径从/socket.io/socket.io.js更改node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js为,但没有帮助。

I also tried serving the app at Heroku instead of AppFog, but in that case I kept running into the issue described hereeven though I specified socket.io version 0.9.6.

我还尝试在 Heroku 而不是 AppFog 上提供应用程序,但在这种情况下即使我指定了 socket.io 版本 0.9.6 ,我仍然遇到了这里描述的问题。

I've also looked at the following questions, but the comments so far don't seem to help me.

我还查看了以下问题,但到目前为止的评论似乎对我没有帮助。

socket.io creating 502 Bad Gateway on connect / websocket invalid

socket.io 在连接上创建 502 Bad Gateway / websocket 无效

Socket.IO node.js websocket connection invalid port 8081

Socket.IO node.js websocket连接无效端口8081

socket.io with node.js not working as expected

socket.io 与 node.js 未按预期工作

Relevant server code

相关服务器代码

Here is the relevant server code:

这是相关的服务器代码:

var server = http.createServer(function (req, res) {
  ...
});

var io = require('socket.io').listen(server);

// Define events that can be triggered by the client
io.sockets.on('connection', function (socket) {
  socket.on('setServerVariable', function (value) {
    ...
  });
  ...
});

server.listen(process.env.VCAPP_APP_PORT || 3000);

Relevant client code

相关客户代码

<script src="/socket.io/socket.io.js"></script>

<script>
  window.onload = function () {
    ...
    var socket = io.connect(window.location.hostname);
    socket.emit('getServerVariable');
    ...
  };
...

Server log

服务器日志

$ af logs sync

====> /logs/stdout.log <====

info: socket.io started
debug: served static content /socket.io.js
debug: client authorized
info: handshake authorized veNnUVtoDTIz-oudG7Pa
debug: setting request GET /socket.io/1/websocket/veNnUVtoDTIz-oudG7Pa
debug: set heartbeat interval for client veNnUVtoDTIz-oudG7Pa
warn: websocket connection invalid
info: transport end (undefined)
debug: set close timeout for client veNnUVtoDTIz-oudG7Pa
debug: cleared close timeout for client veNnUVtoDTIz-oudG7Pa
debug: cleared heartbeat interval for client veNnUVtoDTIz-oudG7Pa
debug: served static content /socket.io.js
debug: client authorized
info: handshake authorized 6AKyhROUNjqujk_uG7Pb
debug: setting request GET /socket.io/1/websocket/6AKyhROUNjqujk_uG7Pb
debug: set heartbeat interval for client 6AKyhROUNjqujk_uG7Pb
warn: websocket connection invalid
info: transport end (undefined)
debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: cleared heartbeat interval for client 6AKyhROUNjqujk_uG7Pb
debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504130465
debug: setting poll timeout
debug: client authorized for 
debug: clearing poll timeout
debug: xhr-polling writing 1::
debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504130724
debug: setting poll timeout
debug: discarding transport
debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: xhr-polling received data packet 5:::{"name":"getServerVariable"}
debug: clearing poll timeout
debug: xhr-polling writing 5:::{"name":"printLog"}
debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
server.js: getServerVariable: 
debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504130980
debug: setting poll timeout
debug: clearing poll timeout
debug: xhr-polling writing ???23???5:::{"name":"printLog"}???39???5:::{"name":"setItemValue","args":[""]}???23???5:::{"name":"printLog"}
debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: discarding transport
debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504131261
debug: setting poll timeout
debug: discarding transport
debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: clearing poll timeout
debug: xhr-polling writing 8::
debug: set close timeout for client 6AKyhROUNjqujk_uG7Pb
debug: xhr-polling closed due to exceeded duration
debug: setting request GET /socket.io/1/xhr-polling/6AKyhROUNjqujk_uG7Pb?t=1348504151659
debug: setting poll timeout
debug: discarding transport
debug: cleared close timeout for client 6AKyhROUNjqujk_uG7Pb

回答by Wes Johnson

AppFog doesn't support websockets yet. Following their recent acquisition of Nodester, they announced it would be coming in a few months.

AppFog 还不支持 websockets。在他们最近收购 Nodester 之后,他们宣布将在几个月内推出。

By adding io.set('transports', ['xhr-polling']);you're just setting the transport to ajax long polling. By default, Socket.io determines the best transport based on browser capabilities and the current connection. Websockets is ideal, when it's available, but if it's not Socket.io will fall back to xhr-polling among other transports.

通过添加,io.set('transports', ['xhr-polling']);您只是将传输设置为 ajax 长轮询。默认情况下,Socket.io 根据浏览器功能和当前连接确定最佳传输方式。Websockets 是理想的,当它可用时,但如果它不是 Socket.io 将回退到其他传输中的 xhr-polling。

I received the same 502 unexpected response / bad gatewayerror myself when using CloudFlare which is also unable to proxy websocket connections via its nginx-based infrastructure (note that as of 2014, Cloudflare can proxy websockets if you're on a premium plan).

502 unexpected response / bad gateway自己在使用 CloudFlare 时也收到了同样的错误,它也无法通过其基于 nginx 的基础设施代理 websocket 连接(请注意,截至 2014 年,如果您使用的是高级计划,Cloudflare 可以代理 websockets)。