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

http and pawl within same loop #75

Closed
erleeman opened this issue Oct 21, 2016 · 2 comments
Closed

http and pawl within same loop #75

erleeman opened this issue Oct 21, 2016 · 2 comments
Labels

Comments

@erleeman
Copy link

erleeman commented Oct 21, 2016

Hey folks,

ist this possible? I want to expose a HTTP POST request and forward to Ratchet Websocket Server.
I would like in detail NOT open and close a socket connection with every POST. So the idea with one same loop.

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$connector = new Ratchet\Client\Connector($loop);


$http = new React\Http\Server($socket);
$http->on('request', function ($request, $response, $connector, $loop) {

    if ($request->getMethod() == "POST") {

        $connector('ws://127.0.0.1:1338')
            ->then(function (Ratchet\Client\WebSocket $conn) {
                $conn->on('message', function (\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
                    echo "Received: {$msg}\n";
                    $conn->close();
                });

                $conn->on('close', function ($code = null, $reason = null) {
                    echo "Connection closed ({$code} - {$reason})\n";
                });

                $conn->send(json_encode([
                    'id' => rand(1, 5),
                    'type' => 'Typoe',
                    'message' => 'Lorem lipsum',
                    'source' => 'Test',
                    'good' => rand(0, 1),
                    'bad' => rand(0, 1)
                ]));

            }, function (\Exception $e) use ($loop) {
                echo "Could not connect: {$e->getMessage()}\n";
                $loop->stop();
            });


        $response->writeHead(200, array('Content-Type' => 'text/plain'));
        $response->end("Hello World!\n" . print_r($request->getMethod()));

    } else {
        $response->writeHead(501, array('Content-Type' => 'text/plain'));
        $response->end("Not Implemented!\n" . print_r($request->getMethod()));
    }
});


$socket->listen(5000);
$loop->run();

Error:
Fatal error: Uncaught Error: Function name must be a string in http.php:22
Line 22 is:
$connector('ws://127.0.0.1:1338')

PS: Thanks for pushing PHP to the next level :-)

@clue clue added the question label Oct 21, 2016
@clue
Copy link
Member

clue commented Oct 21, 2016

http and pawl within same loop

I haven't used this personally, but this should absolutely be possible 👍

One of the core issues with your code is this subtle error here, which should likely fix most of the problems you're having:

-$http->on('request', function ($request, $response, $connector, $loop) {
+$http->on('request', function ($request, $response) use ($connector, $loop) {

I would like in detail NOT open and close a socket connection with every POST.

That's how you've coded this currently :-) You'll likely want to move to the connector call out of the request handler so that you only create a connection once instead of opening a new one for each incoming request.

I hope this helps! 👍

@clue clue closed this as completed Oct 21, 2016
@erleeman
Copy link
Author

erleeman commented Oct 22, 2016

Great.
Changed the code a bit and now its up and running. Thanks!

$loop = React\EventLoop\Factory::create();

$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);

$connector = new Ratchet\Client\Connector($loop);
$connector('ws://127.0.0.1:1338')
    ->then(function (Ratchet\Client\WebSocket $conn) use ($http) {
        $conn->on('message', function (\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
            echo "Empfangen von anderem Client oder dem Server: {$msg}\n";
            //$conn->close();
        });

        $conn->on('close', function ($code = null, $reason = null) {
            echo "Connection closed ({$code} - {$reason})\n";
        });


        $http->on('request', function ($request, $response) use ($conn, $http) {

            print_r($request->getQuery());

            if ($request->getMethod() == "POST") {
                $param = $request->getQuery();
                $conn->send($param['data']);

                $response->writeHead(200, array('Content-Type' => 'text/plain'));
                $response->end("Daten gesendet...\n" . print_r($request->getMethod()));

            } else {
                $response->writeHead(501, array('Content-Type' => 'text/plain'));
                $response->end("Not Implemented!\n" . print_r($request->getMethod()));
            }
        });



    }, function (\Exception $e) use ($loop) {
        echo "Could not connect: {$e->getMessage()}\n";
        $loop->stop();
    });


$socket->listen(5000);
$loop->run();

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

No branches or pull requests

2 participants