【问题标题】:Websocket work locally but not on Heroku - PHP applicationWebsocket 在本地工作,但不在 Heroku - PHP 应用程序上工作
【发布时间】:2021-05-19 06:03:00
【问题描述】:

我很久没有使用 Heroku,所以我有点生疏了。 我创建了一个运行 Ratchet IOServer 的小型 PHP 应用程序。它侦听端口 5000。如果我运行 heroku local 并与 telnet localhost 5000 连接,一切似乎都正常。我尝试了几种让 PHP 进程运行并接受连接的方法。

我的 Procfile 看起来像这样;

web: php bin/console bot:start

运行heroku local

Downloading forego-0.16.1 to /Users/roje/.heroku... done
forego | starting web.1 on port 8080
web.1  | It works!
web.1  | New connection! (97)
web.1  | Connection 97 sending message "sdfdfs"

当我将服务器部署到 Heroku 时,它不起作用。 当我尝试远程登录到端口5000 上的盒子时,我得到了

telnet myapplication.herokuapp.com 5000
Trying 46.137.127.234...
telnet: connect to address 46.137.127.234: Connection refused
telnet: Unable to connect to remote host

日志显示了更多信息。

2016-03-19T16:08:27.258081+00:00 heroku[web.1]: State changed from crashed to starting
2016-03-19T16:08:29.754688+00:00 heroku[web.1]: Starting process with command `php bin/console bot:start`
2016-03-19T16:08:31.659074+00:00 app[web.1]: It works!
2016-03-19T16:09:29.999903+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-03-19T16:09:29.999903+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-03-19T16:09:30.638659+00:00 heroku[web.1]: Process exited with status 137
2016-03-19T16:09:30.660710+00:00 heroku[web.1]: State changed from starting to crashed
2016-03-19T16:18:51.610894+00:00 heroku[web.1]: State changed from crashed to starting
2016-03-19T16:18:55.208396+00:00 heroku[web.1]: Starting process with command `php bin/console bot:start`
2016-03-19T16:18:56.648708+00:00 app[web.1]: It works!
2016-03-19T16:19:55.569256+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-03-19T16:19:55.569256+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-03-19T16:19:56.252235+00:00 heroku[web.1]: Process exited with status 137
2016-03-19T16:19:56.264205+00:00 heroku[web.1]: State changed from starting to crashed

有什么建议吗?

编辑#1

我刚刚阅读了关于 Heroku 分配动态端口 Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch) 的部分

我尝试在该端口上进行监听,但仍然无法通过该端口进行连接。远程登录。虽然我确实得到了一些有趣的日志。也许你们中的一些人正在尝试 IP 地址?

2016-03-19T16:38:35.577130+00:00 app[web.1]: It works!
2016-03-19T16:38:35.827466+00:00 app[web.1]: New connection! (97)
2016-03-19T16:38:35.827531+00:00 app[web.1]: Connection 97 has disconnected
2016-03-19T16:38:35.832097+00:00 app[web.1]: New connection! (107)
2016-03-19T16:38:35.832160+00:00 app[web.1]: Connection 107 has disconnected
2016-03-19T16:38:49.891455+00:00 app[web.1]: New connection! (108)
2016-03-19T16:38:49.891506+00:00 app[web.1]: Connection 108 has disconnected
2016-03-19T16:40:54.694321+00:00 app[web.1]: New connection! (109)
2016-03-19T16:40:54.694368+00:00 app[web.1]: Connection 109 has disconnected

但我仍然遇到同样的错误。

Trying 176.34.255.126...
telnet: connect to address 176.34.255.126: Connection refused
telnet: Unable to connect to remote host

【问题讨论】:

  • 我不相信 Heroku 允许您从外部连接到 80 或 443 以外的任何东西。请参阅stackoverflow.com/questions/8107748/… - 您的端口 5000 仅在实例内,但 Heroku 的路由器不会将其路由到外部。
  • 我尝试在 Heroku 在环境变量中动态定义的端口上启动 IOServer。在这种情况下,端口被分配为40220。仍然没有运气。

标签: php heroku websocket


【解决方案1】:

我能够使用 nginx 作为 Web 套接字前的代理来管理它。

过程文件:

web: private/bin/start_socket vendor/bin/heroku-php-nginx -C nginx_app.conf

start_socket:

php private/bin/start_socket.php &
exec "$@"

start_socket.php:

<?php
require_once(dirname(dirname(__DIR__)) . '/vendor/autoload.php');
require_once(__DIR__ . '/socket/socket.php');

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Socket()
        )
    ),
    8085
);
$server->run();

nginx.conf:

location /wss {
    proxy_pass http://localhost:8085;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

现在您可以像这样访问套接字:ws://myapp.com/wss?mydata=test。这并没有解决每个 dyno 将运行它自己独特的 WebSocket 服务器的事实,但这是一个完全独立的问题。

【讨论】:

  • 这种方法确实有效,谢谢!这个答案应该被接受。我正在寻找一种将 WS 设置为与我的主要后端进程一起工作的方法,但我认为这是不可能的 :( 对此有什么想法吗?
【解决方案2】:

根据 Floss 的回答...这对我来说是为由 bloatless/php-websocket 提供支持的 websocket 服务的。类似的东西也可能适用于 Ratchet。

过程文件:

web: bash start vendor/bin/heroku-php-nginx -C nginx.conf

开始:

php server.php &
exec "$@"

nginx.conf:

location /app {
    proxy_pass http://localhost:1112;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Sec-WebSocket-Protocol $http_sec_websocket_protocol;
    proxy_set_header Sec-WebSocket-Extensions $http_sec_websocket_extensions;
    proxy_set_header Sec-WebSocket-Key $http_sec_websocket_key;
    proxy_set_header Sec-WebSocket-Version $http_sec_websocket_version;
}

server.php 将是为您的 websockets 应用程序提供服务的任何 PHP 文件。

:1112 将是您设置 websockets 应用程序运行的任何端口。

/app 将是您需要为 Bloatless 设置的应用程序名称。

注意:您可能还需要设置其他 websocket 标头,具体取决于您使用的客户端。可以在此处找到 websocket 标头的完整列表: https://www.rfc-editor.org/rfc/rfc6455#section-11.3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    相关资源
    最近更新 更多