tourze/workerman-connection-context 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tourze/workerman-connection-context

最新稳定版本:0.0.3

Composer 安装命令:

composer require tourze/workerman-connection-context

包简介

Workerman连接上下文管理包,提供线程安全的连接级别上下文存储

README 文档

README

PHP Version License Build Status Code Coverage

English | 中文

A context management solution for Workerman connections. This package provides a way to store and manage connection-specific context objects using PHP's WeakMap feature for automatic memory management.

Features

  • Connection-specific context storage
  • Multiple context types per connection
  • Automatic cleanup using WeakMap
  • Type-safe context retrieval
  • Singleton pattern for global access

Installation

composer require tourze/workerman-connection-context

Quick Start

Basic Usage

use Tourze\Workerman\ConnectionContext\ContextContainer;

// Define your context class
class UserContext
{
    public function __construct(
        private string $userId,
        private array $permissions
    ) {}

    public function getUserId(): string
    {
        return $this->userId;
    }

    public function getPermissions(): array
    {
        return $this->permissions;
    }
}

// In your Workerman callback
$worker->onConnect = function ($connection) {
    $context = new UserContext('user123', ['read', 'write']);
    ContextContainer::getInstance()->setContext($connection, $context);
};

$worker->onMessage = function ($connection, $data) {
    $context = ContextContainer::getInstance()->getContext($connection, UserContext::class);
    if ($context) {
        echo "User: " . $context->getUserId() . "\n";
        echo "Permissions: " . implode(', ', $context->getPermissions()) . "\n";
    }
};

Multiple Context Types

class SessionContext
{
    public function __construct(private string $sessionId) {}
    
    public function getSessionId(): string
    {
        return $this->sessionId;
    }
}

class AuthContext
{
    public function __construct(private bool $isAuthenticated) {}
    
    public function isAuthenticated(): bool
    {
        return $this->isAuthenticated;
    }
}

// Store multiple context types for the same connection
$sessionContext = new SessionContext('sess_123');
$authContext = new AuthContext(true);

ContextContainer::getInstance()->setContext($connection, $sessionContext);
ContextContainer::getInstance()->setContext($connection, $authContext);

// Retrieve specific context types
$session = ContextContainer::getInstance()->getContext($connection, SessionContext::class);
$auth = ContextContainer::getInstance()->getContext($connection, AuthContext::class);

Context Cleanup

// Clear specific context type
ContextContainer::getInstance()->clearContext($connection, UserContext::class);

// Clear all contexts for a connection
ContextContainer::getInstance()->clearAllContexts($connection);

API Reference

ContextContainer

getInstance(): ContextContainer

Get the singleton instance of the context container.

setContext(ConnectionInterface $connection, object $context): void

Store a context object for the specified connection.

getContext(ConnectionInterface $connection, string $className): ?object

Retrieve a context object of the specified type for the connection.

clearContext(ConnectionInterface $connection, string $className): void

Remove a specific context type for the connection.

clearAllContexts(ConnectionInterface $connection): void

Remove all context objects for the connection.

Memory Management

This package uses PHP's WeakMap to automatically clean up context data when connection objects are garbage collected. This prevents memory leaks in long-running Workerman applications.

License

MIT License

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固