18

错误

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

Unexpected response code: 502 error

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

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

我试过的

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

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

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

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

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

带有 node.js 的 socket.io 没有按预期工作

相关服务器代码

这是相关的服务器代码:

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

相关客户端代码

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

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

服务器日志

$ 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
4

1 回答 1

31

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

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

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

于 2012-09-24T23:27:33.630 回答