SWOOLE開發(fā)實時聊天系統(tǒng)(十)用戶斷開聊天

最后我們再處理一下用戶斷開連接的時候的處理工作。
首先,我們需要在websocket斷開的時候,去調(diào)用MessageController中的close函數(shù),傳遞當前用戶的fd.

$this->server->on('close', function ($ser, $fd) {
            echo "server: close a fd  : fd{$fd}\n";
            $this->MessageController->closeFd($fd);
        });

在MessageController中,去調(diào)用處理用戶登錄注冊所有邏輯的LoginService處理具體的業(yè)務邏輯,刪除用戶和直播的關系。其中包括刪除user_id 對應的fd,group對應的fd等的內(nèi)容。

    //關閉連接
    public function closeFd($fd)
    {
        $loginService = new LoginService($this->server);
        $loginService->close($fd);
    }

然后我們?nèi)oginService中完成邏輯處理:

 public function close($fd) {
        try{
            $result=$this->removeFromRedis($fd);
            if(!$result){
                throw new \Exception('刪除用戶失敗,請重試!');
            }
            //在這里也加上notify,如果有需要通知的內(nèi)容,可以在前面直接寫,不用再更改此處的業(yè)務邏輯
            $this->notify();
        }catch (\Exception $e){
            $this->result['status'] = 1;
            $this->result['msg'] = $e->getMessage();
            echo json_encode($this->result,JSON_UNESCAPED_UNICODE);
        }
        return $this->result;
    }

具體的實現(xiàn)方法如下:

   private function removeFromRedis($fd){
        $user_id = pool::redis()->hGet(self::user_bind_redis_key, $fd);
        pool::redis()->hDel(self::user_bind_redis_key, $fd);
        //刪除當前用戶所對應的分組
        $rk = str_replace('fd', $fd, self::redis_key_user_group);
        $group = pool::redis()->get($rk);
        pool::redis()->del($rk);
        //刪除分組中對應的用戶
        if ($group) {
            $group = explode("_", $group);
            //從分組對應的用戶中,刪除當前用戶
            $rk = str_replace(['first_topic', 'second_topic'], [$group[0], $group[1]], self::redis_key_group_user);
            $userlist = pool::redis()->get($rk);
            $userlist = explode(',', $userlist);
            $key = array_search($user_id, $userlist);
            array_splice($userlist, $key, 1);
            if (!$userlist) {
                pool::redis()->del($rk);
            } else {
                pool::redis()->set($rk, implode(",", $userlist));
            }
        }
        //刪除fd綁定的用戶。
        pool::redis()->hDel(self::fd_bind_user_redis_key, $user_id);
        return true;
    }

在這段邏輯中主要是通過fd來查找之前我們生成的所有緩存內(nèi)容,將所有與當前fd相關的內(nèi)容全部刪除。也就使得用戶與我們的業(yè)務脫離了關系。

當兩個用戶都連接之后,我們可以看到,redis中一共有如下幾個Key:

1) "ws_topic_1_1"
2) "ws_user_2"
3) "ws_fd_bind_user_redis_key"
4) "ws_user_bind_fd_redis_key"
5) "ws_user_4"

我們打開其中的一個去查看,可以發(fā)現(xiàn),兩個用戶ID正好對應兩個fd

127.0.0.1:6379> hkeys ws_fd_bind_user_redis_key
1) "10086"
2) "10087"
127.0.0.1:6379> hget ws_fd_bind_user_redis_key 10086
"2"
127.0.0.1:6379> hget ws_fd_bind_user_redis_key 10087
"4"

然后我們試著關閉10087的用戶
再去查看redis中的內(nèi)容

127.0.0.1:6379> keys *
1) "ws_topic_1_1"
2) "ws_user_2"
3) "ws_fd_bind_user_redis_key"
4) "ws_user_bind_fd_redis_key"

127.0.0.1:6379> hkeys ws_fd_bind_user_redis_key
1) "10086"

127.0.0.1:6379> hkeys ws_user_bind_fd_redis_key
1) "2"
127.0.0.1:6379> 

所有關于用戶10087的內(nèi)容都已經(jīng)被刪除了。
至此,整個業(yè)務流程全部完成。

項目已經(jīng)放在了github上,歡迎大家批評指正。https://github.com/gsbhx/swoole_chat

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容