简体   繁体   English

为什么我的应用程序不能在 Heroku 上建立 websocket 连接?

[英]Why won't my app establish websocket connection on Heroku?

I am trying to deploy my nodejs app on heroku.我正在尝试在 heroku 上部署我的 nodejs 应用程序。 I cannot get the websocket connection to establish.我无法建立 websocket 连接。 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.当我尝试打开我的页面“heroku open”时,nodejs 正确地给了我 html 文件。 However, the websocket 'ws' connection never establishes.但是,websocket 'ws' 连接永远不会建立。

My server.js has these configurations:我的 server.js 有这些配置:

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.旁注当我检查“Heroku的日志”,我发现我的应用程序运行的端口是随机5位数字。 ( like 28896, 53365, etc.) It never actually runs on the second half || (如 28896、53365 等)它实际上从未在下半年运行 || 5000. 5000。

But the thing is, in order for my game.html file to establish a websocket connection, it needs to know what port.但问题是,为了让我的 game.html 文件建立 websocket 连接,它需要知道什么端口。 I have tried the following client configurations, none have worked:我尝试了以下客户端配置,但都没有奏效:

1) 1)

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

2) 2)

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

3) 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.我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM