Я не могу наладить связь с Паулом и Храповиком ReactPHP - PullRequest
0 голосов
/ 26 апреля 2019

В настоящее время я пытаюсь подключиться к двум разным серверам сокетов. Один из них по сути является IRC-соединением, другой - интерфейсным сервером, который я сделал сам. Эти две петли должны иметь возможность общаться друг с другом, но у меня возникают трудности с отправкой сообщения из одного соединения в другое.

Вот то, что я пробовал как упрощенный способ введения сообщения, комментарии не очень уверенные, потому что я, честно говоря, не уверен, в чем я ошибаюсь:

<?php

    require __DIR__ . '/vendor/autoload.php';

    // EventLoop the way I understand it is designed to split up your loops and
    // run them one after another so you can kind of multithread, even though
    // it's just one step of the loop at a time.
    $loop = \React\EventLoop\Factory::create();

    // Verbose defintion of connectors mainly trying to just gain extra control
    // and so I could see how each thing was defined.
    $reactConnector = new \React\Socket\Connector($loop, [
        'dns' => '8.8.8.8',
        'timeout' => 10
    ]);

    $connector = new \Ratchet\Client\Connector($loop, $reactConnector);

    // Connect as a client to the development Socket server. This is all just
    // from the Pawl github essentially.
    // 
    // The connection is successful every time.
    $ws = $connector('ws://0.0.0.0:5069', [], ['Origin' => 'http://localhost']);
    ->then(function(Ratchet\Client\WebSocket $conn) {

        // Simple echo on message received for the test.
        $conn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($conn) {
            echo "Received: {$msg}\n";
        });

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

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

    // Instead of including a second socket connector I decided to use a simple
    // timer and try to get it to use the client connection above to send
    // messages to the socket server.
    // 
    // The problem is here, I can't get the socket server to send a message
    // from outside of the ->then();
    $loop->addPeriodicTimer(1, function () use ($ws) {

        $ws->then(function (Ratchet\Client\WebSocket $conn) {
            $conn->send('Figured out?');
        });

    });

    $loop->run();

Мне бы очень хотелось иметь возможность отправлять сообщения с одного соединения на другое через какое-то $ws->send('message');, но я никак не могу понять, как это сделать.

...