使用带有AppFog的socket.io时出现意外的响应代码:502错误

时间:2012-09-24 16:49:43

标签: node.js socket.io appfog

错误

当用户打开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,如here所述,但它没有帮助。

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

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

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

Socket.IO node.js websocket connection invalid port 8081

socket.io with node.js not working as expected

相关服务器代码

以下是相关的服务器代码:

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

1 个答案:

答案 0 :(得分:30)

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

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

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