hyperf / hyperf

🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.
https://www.hyperf.io
MIT License
6.25k stars 1.21k forks source link

[BUG] 3.0中关于WebSocket上下文问题 #4613

Closed yanghaixi closed 2 years ago

yanghaixi commented 2 years ago

Execute the command and paste the result below.

Command: uname -a && php -v && composer info | grep hyperf && php --ri swoole

Linux YHX-NETBOOK 5.10.60.1-microsoft-standard-WSL2 #1 SMP Wed Aug 25 23:20:18 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
PHP 8.1.3 (cli) (built: Feb 21 2022 14:48:42) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.3, Copyright (c), by Zend Technologies
    with Yasd v0.3.9-alpha, Our Copyright, by codinghuang
hyperf/amqp                         v3.0.0-alpha.1     A amqplib for hyperf.    
hyperf/async-queue                  v3.0.0-alpha.4     A async queue comp...    
hyperf/cache                        v3.0.0-alpha.1     A cache component ...    
hyperf/command                      v3.0.0-alpha.5     Command for hyperf       
hyperf/config                       v3.0.0-alpha.3     An independent com...    
hyperf/config-aliyun-acm            v3.0.0-alpha.1     An aliyun acm adap...    
hyperf/config-center                v3.0.0-alpha.1     The abstraction co...    
hyperf/constants                    v3.0.0-alpha.4     A constants compon...    
hyperf/context                      v3.0.0-alpha.5     A coroutine contex...    
hyperf/contract                     v3.0.0-alpha.1     The contracts of H...    
hyperf/coordinator                  v3.0.0-alpha.1     Hyperf Coordinator       
hyperf/crontab                      v3.0.0-alpha.1     A crontab componen...    
hyperf/database                     v3.0.0-alpha.5     A flexible databas...    
hyperf/db-connection                v3.0.0-alpha.4     A hyperf db connec...    
hyperf/devtool                      v3.0.0-alpha.1     A Devtool for Hyperf.    
hyperf/di                           v3.0.0-alpha.4     A DI for Hyperf.
hyperf/dispatcher                   v3.0.0-alpha.4     A HTTP Server for ...    
hyperf/engine                       v1.2.1
hyperf/event                        v3.0.0-alpha.1     an event manager t...    
hyperf/exception-handler            v3.0.0-alpha.4     Exception handler ...    
hyperf/filesystem                   v3.0.0-alpha.1     flysystem integrat...    
hyperf/flysystem-oss                dev-main ddb6493
hyperf/framework                    v3.0.0-alpha.3     A coroutine framew...    
hyperf/guzzle                       v3.0.0-alpha.1     Swoole coroutine h...    
hyperf/http-message                 v3.0.0-alpha.1     microservice frame...    
hyperf/http-server                  v3.0.0-alpha.5     A HTTP Server for ...    
hyperf/ide-helper                   v3.0.0-alpha.1     IDE help files for...    
hyperf/logger                       v3.0.0-alpha.4     A logger component...    
hyperf/macroable                    v3.0.0-alpha.1     Hyperf Macroable p...    
hyperf/memory                       v3.0.0-alpha.1     An independent com...    
hyperf/model-cache                  v3.0.0-alpha.5     A model cache comp...    
hyperf/model-listener               v3.0.0-alpha.1     A model listener f...    
hyperf/paginator                    v3.0.0-alpha.4     A paginator compon...    
hyperf/pool                         v3.0.0-alpha.4     An independent uni...    
hyperf/process                      v3.0.0-alpha.1     A process componen...    
hyperf/redis                        v3.0.0-alpha.4     A redis component ...    
hyperf/server                       v3.0.0-alpha.4     A base server libr...    
hyperf/snowflake                    v3.0.0-alpha.1     A snowflake library      
hyperf/testing                      v3.0.0-alpha.4     Testing for hyperf       
hyperf/translation                  v3.0.0-alpha.4     An independent tra...    
hyperf/utils                        v3.0.0-alpha.5     A tools package th...    
hyperf/validation                   v3.0.0-alpha.4     hyperf validation        
hyperf/watcher                      v3.0.0-alpha.1     Hot reload watcher...    
hyperf/websocket-server             v3.0.0-alpha.5     A websocket server...    

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.8.7
Built => Mar  8 2022 16:40:10
coroutine => enabled with boost asm context
epoll => enabled
json => enabled
curl-native => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

Description:

在 onOpen 中设置的上下文内容,在onMessage无法接收

结束返回 NULL

Steps To Reproduce:

<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Hyperf\WebSocketServer\Context;

class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
    public function onMessage($server, $frame): void
    {
        var_dump(Context::get('username'));
    }

    public function onClose($server, int $fd, int $reactorId): void
    {
        var_dump('closed');
    }

    public function onOpen($server, $request): void
    {
        Context::set('username', 'test');
        var_dump('open');
    }
}
limingxinleo commented 2 years ago

不要忘了点个 star