D
D
Denis Nicholas2019-09-04 20:36:53
PHP
Denis Nicholas, 2019-09-04 20:36:53

How to set the Heroku port?

How to set a specific port for a worker in php?
The worker starts the swoole server:

$http = new Server('0.0.0.0', '8443', SWOOLE_BASE);

$http->on('start', function (Server $server) use ($container) {
    $logger = $container->get(Logger::class);
    $config = $container->get('config');
    $logger->info('Swoole http server is started at ' . $config['app']['url'] . ', port :8443');
});

$http->on('request', function (Request $request, Response $response) use ($container) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$http->start();

Whatever port you specify - does not let through the browser.
443 and 80 cannot be specified - Permission denied.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Nicholas, 2019-09-04
@dennikolas

I decided on my own, I was very close to the solution.
It was just necessary to specify in the procfile:
web: php web/server.php
I indicated:
worker: php web/server.php
Plus, in the code you need to specify the port taken from the environment, the PORT variable:

$port = getenv('PORT');
$http = new Server('0.0.0.0', $port, SWOOLE_BASE);

$http->on('start', function (Server $server) use ($container) {
    $logger = $container->get(Logger::class);
    $config = $container->get('config');
    $logger->info('Swoole http server is started at ' . $config['app']['url'] . ', port :' . $port);
});

$http->on('request', function (Request $request, Response $response) use ($container) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$http->start();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question