Heroku- Web进程在启动后90秒内无法绑定到$ PORT。 TooTallNate Websockets

我正在使用一个tootallnate websockets服务器来监听来自网站的连接。

如何在heroku上建立与服务器的连接?

当我的网站尝试连接时

wss://Heroku-Name-39329.herokuapp.com/ 

要么

 wss://Heroku-Name-39329.herokuapp.com:5000/ 

我的heroku记录输出。

 at=error code=H10 desc="App crashed" method=GET path="/" host=wss://Heroku-Name-39329.herokuapp.com request_id=4afca002-2078-439c-85dc-ad6ef7db50d2 fwd="207.244.77.23" dyno= connect= service= status=503 bytes= 

然后(Still Heroku Logs)

 Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 90 seconds of launch Stopping process with SIGKILL Process exited with status 137 State changed from starting to crashed State changed from crashed to starting Starting process with command `java $JAVA_OPTS -cp target/classes/:target/dependency/* v1.a1.server 

我的javascript日志

 WebSocket connection to 'wss://Heroku-Name-39329.herokuapp.com/:5000/' failed: Establishing a tunnel via proxy server failed. 

我在服务器中设置的应用程序是。

 String host = ""; int port = 5000; 

我的Procfile

 web: java $JAVA_OPTS -cp target/classes/:target/dependency/* v1.a1.server 

试试这个:

 String host = "0.0.0.0"; int port = System.getenv("PORT"); 

在Heroku上,您必须绑定到0.0.0.0并使用分配给您的应用程序的端口,该$PORT包含在$PORT环境变量中。

从客户端,您不需要指定端口,因此只应使用wss://Heroku-Name-39329.herokuapp.com/ (而不是5000)。

Heroku希望你使用某个端口。

将其添加到Procfile以获取该端口:

 -Dserver.port=$PORT 

所以你的看起来像这样:Procfile

 web: java $JAVA_OPTS -Dserver.port=$PORT -cp target/classes/:target/dependency 

Heroku的端口号有自己的env变量。 在server.js文件中使用它。

 // included process.env PORT variable for herokus specific port needed to deploy const PORT = process.env.PORT || 8000; 

因此,如果不使用PORT,它将使用8000. heroku将使用PORT变量。 完整文件示例:

 const express = require('express'); const app = express(); const router = express.Router(); app.use(express.static('public')); router.get('*',function(req, res) { res.sendFile(path.join(__dirname, './public/index')); }); included process.env PORT variable for herokus specific port needed to deploy const PORT = process.env.PORT || 8000; app.listen(PORT, function(err) { if (err) { console.log(err); return; } console.log('listening on port 8000'); });