pipe

Description:

void Connection::pipe(TcpConnection $target_connection)

Parameters

This method redirects the data stream from the current connection to the target connection. It has built-in flow control. This method is very useful for TCP proxying.

Example TCP Proxy

<?php
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('tcp://0.0.0.0:8483');
$worker->count = 12;

// After TCP connection is established
$worker->onConnect = function(TcpConnection $connection)
{
    // Establish an asynchronous connection to local port 80
    $connection_to_80 = new AsyncTcpConnection('tcp://127.0.0.1:80');
    // Set the current client connection data to be redirected to the connection on port 80
    $connection->pipe($connection_to_80);
    // Set the data returned from the port 80 connection to be redirected to the client connection
    $connection_to_80->pipe($connection);
    // Execute the asynchronous connection
    $connection_to_80->connect();
};

// Run the worker
Worker::runAll();