我无法与ReactPHP的Pawl和Ratchet进行交叉交流

时间:2019-04-26 04:37:53

标签: php websocket ratchet reactphp

我当前正在尝试连接到两个不同的套接字服务器。其中一个实质上是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');从一个连接向另一个连接发送消息,但是我一生都不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

啊,终于可以回答一个问题了!!我昨天的大部分时间都是通过自己的棘轮/棘爪客户端工作的,该客户端必须具有addPeriodicTimer循环才能定期发送内容。它花了一些时间,但我通过在连接器块的-> then(function(Ratchet \ Client \ WebSocket $ conn)部分之后和$ conn-之前放置了$ loop-> addPeriodicTimer()调用来使之工作。 > on('message'...)调用。另外,对于$ loop-> addPeriodicTimer调用,请确保添加通过连接的 use 子句...并确保添加 use 子句,将$ loop传递给连接器。

<?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) use ( $loop ) {


    $loop->addPeriodicTimer(1, function () use ( $conn ) {

            $conn->send('Figured out?');

    });

        // 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();
    });


    $loop->run();