搜索
首页php框架Workermanworkerman如何实现聊天系统

workerman如何实现聊天系统

Dec 12, 2019 pm 02:03 PM
workerman

workerman如何实现聊天系统

安装 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(&#39;<li>&#39;+your.content+&#39;</li>&#39;);
    }
};

推荐:workerman教程

以上是workerman如何实现聊天系统的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。