Thinkphp5+FastAdmin配置workerman消息推送(多线程)

2023-12-14 00:57:43

环境:linux系统、fastadmin(tp5内核)、php7.4.3,workerman1.0
1.首先删除vordor文件夹

2.安装扩展,下载workerman

//安装扩展
yum install php-process
//下载workerman
composer require topthink/think-worker

3.在application创建server.php

<?php
define('APP_PATH', __DIR__ . '/../application/');
 //定义监听控制器
define('BIND_MODULE','push/Workerman');
 
 
 
// 加载框架引导文件
 
require __DIR__ . '/../thinkphp/start.php';

4.在application目录创建push目录

application/push/controller/Worker.php

注:如果想监听多个端口,只需要在application/push/controller中再创建一个文件,把端口改一下即可,另外在application创建再创建一个server.php

<?php
namespace app\push\controller; 
use think\worker\Server; 
use Workerman\Lib\Timer;
use think\Db;
 
class Worker extends Server{
       
    protected $socket = 'websocket://127.0.0.1:2346'; //linux服务器端口
    protected static $heartbeat_time=55;
    
      /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
   
        if($data=="ping"&&$data==0){
   
            $connection->send("pong");
            $connection->lastMessageTime=time();
        }else{
   

//            $ordercount = Db::name('ddrive_order')->where('status', '0')->whereTime('appointment_time','>',time())->count();
//            $orderpccount = Db::name('ddrive_pc_order')->where('status', '0')->count();
//
//            $ddnumber = bcadd($ordercount,$orderpccount);
//
//            $connection->lastMessageTime=time();
//            $connection->send($ddnumber);

        }
    }
    
   
    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker){
   
    //查看是否有新的充值或提现订单,有就推送给所有用户
        Timer::add(3, function()use($worker){
   
 
            $time_now=time();
            $hasNewDepositOrder   =   Db::name('worker')->where('is_push',0)->order('id desc')->count('id');
            // $system_listener    =   Db::name('worker')->cache(true)->order('id desc')->select();
 
            if($hasNewDepositOrder){
   
                $depositOrderInfo   =   Db::name('worker')->where('is_push',0)->order('id desc')->find();
                $data   =   ['creatTime'=>date('Y-m-d H:i:s'),'name'=>$depositOrderInfo['name'],'tel'=>$depositOrderInfo['tel']];
                foreach($worker->connections as $connection) {
   
                    if(empty($connection->lastMessageTime)){
   
                        $connection->lastMessageTime =   $time_now;
                    }
 
                    if($time_now-$connection->lastMessageTime > self::$heartbeat_time){
   

文章来源:https://blog.csdn.net/hanzhuhuaa/article/details/134890764
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。