workerman如何实现聊天系统
时间:2019-12-12 14:03
安装 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如何实现聊天系统的详细内容,更多请关注gxlsystem.com其它相关文章!