examples/tcp_proxy_example.php
protected function requestConnect(WebSocketTransportInterface $user, $message)
    {
        $address = $message->address;
        $this->logger->notice(sprintf("User %s requests connection to %s", $user->getId(), $address));

        try {
            $dnsResolverFactory = new React\Dns\Resolver\Factory();
            $dns = $dnsResolverFactory->createCached('8.8.8.8', $this->loop);
            $stream = new \React\SocketClient\Connector($this->loop, $dns);

            list($host, $port) = explode(":", $address);

            $logger = $this->logger;
            $that = $this;

            $stream->create($host, $port)->then(function (\React\Stream\Stream $stream) use($user, $logger, $message, $address, $that){
                $id = uniqid("stream-$address-");
                $that->addStream($user, $id, $stream);

                // Notify the user when the connection has been made
                $user->sendString(json_encode(array(
                    'connection' => $id,
                    'event' => 'connected',
                    'tag' => property_exists($message, 'tag') ? $message->tag : null
                )));

                // Forward data back to the user
                $stream->on("data", function ($data) use ($stream, $id, $user, $logger){
                    $logger->notice("Forwarding ".strlen($data). " bytes from stream $id to {$user->getId()}");
                    $message = array(
                        'connection' => $id,
                        'event' => 'data',
                        'data' => $data
                    );

                    $user->sendString(json_encode($message));
                });

                // When the stream closes, notify the user
                $stream->on("close", function() use($user, $id, $logger, $address){
                    $logger->notice(sprintf("Connection %s of user %s to %s has been closed", $id, $user->getId(), $address));

                    $message =
                        array(
                            'connection' => $id,
                            'event' => 'close'
                        );

                    $user->sendString(json_encode($message));
                });
            });
        } catch (Exception $e) {
            $user->sendString(json_encode(array(
                'event' => 'error',
                'tag' => property_exists($message, 'tag') ? $message->tag : null,
                'message' => $e->getMessage()
            )));
        }
    }
protected function requestConnect(WebSocketTransportInterface $user, $message)
    {
        $address = $message->address;
        $this->logger->notice(sprintf("User %s requests connection to %s", $user->getId(), $address));

        try {
            $dnsResolverFactory = new React\Dns\Resolver\Factory();
            $dns = $dnsResolverFactory->createCached('8.8.8.8', $this->loop);
            $stream = new \React\SocketClient\Connector($this->loop, $dns);

            list($host, $port) = explode(":", $address);

            $logger = $this->logger;
            $that = $this;

            $stream->create($host, $port)->then(function (\React\Stream\Stream $stream) use($user, $logger, $message, $address, $that){
                $id = uniqid("stream-$address-");
                $that->addStream($user, $id, $stream);

                // Notify the user when the connection has been made
                $user->sendString(json_encode(array(
                    'connection' => $id,
                    'event' => 'connected',
                    'tag' => property_exists($message, 'tag') ? $message->tag : null
                )));

                // Forward data back to the user
                $stream->on("data", function ($data) use ($stream, $id, $user, $logger){
                    $logger->notice("Forwarding ".strlen($data). " bytes from stream $id to {$user->getId()}");
                    $message = array(
                        'connection' => $id,
                        'event' => 'data',
                        'data' => $data
                    );

                    $user->sendString(json_encode($message));
                });

                // When the stream closes, notify the user
                $stream->on("close", function() use($user, $id, $logger, $address){
                    $logger->notice(sprintf("Connection %s of user %s to %s has been closed", $id, $user->getId(), $address));

                    $message =
                        array(
                            'connection' => $id,
                            'event' => 'close'
                        );

                    $user->sendString(json_encode($message));
                });
            });
        } catch (Exception $e) {
            $user->sendString(json_encode(array(
                'event' => 'error',
                'tag' => property_exists($message, 'tag') ? $message->tag : null,
                'message' => $e->getMessage()
            )));
        }
    }
protected function requestConnect ( WebSocketTransportInterface $ user , $ message ) { $ address = $ message -> address ; $ this -> logger -> notice ( sprintf ( "User %s requests connection to %s" , $ user -> getId ( ) , $ address ) ) ; try { $ dnsResolverFactory = new React \ Dns \ Resolver \ Factory ( ) ; $ dns = $ dnsResolverFactory -> createCached ( '8.8.8.8' , $ this -> loop ) ; $ stream = new \ React \ SocketClient \ Connector ( $ this -> loop , $ dns ) ; list ( $ host , $ port ) = explode ( ":" , $ address ) ; $ logger = $ this -> logger ; $ that = $ this ; $ stream -> create ( $ host , $ port ) -> then ( function ( \ React \ Stream \ Stream $ stream ) use ( $ user , $ logger , $ message , $ address , $ that ) { $ id = uniqid ( "stream-$address-" ) ; $ that -> addStream ( $ user , $ id , $ stream ) ; // Notify the user when the connection has been made $ user -> sendString ( json_encode ( array ( 'connection' => $ id , 'event' => 'connected' , 'tag' => property_exists ( $ message , 'tag' ) ? $ message -> tag : null ) ) ) ; // Forward data back to the user $ stream -> on ( "data" , function ( $ data ) use ( $ stream , $ id , $ user , $ logger ) { $ logger -> notice ( "Forwarding " . strlen ( $ data ) . " bytes from stream $id to {$user->getId()}" ) ; $ message = array ( 'connection' => $ id , 'event' => 'data' , 'data' => $ data ) ; $ user -> sendString ( json_encode ( $ message ) ) ; } ) ; // When the stream closes, notify the user $ stream -> on ( "close" , function ( ) use ( $ user , $ id , $ logger , $ address ) { $ logger -> notice ( sprintf ( "Connection %s of user %s to %s has been closed" , $ id , $ user -> getId ( ) , $ address ) ) ; $ message = array ( 'connection' => $ id , 'event' => 'close' ) ; $ user -> sendString ( json_encode ( $ message ) ) ; } ) ; } ) ; } catch ( Exception $ e ) { $ user -> sendString ( json_encode ( array ( 'event' => 'error' , 'tag' => property_exists ( $ message , 'tag' ) ? $ message -> tag : null , 'message' => $ e -> getMessage ( ) ) ) ) ; } }
Handler called when a CONNECT message is sent by a client

A React SocketClient will be created, Google DNS is used to resolve host names. When the connection is made
several event listeners are attached. When data is received on the stream, it is forwarded to the client requesting
the proxied TCP connection

Other events forwarded are connect and close

@param WebSocketTransportInterface $user
@param $message
Handler called when a CONNECT message is sent by a client