Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using http-client with Workerman HTTP server #11

Closed
cayolblake opened this issue Aug 14, 2022 · 3 comments
Closed

Using http-client with Workerman HTTP server #11

cayolblake opened this issue Aug 14, 2022 · 3 comments

Comments

@cayolblake
Copy link

Hi @walkor

How to make the HTTP response of Workerman HTTP server waits from the HTTP client to finish first? 🤔

<?php

use Workerman\Worker;
require_once __DIR__ . '/vendor/autoload.php';
$http_worker = new Worker('http://127.0.0.1:2345');
$http_client = new Workerman\Http\Client();
$http_worker->count = 1;

$http_worker->onMessage = function ($connection, $request) use ($http_client) {
    $http_client->request('https://ifconfig.me/ip', [
        'method' => 'GET',
        'version' => '1.1',
        'success' => function ($response) use ($connection) {
            echo $response->getBody() . "\n";
        },
        'error' => function ($exception) {
            echo $exception;
        }
    ]);
    $connection->send(""); // need to use $response->getBody() here
};

Worker::runAll();
@cayolblake
Copy link
Author

@walkor anyluck here ? :)

@walkor
Copy link
Owner

walkor commented Aug 17, 2022

$http_worker->onMessage = function ($connection, $request) use ($http_client) {
    $http_client->request('https://ifconfig.me/ip', [
        'method' => 'GET',
        'version' => '1.1',
        'success' => function ($response) use ($connection) {
            $connection->send((string) $response->getBody());
        },
        'error' => function ($exception) use ($connection) {
            $connection->send((string) $exception);
        }
    ]);
};

@cayolblake
Copy link
Author

Thanks @walkor 👍

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants