workerman如何實(shí)現(xiàn)聊天系統(tǒng)

workerman如何實(shí)現(xiàn)聊天系統(tǒng)

安裝 thinkphp5.1

composer?create-project?topthink/think=5.1.x-dev?tp5andworkerman

安裝 think-worker

composer?require?topthink/think-worker=2.0.*

直接安裝 Workerman

composer?require?workerman/workerman

(2)我們先看 think-worker 的代碼

config/worker_server.php

先來個(gè)服務(wù)器廣播消息的示例,每10秒鐘定時(shí)廣播一條消息

'onWorkerStart'??=>?function?($worker)?{ ????WorkermanLibTimer::add(10,?function()use($worker){ ????????//?遍歷當(dāng)前進(jìn)程所有的客戶端連接,發(fā)送自定義消息 ????????foreach($worker->connections?as?$connection){ ????????????$send['name']?=?'系統(tǒng)信息'; ????????????$send['content']?=?'這是一個(gè)定時(shí)任務(wù)信息'; ????????????$send['time']?=?time(); ????????????$connection->send(json_encode($send)); ????????} ????});}

但是在 onMessage 時(shí),我們獲取不到 $worker 對(duì)象,所以無法廣播消息。

'onMessage'??????=>?function?($connection,?$data)?{ ????$origin?=?json_decode($data,true); ????$send['name']?=?'廣播數(shù)據(jù)'; ????$send['content']?=?$origin['content']; ????$message?=?json_encode($send);  ????foreach($worker->connections?as?$connection) ????{ ????????$connection->send($message); ????}}

修改框架內(nèi)部的代碼:/vendor/topthink/think-worker/src/command/Server.php,主要是把 onMessage 方法自己加進(jìn)去

use() 就是把外部變量傳遞到函數(shù)內(nèi)部使用,或者使用global $worker

$worker?=?new?Worker($socket,?$context);$worker->onMessage?=?function?($connection,?$data)use($worker)?{ ????$origin?=?json_decode($data,true); ????$send['name']?=?'廣播數(shù)據(jù)'; ????$send['content']?=?$origin['content']; ????$send['uid']?=?$connection->uid; ????$message?=?json_encode($send); ????foreach($worker->connections?as?$connection) ????{ ????????$connection->send($message); ????}};

這樣,我們就能夠獲取到 $worker 對(duì)象了

$worker->onMessage = function ($connection, $data)use($worker) { … }

(3)$connection 綁定 uid

其實(shí)你早都已經(jīng)看出,$worker->connections 獲取到的是當(dāng)前所有用戶的連接,connections 即為其中一個(gè)鏈接。

記錄websocket連接時(shí)間:

$worker->onConnect?=?function?($connection)?{ ????$connection->login_time?=?time();};

獲取websocket連接時(shí)間:

$worker->onMessage?=?function?($connection,?$data)use($worker)?{ ????$login_time?=?$connection->login_time;};

由此可以看出,我們可以把數(shù)據(jù)綁定到 $connection 連接的一個(gè)屬性,例如:

$connection->uid?=?$uid;

當(dāng)JavaScript端在連接websocket服務(wù)器成功后,即把自己的 uid 立馬發(fā)送服務(wù)端綁定:

var?uid?=?600;ws.onopen?=?function()?{ ????ws.send(JSON.stringify({bind:'yes',uid:uid}));};
$worker->onMessage?=?function?($connection,?$data)use($worker)?{ ????$origin?=?json_decode($data,true); ????if(array_key_exists('bind',$origin)){ ????????$connection->uid?=?$origin['uid']; ????}};

(4)單播發(fā)送消息,即自定義發(fā)送

$worker->onMessage?=?function?($connection,?$data)use($worker)?{ ????$origin?=?json_decode($data,true); ????$sendTo?=?$origin['sendto'];?//?需要發(fā)送的對(duì)方的uid ????$content?=?$origin['content'];?//?需要發(fā)送到對(duì)方的內(nèi)容 ????foreach($worker->connections?as?$connection) ????{ ????????if(?$connection->uid?==?$sendTo){ ????????????$connection->send($content); ????????} ????}};

到此,已經(jīng)完成基于 Workerman 的自定義對(duì)象發(fā)送消息。

由于該php文件存放于composer中,只需要把該文件復(fù)制出來,放到application/command,修改命名空間,即可保存到自己的項(xiàng)目中

(5)存儲(chǔ)聊天記錄

使用 Redis 做緩存對(duì)服務(wù)器影響較小,且基本不影響響應(yīng)時(shí)間

1、把聊天記錄存儲(chǔ)到 Redis 中,使用列表存儲(chǔ)

$message?=?json_decode($data,true);?//?$data為接收到的數(shù)據(jù) $redis_instance?=?Cache::handler();?//?TP5代碼獲取Cache實(shí)例 $redis_instance->lPush('message',json_encode($message,JSON_UNESCAPED_UNICODE));

2、某些情況下,當(dāng)用戶第一次(或刷新)聊天頁面時(shí),顯示最近10條記錄

$redis_instance?=?Cache::handler();?//?TP5代碼獲取Cache實(shí)例 $worker->onConnect?=?function?($connection)use($redis_instance)?{ ????$length?=?$redis_instance->lLen('message'); ????if($length?>?0){ ????????$send['recently']?=?array_reverse($redis_instance->lRange('message',?0,?10)); ????????$send['state']?=?200; ????????$message?=?json_encode($send,JSON_UNESCAPED_UNICODE); ????????$connection->send($message); ????}else{ ????????$send['state']?=?204; ????????$send['recently']?=?[]; ????????$send['msg']?=?'暫無聊天記錄'; ????????$message?=?json_encode($send,JSON_UNESCAPED_UNICODE); ????????$connection->send($message); ????} };

javascript獲取到 recently 最近聊天記錄時(shí)處理:

ws.onmessage?=?function(e)?{ ????var?your?=?JSON.parse(e.data); ????if(your.recently){ ????????//?初次打開頁面,渲染最近10條聊天記錄 ????????$.each(your.recently,function(index,item){ ????????????item?=?JSON.parse(item); ????????????//?TODO:遍歷渲染頁面 ????????}); ????}else{ ????????//?處理其他消息 ????????msglist.append('
  • ‘+your.content+’
  • ‘); ????} };

    推薦:workerman教程

    以上就是

    ? 版權(quán)聲明
    THE END
    喜歡就支持一下吧
    點(diǎn)贊7 分享