PHP 从原始 tcp 套接字读取数据并使用 2 React Connector 通过 WebSocket 客户端发送它们

标签 php sockets websocket ratchet reactphp

这是我想要做的:
(套接字服务器:9006) <- (Websocket 客户端) -> (:8080 Websocket 服务器)
我想从 Socket Server 读取数据,
编辑一些东西,然后发送到 Websocket 服务器。
我必须能够同时读取来自 Websocker 服务器的数据
我设法使用 1 React Connector for Socket Server 和 1 Ratchet Connector for WebSocket Server 连接到两台服务器:

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

$connector->connect('192.168.0.1:9006')->then(function (React\Socket\ConnectionInterface $connection) use ($loop) {
    $connection->pipe(new React\Stream\WritableResourceStream(STDOUT, $loop));
    $connection->write("Hello World!\n");
    
    $connection->on('data', function ($chunk) {
    echo $chunk;
    // How to send $chunk to the Websocket server through second connector, see below?

});

    
});

    $reactConnector = new \React\Socket\Connector($loop, [
        'dns' => '8.8.8.8',
        'timeout' => 10
    ]);
    $connector = new \Ratchet\Client\Connector($loop, $reactConnector);

    $connector('ws://1.2.3.4:8080')
    ->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('Hello World!');
    }, function(\Exception $e) use ($loop) {
        echo "Could not connect: {$e->getMessage()}\n";
        $loop->stop();
    });



$loop->run();
所以我的问题是:如何使用在第二个连接器中的第一个连接器中接收到的数据?
感谢您的帮助!

最佳答案

我找到了解决方案,
如果它可以帮助任何人,我粘贴在那里...:

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

use React\Socket\Connector;
use React\Socket\ConnectionInterface;

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


    $reactConnector = new \React\Socket\Connector($loop, [
        'dns' => '8.8.8.8',
        'timeout' => 10
    ]);
    $wsConnector = new \Ratchet\Client\Connector($loop, $reactConnector);

    $wsConnector('ws://1.2.3.4:8080')
    ->then(function(Ratchet\Client\WebSocket $wsConn) use ($loop) {
        
        
        $socketConnector = new React\Socket\Connector($loop);
        $socketConnector->connect('192.168.0.1:9006')->then(function (ConnectionInterface $socketConn) use ($wsConn) {
                $socketConn->on('data', function ($msg) use ($wsConn){
                 printf("Msg received from Socket Server : %s\n", $msg);
                    // Send it to wsServer
                    $wsConn->send($msg);
                });
                });
        
        
        $wsConn->on('message', function(\Ratchet\RFC6455\Messaging\MessageInterface $msg) use ($wsConn) {
                 printf("Msg received from WebSocket Server : %s\n", $msg);
        });

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

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



$loop->run();

关于PHP 从原始 tcp 套接字读取数据并使用 2 React Connector 通过 WebSocket 客户端发送它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63075726/

相关文章:

php - 在进行下拉选择之前隐藏字段的搜索框

php - 将多维子数组值与其他子数组值相加

python - AES 在 python 中加密 在 php 中解密

java - 为什么这个非阻塞IO调用会失败呢?

c++ - 在 C/C++ 中通过 TCP 套接字发送十六进制

c++ - 在 QWebSockets 客户端检查 SSL 证书有效性的最佳实践

java - javax.servlet.Filter 会拦截 Websocket 消息吗? (Java 服务小程序 API)

javascript - 如何更改每个 html 表格行 Accordion

c - 使用 C 在套接字编程中创建数据包以及发送和接收数据包的正确方法

websocket - 如何在suave中通过websocket实现服务器推送?