thinkphp结合workerman和gateway实现数据同步

Posted 小枫同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了thinkphp结合workerman和gateway实现数据同步相关的知识,希望对你有一定的参考价值。

thinkphp结合workerman和gateway实现数据同步

0x10安装扩展

composer require topthink/think-worker=2.0.*
composer require workerman/gatewayclient

此安装仅针对5.1,其他版本根据官方要求安装。client仅针对3.0.8以上,具体看GitHub

0x20服务端Gateway

0x21config/worker_gateway.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker:gateway 指令有效
// +----------------------------------------------------------------------
return [
    // BusinsessWorker配置
    \'businessWorker\'        => [
        \'name\'         => \'BusinessWorker\',
        \'count\'        => 1,
        \'eventHandler\' => \'\\app\\http\\Gateway\',
    ],

];

0x22 application/http/Gateway.php

<?php

namespace app\\http;

use GatewayWorker\\Lib\\Gateway as Way;

class GateWay

    public static function onWebSocketConnect($id, $data)
    
        dump(\'设备连接,ID:\' . $id);
        Way::bindUid($id, \'1\');
    

    public static function onMessage($client_id, $data)
    
        Way::sendToClient($client_id, \'服务端消息\');
    

0x23 向客户端发送消息

//use GatewayClient\\Gateway;
$post = request()->post();
Gateway::$registerAddress = \'127.0.0.1:1236\';
// 向任意uid的网站页面发送数据
Gateway::sendToUid(\'1\', json_encode($post));
dump($post);

0x30客户端代码

客户端需要用到command,即thinkphp命令行

protected function execute(Input $input, Output $output)
    
		//忽略....
        $a = new Worker();
        $a->onWorkerStart = function ($work) 
            $con = new AsyncTcpConnection(\'ws://192.168.25.149:2346\');//服务端地址
            $con->onConnect = function (AsyncTcpConnection $con) 
                // 连接成功的事件
            ;

            $con->onMessage = function (AsyncTcpConnection $con, $data) 
                dump($data);//来自服务端的消息
            ;

            $con->connect();
        ;
        Worker::runAll();
    

0x40效果图

Thinkphp6+Workerman消息推送

业务需求示例:系统后台监听有新的商品订单,新消息等的通知,推送给所有登陆系统的用户。

逻辑:使用websocket建立连接,并设置定时器发送心跳保持连接不被断开。建立连接后,php端需要设置心跳时间,判断mysql业务表是否有新记录或者订单,如果有就推送消息给所有登陆系统用户,并更新该记录已推送。

### 1.composer先安装workerman组件

composer require topthink/think-worker`

### 2.配置

- worker配置 `config/worker_server.php`

// 扩展自身需要的配置

'protocol'       => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host'           => '0.0.0.0', // 监听地址
'port'           => 2346, // 监听端口
'socket'         => '', // 完整监听地址
'context'        => [], // socket 上下文选项
'worker_class'   => 'app\\work\\Push', // 自定义Workerman服务类名 支持数组定义多个服务

- worker实现 `app/work/Push.php`

### 

<?php
namespace app\\work;
use think\\worker\\Server;
use Workerman\\Lib\\Timer;
use think\\facade\\Db;
class Push extends Server
{
    protected $socket = 'http://0.0.0.0:2346';   //端口自行修改

    protected static $heartbeat_time    =   55;

    public function onWorkerStart($worker){
        //查看是否有新的充值或提现订单,有就推送给所有用户
        Timer::add(3, function()use($worker){

            $time_now   =   time();
            $hasNewDepositOrder   =   Db::name('deposit_order')->where('order_status',0)->where('is_push',0)->order('id desc')->count('id');
            $system_listener    =   Db::name('system_listener')->cache(true)->order('id desc')->select()->toArray();

            if($hasNewDepositOrder){
                $depositOrderInfo   =   Db::name('deposit_order')->where('order_status',0)->where('is_push',0)->order('id desc')->find();
                $data   =   ['creatTime'=>$depositOrderInfo['create_time'],'money'=>$depositOrderInfo['pay_amount'],'type'=>'deposit','system_listener'=>$system_listener];
                foreach($worker->connections as $connection) {
                    if(empty($connection->lastMessageTime)){
                        $connection->lastMessageTime =   $time_now;
                    }

                    if($time_now-$connection->lastMessageTime > self::$heartbeat_time){
                        $connection->close();
                    }

                    $connection->send(json_encode($data));
                }

               Db::name('deposit_order')->where('id',$depositOrderInfo['id'])->save(['is_push'=>1]);
            }else{
                foreach($worker->connections as $connection) {
                    if(empty($connection->lastMessageTime)){
                        $connection->lastMessageTime = $time_now;
                        continue;
                    }

                    if($time_now-$connection->lastMessageTime > self::$heartbeat_time){      //连接超时
                        $connection->close();
                    }
                }
            }
        });
    }

### 运行

`php think worker:server` reload|restart|stop|status

###  websokcet实现 

<script>
let ws = new WebSocket("ws://127.0.0.1:2346")

ws.onopen = function() {  //绑定连接事件
    console.log("连接成功");
    //每30秒发送一次心跳
    setInterval(function(){
        ws.send(JSON.stringify({'type':"peng"}));
        console.log('发送心跳...');

    },30000)

};

ws.onmessage = function(evt) {//绑定收到消息事件
    data = JSON.parse(evt.data)
    console.log(data);
    //这里处理收到的消息, type类型有两种: connectin、deposit如果有deposit要提示有新的订单

};


ws.onclose = function(evt) { //绑定关闭或断开连接事件
  console.log("连接已关闭");
};
</script>

以上是关于thinkphp结合workerman和gateway实现数据同步的主要内容,如果未能解决你的问题,请参考以下文章

请教一个thinkphp5与workerman的问题

ThinkPHP5中使用Workerman框架

thinkphp 怎么获取workerman进程连接数

thinkphp3.2整合workerman 多入口模式(windows)

ThinkPHP和Workerman整合实现Socket通信Demo(转载)

Thinkphp6+Workerman消息推送