tourze/workerman-relay-worker

0.0.3 2025-04-04 17:53 UTC

This package is auto-updated.

Last update: 2025-04-04 17:54:05 UTC


README

English | 中文

Latest Version Total Downloads

A connection relay worker for Workerman that enables traffic forwarding between different connections (TCP/UDP).

Features

  • Support for TCP and UDP connection relaying
  • Works with the Workerman PHP framework
  • Automatic pipeline creation between source and target connections
  • Connection buffering while setting up the relay
  • Seamless integration with existing Workerman applications

Installation

composer require tourze/workerman-relay-worker

Quick Start

<?php

use Tourze\Workerman\ConnectionPipe\Model\Address;
use Tourze\Workerman\RelayWorker\RelayWorker;
use Workerman\Connection\ConnectionInterface;

// Create a relay worker listening on a TCP port
$worker = new RelayWorker('tcp://0.0.0.0:8080');

// Set onConnect callback to establish the relay target
$worker->onConnect = function(ConnectionInterface $connection) {
    // Define target address for the connection
    $address = new Address('127.0.0.1', 9000, 'tcp');
    RelayWorker::setTarget($connection, $address);
};

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

Usage

The RelayWorker class extends Workerman's Worker class and provides a connection relay mechanism. You can use it to:

  • Create TCP-to-TCP, TCP-to-UDP, UDP-to-TCP, or UDP-to-UDP relays
  • Buffer incoming data until a target is set
  • Process connections asynchronously

For each connection, you need to set a target address using RelayWorker::setTarget(). Once set, any data received on that connection will be forwarded to the target address.

Dependencies

  • PHP 8.1+
  • workerman/workerman: ^5.1
  • tourze/workerman-connection-pipe: 0.0.*

License

The MIT License (MIT). Please see License File for more information.