安裝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
先來個伺服器廣播訊息的範例,每10秒鐘定時廣播一則訊息
'onWorkerStart' => function ($worker) { \Workerman\Lib\Timer::add(10, function()use($worker){ // 遍历当前进程所有的客户端连接,发送自定义消息 foreach($worker->connections as $connection){ $send['name'] = '系统信息'; $send['content'] = '这是一个定时任务信息'; $send['time'] = time(); $connection->send(json_encode($send)); } });}
但是在onMessage 時,我們取得不到$worker 對象,所以無法廣播訊息。
'onMessage' => function ($connection, $data) { $origin = json_decode($data,true); $send['name'] = '广播数据'; $send['content'] = $origin['content']; $message = json_encode($send); foreach($worker->connections as $connection) { $connection->send($message); }}
修改框架內部的程式碼:/vendor/topthink/think-worker/src/command/Server.php,主要是把onMessage 方法自己加進去
use() 就是把外部變數傳遞到函數內部使用,或是使用global $worker
$worker = new Worker($socket, $context);$worker->onMessage = function ($connection, $data)use($worker) { $origin = json_decode($data,true); $send['name'] = '广播数据'; $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
其實你早都已經看出,$worker->connections 取得到的是目前所有使用者的連接,connections 即為其中一個連結。
記錄websocket連線時間:
$worker->onConnect = function ($connection) { $connection->login_time = time();};
取得websocket連線時間:
$worker->onMessage = function ($connection, $data)use($worker) { $login_time = $connection->login_time;};
由此可以看出,我們可以把資料綁定到$connection 連線的一個屬性,例如:
$connection->uid = $uid;
當JavaScript端在連接websocket伺服器成功後,也就是把自己的uid 立刻發送服務端綁定:
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)單播傳送訊息,即自訂傳送
$worker->onMessage = function ($connection, $data)use($worker) { $origin = json_decode($data,true); $sendTo = $origin['sendto']; // 需要发送的对方的uid $content = $origin['content']; // 需要发送到对方的内容 foreach($worker->connections as $connection) { if( $connection->uid == $sendTo){ $connection->send($content); } }};
到此,已經完成基於Workerman 的自訂物件傳送訊息。
由於這個php檔案存放在composer中,只需要把該檔案複製出來,放到application/command,修改命名空間,即可儲存到自己的專案中
(5)儲存聊天記錄
使用Redis 做快取對伺服器影響較小,且基本上不影響回應時間
1、把聊天記錄儲存到Redis 中,使用清單儲存
$message = json_decode($data,true); // $data为接收到的数据 $redis_instance = Cache::handler(); // TP5代码获取Cache实例 $redis_instance->lPush('message',json_encode($message,JSON_UNESCAPED_UNICODE));
2、某些情況下,當使用者第一次(或刷新)聊天頁面時,顯示最近10筆記錄
$redis_instance = Cache::handler(); // TP5代码获取Cache实例 $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 最近聊天記錄時處理:
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教學
#以上是workerman如何實現聊天系統的詳細內容。更多資訊請關注PHP中文網其他相關文章!