定制 tourze/workerman-chain-protocol 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/workerman-chain-protocol

最新稳定版本:0.1.0

Composer 安装命令:

composer require tourze/workerman-chain-protocol

包简介

Workerman 链式协议处理器,支持多层协议栈处理数据编解码

README 文档

README

[Latest Version] (https://packagist.org/packages/tourze/workerman-chain-protocol) [PHP Version] (composer.json) [Build Status] (https://github.com/tourze/php-monorepo/actions) [Coverage Status] (https://codecov.io/gh/tourze/php-monorepo) [License] (LICENSE)

English | 中文

Chain protocol processing for Workerman. This package allows you to create a chain of protocol parsers to decode and encode data in Workerman connections.

Features

  • Chain-style protocol processing for Workerman
  • Protocol-agnostic, works with any protocol that implements Workerman's ProtocolInterface
  • Support for both TCP and UDP connections
  • Event-driven architecture with Symfony Event Dispatcher
  • Comprehensive logging
  • Performance monitoring using PHP Timer

Installation

composer require tourze/workerman-chain-protocol

Quick Start

<?php

use Tourze\Workerman\ChainProtocol\ChainProtocol;
use Tourze\Workerman\ChainProtocol\Container;
use Workerman\Worker;

// Setup your custom protocol classes that implement ProtocolInterface
Container::$decodeProtocols = [
    MyProtocol1::class, 
    MyProtocol2::class,
    // Add more protocols as needed
];

Container::$encodeProtocols = [
    MyProtocol2::class,
    MyProtocol1::class,
    // The order matters for encoding (reverse of decoding)
];

// Setup logger if needed
Container::$logger = new MyLogger();

// Setup event dispatcher if needed
Container::$eventDispatcher = new MyEventDispatcher();

// Create a Workerman server with ChainProtocol
$worker = new Worker('tcp://0.0.0.0:8080');
$worker->protocol = ChainProtocol::class;

$worker->onMessage = function($connection, $data) {
    // $data is already decoded through all protocols in the chain
    // Process your business logic here

    // Send a response, which will be encoded through all protocols in the chain
    $connection->send('Your response');
};

Worker::runAll();

Dependencies

This package requires:

  • PHP 8.1 or higher
  • workerman/workerman ^5.1
  • symfony/event-dispatcher ^7.3
  • symfony/event-dispatcher-contracts ^3
  • tourze/workerman-connection-context 0.0.*
  • psr/log ^1|^2|^3
  • phpunit/php-timer ^7.0

Advanced Usage

Custom Protocol Implementation

<?php

use Workerman\Protocols\ProtocolInterface;
use Workerman\Connection\ConnectionInterface;

class MyCustomProtocol implements ProtocolInterface
{
    public static function input(string $buffer, ConnectionInterface $connection): int
    {
        // Return the length of a complete packet, or 0 if more data is needed
        return strlen($buffer) >= 4 ? unpack('N', substr($buffer, 0, 4))[1] + 4 : 0;
    }

    public static function decode(string $buffer, ConnectionInterface $connection): string
    {
        // Remove the length header and return the actual data
        return substr($buffer, 4);
    }

    public static function encode(mixed $data, ConnectionInterface $connection): string
    {
        // Add a length header to the data
        return pack('N', strlen($data)) . $data;
    }
}

Event Handling

<?php

use Symfony\Component\EventDispatcher\EventDispatcher;
use Tourze\Workerman\ChainProtocol\Event\ChainDataDecodedEvent;
use Tourze\Workerman\ChainProtocol\Container;

$eventDispatcher = new EventDispatcher();

$eventDispatcher->addListener(ChainDataDecodedEvent::class, function(ChainDataDecodedEvent $event) {
    // Log or process decoded data
    error_log('Data decoded: ' . $event->getBuffer());
});

Container::$eventDispatcher = $eventDispatcher;

Custom Logger Configuration

<?php

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Tourze\Workerman\ChainProtocol\Container;

$logger = new Logger('chain-protocol');
$logger->pushHandler(new StreamHandler('path/to/logfile.log', Logger::DEBUG));

Container::$logger = $logger;

Contributing

Please see CONTRIBUTING.md for details.

License

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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固