2012-09-24 692 views
17

误差

当用户打开http://sync.aws.af.cm/,谷歌浏览器JavaScript控制台示出了(在几秒钟内)时:意外响应代码:502错误使用socket.io与AppFog

Unexpected response code: 502 error 

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

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

我已经试过

我试图从/socket.io/socket.io.js更改为node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js在我的客户script src=<path_to_socket_io>路径描述here,但它并没有帮助。

我也尝试在Heroku上代替AppFog服务应用程序,但是在那种情况下,即使我指定了socket.io版本0.9.6,我仍然遇到描述为here的问题。

我也看了下面的问题,但到目前为止的意见似乎并没有帮助我。

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'); 
    ... 
    }; 
... 

Server日志

$ 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

解决:如在此所描述: http://stackoverflow.com/questions/11350279/socket-io-does-not-work-on-firefox-chrome 添加此代码固定的问题: IO .configure('development',function()io。set('transports',['xhr-polling']); }); 但我不明白为什么。现在,我的问题似乎是一个现有问题的重复。我如何关闭此问题并将其链接到其他问题? –

+0

这不是同一个问题。从另一个问题的Node.js日志中可以看到,应用程序在回退之前正在通过websockets进行握手,而您的应用程序从头开始声明无效的连接。 –

回答

29

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

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

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

+0

最后我发现发生了什么事!非常感谢你的回答。 – LordZardeck

+1

对于那些使用CF的人,这种配置解决了Cloudfoundry + Heroku上的相同问题。谢谢Wes! – Gary

+0

感谢您的cloudflare位,它帮助我 – GiveMeAllYourCats

相关问题