2012-09-24 700 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

相關問題