swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.48k stars 3.16k forks source link

Websocket服務,調用Task做訊息發送,出現session does not exits #5319

Closed lda124520 closed 6 months ago

lda124520 commented 6 months ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error. 我要做聊天室,透過Worker接收client request後,調用Task做邏輯處理以及push訊息回Client

假設當前有一位使用者連上websocket,其fd = 100 當client 送出聊天訊息 request server 後, 直接在OnMessage做邏輯處理和push訊息給fd = 100的 client是成功的 但如在OnMessage調用Task做push訊息 會出現Session#100 does not exits 這在以前使用php5/6 + swoole2.x版是可以的 近期升級後發現不行

  1. What did you expect to see? 期望可以調用Task做訊息推送

  2. What did you see instead? 無法透過Task做訊息推送

  3. What version of Swoole are you using (show your php --ri swoole)? Swoole => enabled Author => Swoole Team team@swoole.com Version => 5.1.2 Built => Jan 24 2024 10:14:10 coroutine => enabled with boost asm context trace_log => enabled epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled sockets => enabled openssl => OpenSSL 1.0.2k-fips 26 Jan 2017 http2 => enabled json => enabled pcre => enabled c-ares => 1.10.0 zlib => 1.2.7 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled mysqlnd => enabled async_redis => enabled coroutine_odbc => enabled coroutine_sqlite => enabled

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

  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ? Linux dev 3.10.0-1062.12.1.el7.x86_64 #1 SMP Tue Feb 4 23:02:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

PHP 8.2.11 (cli) (built: Sep 26 2023 11:11:58) (NTS gcc x86_64) Copyright (c) The PHP Group Zend Engine v4.2.11, Copyright (c) Zend Technologies

Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

以下是server端 php碼 未命名

以下是執行結果 未命名

NathanFreeman commented 6 months ago
$this->ws = new Swoole\WebSocket\Server("0.0.0.0", 9061, SWOOLE_PROCESS);

尝试一下实例化的时候加上SWOOLE_PROCESS

lda124520 commented 6 months ago
$this->ws = new Swoole\WebSocket\Server("0.0.0.0", 9061, SWOOLE_PROCESS);

尝试一下实例化的时候加上SWOOLE_PROCESS

可以了,刚查了文档发现swoole5后预设是 SWOOLE_BASE 非常感谢您

另外想请问 使用swoole websocket来开发多人聊天室(可群聊,可私聊) 将所有onMessage 收到的讯息都一律调用task做逻辑处理 所有push讯息,也只让task处理 这样的设计在swoole适合吗 或是讯息量大的时候,会不会容易造成server崩溃 网上比较少看到其他人这样设计 怕是我对swoole的观念不正确

NathanFreeman commented 6 months ago

不合理,Task的设计目的是为了执行阻塞代码,不阻塞主进程

lda124520 commented 6 months ago

不合理,Task的设计目的是为了执行阻塞代码,不阻塞主进程

了解

想请问您的建议 在处理群聊讯息的需求下 使用swoole websocket server (实利化使用SWOOLE_PROCESS)

直接透过OnMessage处理逻辑和push广播讯息 并拉高work_num的数量维持效能 如有遇到需要耗时处理的业务,才额外调用task

这样合适吗,是否符合swoole设計的worker和task用意呢

NathanFreeman commented 6 months ago

可以呀