[英]Heroku Websockets with Java ECONNREFUSED (Connection refused)
I am writing a small game server using Java.我正在使用 Java 编写一个小型游戏服务器。
I use TooTallNate-Java-Websockets library to create my websocket server.我使用TooTallNate-Java-Websockets库来创建我的 websocket 服务器。 Everything works when I run my server on localhost , I can connect to it from everywhere.However when I submit my app to Heroku , every time I try to establish a socket connection I get an error ECONNREFUSED (Connection refused) .当我在 localhost 上运行我的服务器时,一切正常,我可以从任何地方连接到它。但是,当我将我的应用程序提交给 Heroku 时,每次尝试建立套接字连接时,我都会收到错误ECONNREFUSED (Connection denied) 。
Worth to mention , that when I am running my app with foreman which is supposed to emulate heroku environment , everything works as it should.值得一提的是,当我与应该模拟 heroku 环境的工头一起运行我的应用程序时,一切正常。
As a port for my websocket server I tried to use 8080 and many others in range between 5000 to 8000 .作为我的 websocket 服务器的端口,我尝试使用8080以及5000到8000之间的许多其他端口。
I can only guess what is going on there on heroku , as logs contain only basic info of http requests.我只能猜测 heroku 上发生了什么,因为日志只包含 http 请求的基本信息。
Please help , I am close to give up :(请帮忙,我快要放弃了:(
EDIT编辑
Here is what I have in my Proc file:这是我的 Proc 文件中的内容:
web: ./build/install/my-app/bin/my-app
UPDATE Created a simple abstraction app to showcase the problem: (Tested in foreman of course , and it works in local environment)更新创建了一个简单的抽象应用程序来展示问题:(当然在领班中测试过,它在本地环境中工作)
WebSocket connection to 'wss://test-websocket-yan.herokuapp.com:39773/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
控制台错误消息: WebSocket connection to 'wss://test-websocket-yan.herokuapp.com:39773/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I am answering my own question to share how I've managed to connect to Java websocket server on Heroku.我正在回答我自己的问题,以分享我如何设法连接到 Heroku 上的 Java websocket 服务器。
You must use the port that Heroku sets as the $PORT
env var:您必须使用 Heroku 设置为$PORT
env var 的$PORT
:
On Heroku, apps are completely self-contained and do not rely on runtime injection of a webserver into the execution environment to create a web-facing service.在 Heroku 上,应用程序是完全独立的,不依赖于将网络服务器运行时注入到执行环境中来创建面向网络的服务。 Each web process simply binds to a port, and listens for requests coming in on that port.每个 Web 进程只绑定到一个端口,并侦听来自该端口的请求。 The port to bind to is assigned by Heroku as the PORT environment variable.要绑定的端口由 Heroku 分配为 PORT 环境变量。
For more see: https://devcenter.heroku.com/articles/runtime-principles#web-servers更多信息请参见: https : //devcenter.heroku.com/articles/runtime-principles#web-servers
You might just need to have REDIS_URL
set.您可能只需要设置REDIS_URL
。 It was in REDISCLOUD_URL
in my case.就我而言,它在REDISCLOUD_URL
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.