hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - PHP框架 - Workerman - 正文 请牢记本站网址www.sosophp.cn

workerman怎样完成谈天体系【workerman】,workerman

2019-12-13Workerman搜搜PHP网51°c
A+ A-

装置 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怎样完成谈天体系的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
workerman怎样完成谈天体系【workerman】,workerman

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签: