From bc50379cbf52629fe446c1e6e8e78e6526cd6905 Mon Sep 17 00:00:00 2001 From: lin <465382251@qq.com> Date: Fri, 13 Aug 2021 10:43:01 +0800 Subject: [PATCH] websocket v5 multiple users subscribe to channels at the same time --- src/Api/WebSocketV5/SocketClient.php | 15 +++++++++++---- src/Api/WebSocketV5/SocketFunction.php | 11 +++++++---- src/Api/WebSocketV5/SocketServer.php | 6 +++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Api/WebSocketV5/SocketClient.php b/src/Api/WebSocketV5/SocketClient.php index 4ee0d0c..ea520df 100644 --- a/src/Api/WebSocketV5/SocketClient.php +++ b/src/Api/WebSocketV5/SocketClient.php @@ -57,7 +57,9 @@ function keysecret(array $keysecret=[]){ * @param array $sub */ public function subscribe(array $sub=[]){ - $this->save('add_sub',$this->resub($sub)); + $add_sub=$this->get('add_sub'); + if(empty($add_sub)) $this->save('add_sub',$this->resub($sub)); + else $this->save('add_sub',array_merge($this->resub($sub),$add_sub)); } /** @@ -183,9 +185,14 @@ protected function getData($global,$callback=null,$sub=[]){ * * */ function reconPrivate(string $key){ - $this->client->debug=[ - 'private'=>[$key=>'close'], - ]; + $debug=$this->client->debug; + if(empty($debug)){ + $this->client->debug=[ + 'private'=>[$key=>$key], + ]; + }else{ + $this->client->debug=['private'=>array_merge($this->client->debug['private'],[$key=>$key])]; + } } function reconPublic(){ diff --git a/src/Api/WebSocketV5/SocketFunction.php b/src/Api/WebSocketV5/SocketFunction.php index b362f4d..517e223 100644 --- a/src/Api/WebSocketV5/SocketFunction.php +++ b/src/Api/WebSocketV5/SocketFunction.php @@ -102,14 +102,17 @@ private function reconnection($global,$type='public',array $keysecret=[]){ $this->keysecret=$keysecret; $temp=[]; - foreach ($all_sub as $v){ - if(is_array($v) && $keysecret['key']==$v[1]['key']){ - $temp[]=$v[0]; + foreach ($all_sub as $k=>$v){ + $t=explode(self::$USER_DELIMITER,$k); + if(count($t)>1 && $t[0]==$keysecret['key']) { + $temp[]=$v; } } } - $global->save('add_sub',$this->resub($temp)); + $add_sub=$global->get('add_sub'); + if(empty($add_sub)) $global->save('add_sub',$this->resub($temp)); + else $global->save('add_sub',array_merge($this->resub($temp),$add_sub)); } } diff --git a/src/Api/WebSocketV5/SocketServer.php b/src/Api/WebSocketV5/SocketServer.php index fd0c8ab..37b42d0 100644 --- a/src/Api/WebSocketV5/SocketServer.php +++ b/src/Api/WebSocketV5/SocketServer.php @@ -272,10 +272,10 @@ private function debug($con,$global){ } }else{ //private - if(isset($debug['private'][$con->tag_keysecret['key']]) && $debug['private'][$con->tag_keysecret['key']]=='close'){ + if(isset($debug['private'][$con->tag_keysecret['key']]) && $debug['private'][$con->tag_keysecret['key']]==$con->tag_keysecret['key']){ $this->log($con->tag_keysecret['key'].' debug '.json_encode($debug)); - $debug['private'][$con->tag_keysecret['key']]='recon'; + unset($debug['private'][$con->tag_keysecret['key']]); $global->save('debug',$debug); //更改登录状态 @@ -431,7 +431,7 @@ private function login($global,$key){ } if($old_client_keysecret[$key]['login']==2) { - $this->log('private doing return '.$key); + $this->log('private login doing return '.$key); return; }