首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法与ReactPHP的Pawl和Ratchet进行交叉通信

我无法与ReactPHP的Pawl和Ratchet进行交叉通信
EN

Stack Overflow用户
提问于 2019-04-26 12:37:53
回答 1查看 496关注 0票数 1

我当前正在尝试连接到两个不同的socket服务器。其中一个本质上是IRC连接,另一个是我自己制作的接口服务器。这两个循环需要能够相互通信,但我在将消息从一个连接发送到另一个连接时遇到了困难。

这是我一直在尝试的一种简单的注入消息的方式,评论不是很有信心,因为我真的不确定我错在哪里:

代码语言:javascript
复制
<?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');从一个连接到另一个连接发送消息,但我终生无法弄清楚如何做到这一点。

EN

回答 1

Stack Overflow用户

发布于 2020-04-08 23:59:36

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

代码语言:javascript
复制
<?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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55860892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档