2013-11-03 44 views
3

我想在heroku上部署我的nodejs應用程序。我無法獲得websocket連接建立。我已經閱讀了所有我能找到的東西,嘗試了每一個例子,而且他們都沒有爲我工作。爲什麼我的應用程序不能在Heroku上建立websocket連接?

我在試圖打開我的頁面「heroku open」時,nodejs正確地給了我html文件。然而,websocket'ws'連接永遠不會建立。

我server.js具有這些配置:

var pport = process.env.PORT || 5000; 
server.listen(pport, function(err) { 
    if(!err) { console.log("Listening on port " + pport); } 
}); 

旁註 當我檢查「Heroku的日誌」,我發現我的應用程序運行的端口是隨機5位數字。 (如28896,53365等)它實際上從未在第二部分||上運行5000.

但事情是,爲了讓我的game.html文件建立一個websocket連接,它需要知道什麼端口。 我曾嘗試以下客戶端配置,還沒有工作:

1)

var host = location.origin.replace(/^http/, 'ws'); 
this.connection = new WebSocket(host); 

2)

var host = location.origin.replace(/^http/, 'ws'); 
host = host + ":5000"; 
this.connection = new WebSocket(host); 

3)

this.connection = new WebSocket('ws://infinite-earth-7708.herokuapp.com/'); 

我也做他們的網站說的話,並試圖使用部署我的應用程序後,執行以下操作:

heroku config:add NODE_ENV=production 

請告知

回答

5

好吧,我想通了。這裏是你應該知道的:

  • 我沒有改變我原來發布的服務器配置。

我的客戶端配置是這樣的:

var host = location.origin.replace(/^http/, 'ws'); 
this.connection = new WebSocket(host); 

但這裏是踢球。 在終端我用下面的命令:

heroku labs:enable websockets 

瞧,它的工作原理!我希望這可以幫助別人。

+0

有關實現websockets的更多信息,請查看** Heroku websockets文檔:** https://devcenter.heroku.com/articles/node-websockets – AmpT

相關問題