Websocket在本地工作,但不在Heroku上运行 - PHP应用程序

时间:2016-03-19 16:31:22

标签: php heroku websocket

我很长一段时间没有使用过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时,它不起作用。 当我尝试telnet到端口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

3 个答案:

答案 0 :(得分:0)

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

Procfile:

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";
}

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

答案 1 :(得分:0)

基于Floss的答案...这就是为我提供由 bloatless / php-websocket 驱动的websocket起作用的原因。类似的事情也可能适用于棘轮游戏。

Procfile:

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://tools.ietf.org/html/rfc6455#section-11.3

答案 2 :(得分:-2)

您检查过防火墙和端口吗?

相关问题