PHP read data from raw tcp socket and send them through WebSocket Client using 2 React Connector

Kévin Lesage

Here what i'm trying to do :

(Socket Server:9006) <- (Websocket Client) -> (:8080 Websocket Server)

I want to read data from Socket Server, edit some stuff and then send to Websocket Server. I must be able to read data coming from Websocker Server in the same time

I managed to connect to both Servers using 1 React Connector for Socket Server, and 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();

So my question is : How can i use data received in the first connector, in the second connector?

Thank for your help!

Kévin Lesage

I found the solution, i paste there if it can help anybody... :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Send a 2D Matrix from Client to Server in Java using TCP connection (Socket Programming)

How to send data from client socket to server socket using Python?

How to send raw bytes through a Dart TCP socket

Send Raw Data from PHP Server API to Swift client API

Send data packet over TCP using PHP socket

In TCP socket program,client send some data, but server need read multiple times. why?

Read from any file in binary mode, append it to string, and send through TCP socket

How to read data from scale through ethernet using a client program

How to send data through a client websocket in a aiohttp request handler

Not able to send data from socket to specific client

WebSocket send and receive data asynchronously (using NuGet websocket-client)

How to differentiate client TCP socket and client websocket using socket.io library?

How to send message to client through websocket using Spring

PHP receiving data from TCP socket repeatedly

How to send raw data using socket.io

Send message through Socket Outputstream from Swift Client to Java Server

Using data received from TCP socket [ python ]

How to read and send data to client from server?

UNIX socket read happening even though client does not send data

Send and receive data using Spring Integration TCP IP socket

Read from a raw socket connected to a network interface using golang

Sending data from the Python TCP Socket(as server) to the JS client

Spring TCP Socket client not receiving data from Mainframe

Send List of Strings from Python to C# Using TCP/socket

Send a string with white space through socket tcp

How to send multiple data streams from single client to a tcp server

Cannot read from TCP socket

Send packet from server to a specific client using TCP c#

Linux tool to send raw data to a TCP server