为什么我的应用程序不能在Heroku上建立websocket连接? [英] Why won't my app establish websocket connection on Heroku?
问题描述
我试图在heroku上部署我的nodejs应用程序。我无法获得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连接,它需要知道哪个端口。
我尝试过以下客户端配置,但都没有成功:
$ p
var host = location.origin.replace(/ ^ http /,'ws');
this.connection =新的WebSocket(主机);
2)
var host = location.origin.replace(/ ^ http /,'ws');
host = host +:5000;
this.connection =新的WebSocket(主机);
3)
this.connection = new WebSocket('ws://infinite-earth-7708.herokuapp.com/');
我也完成了他们网站的说明,并在部署我的应用后尝试使用以下内容:
heroku config:add NODE_ENV =生产
请指教
我想通了。以下是您应该知道的内容:
我的客户端配置如下所示:
var host = location。 origin.replace(/ ^ http /,'ws');
this.connection =新的WebSocket(主机);
但是这里是踢球者。
在终端上我使用了以下命令:
$ p $ heroku实验:启用websockets
瞧,它的工作原理!我希望这可以帮助别人。
I am trying to deploy my nodejs app on heroku. I cannot get the websocket connection to establish. I have read everything I could find, tried every example and none of them seem to work for me.
I when I try to open my page "heroku open", nodejs is correctly giving me the html file. However, the websocket 'ws' connection never establishes.
My server.js has these configurations:
var pport = process.env.PORT || 5000;
server.listen(pport, function(err) {
if(!err) { console.log("Listening on port " + pport); }
});
Side Note When I check "heroku logs", I find that the port my app is running on is a random 5 digit number. ( like 28896, 53365, etc.) It never actually runs on the second half || 5000.
But the thing is, in order for my game.html file to establish a websocket connection, it needs to know what port. I have tried the following client configurations, none have worked:
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/');
I have also done what their website said, and attempted to use the following after deploying my app:
heroku config:add NODE_ENV=production
Please advise
Well I figured it out. Here is what you should know:
- I did not change my server configurations from my original post.
My client configurations looked like this:
var host = location.origin.replace(/^http/, 'ws');
this.connection = new WebSocket(host);
But here is the kicker. On the terminal I used the following command:
heroku labs:enable websockets
And voila, it works! I hope this helps someone.
这篇关于为什么我的应用程序不能在Heroku上建立websocket连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!