這篇文章主要介紹workerman實(shí)現(xiàn)聊天系統(tǒng)的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

安裝 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) {
\Workerman\Lib\Timer::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 對象,所以無法廣播消息。
'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 對象了
$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ā)送的對方的uid
$content = $origin['content']; // 需要發(fā)送到對方的內(nèi)容
foreach($worker->connections as $connection)
{
if( $connection->uid == $sendTo){
$connection->send($content);
}
}};到此,已經(jīng)完成基于 Workerman 的自定義對象發(fā)送消息。
由于該php文件存放于composer中,只需要把該文件復(fù)制出來,放到application/command,修改命名空間,即可保存到自己的項(xiàng)目中
(5)存儲聊天記錄
使用 Redis 做緩存對服務(wù)器影響較小,且基本不影響響應(yīng)時(shí)間
1、把聊天記錄存儲到 Redis 中,使用列表存儲
$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('<li>'+your.content+'</li>');
}
};以上是“workerman實(shí)現(xiàn)聊天系統(tǒng)的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站題目:workerman實(shí)現(xiàn)聊天系統(tǒng)的方法-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://chinadenli.net/article32/cdoepc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、軟件開發(fā)、營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容