Fastadmin(thinkphp5)安裝整合workerman(gatewayworker)Linux環境

温柔的风發表於2024-10-05

一、安裝

直接上命令

composer require topthink/think-worker 1.0.1 //因為fastadmin的tp版本是5,所以這裡1.0.1
composer update --with-all-dependencies

找到根目錄的composer.json修改制定版本

composer require workerman/gateway-worker

至此安裝完成。

二、啟動

然後在專案中增加相應的檔案啟動wokerman gateway

找到路徑檔案  application/command.php
'app\common\command\Workerman',         // workerman //增加一行

//檔案內容

<?php


namespace app\common\command;

use app\workerman\controller\WorkerEvents;

use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Env;
use Workerman\Worker;

class Workerman extends Command
{
    protected function configure()
    {
        $this->setName('workerman')
            ->addArgument('action', Argument::OPTIONAL, "action  start|stop|restart")
            ->addArgument('type', Argument::OPTIONAL, "d -d")
            ->setDescription('workerman chat');
    }

    protected function execute(Input $input, Output $output)
    {
        global $argv;
        $action = trim($input->getArgument('action'));
        $type   = trim($input->getArgument('type')) ? '-d' : '';


        $argv[0] = 'chat';
        $argv[1] = $action;
        $argv[2] = $type ? '-d' : '';
//        dump($argv);exit;
        $this->start();
    }
    private function start()
    {
        $this->startGateWay();
        $this->startBusinessWorker();
        $this->startRegister();
        Worker::runAll();
    }

    private function startBusinessWorker()
    {
        $worker                  = new BusinessWorker();
        $worker->name            = 'BusinessWorker';
        $worker->count           = 1;
        $worker->registerAddress = '127.0.0.1:1236';
        $worker->eventHandler    = WorkerEvents::class;
    }

    private function startGateWay()
    {
        // gateway 程序
        $gateway = new Gateway("websocket://0.0.0.0:39001");
        // 設定名稱,方便status時檢視
        $gateway->name = 'Gateway';
        // 設定程序數,gateway程序數建議與cpu核數相同
        $gateway->count = 4;
        // 分散式部署時請設定成內網ip(非127.0.0.1)
        $gateway->lanIp = '127.0.0.1';
        // 內部通訊起始埠,假如$gateway->count=4,起始埠為4000
        // 則一般會使用4000 4001 4002 4003 4個埠作為內部通訊埠
        $gateway->startPort = 2300;
        // 心跳間隔
        $gateway->pingInterval = 60;
        // 時間內,客戶端未傳送任何資料,斷開客戶端連線
        $gateway->pingNotResponseLimit = 180;
        // 心跳資料
        $gateway->pingData = '{"type":"ping"}';
        // 服務註冊地址
        $gateway->registerAddress = '127.0.0.1:1238';
        /*
        // 當客戶端連線上來時,設定連線的onWebSocketConnect,即在websocket握手時的回撥
        $gateway->onConnect = function($connection)
        {
            $connection->onWebSocketConnect = function($connection , $http_header)
            {
                // 可以在這裡判斷連線來源是否合法,不合法就關掉連線
                // $_SERVER['HTTP_ORIGIN']標識來自哪個站點的頁面發起的websocket連結
                if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
                {
                    $connection->close();
                }
                // onWebSocketConnect 裡面$_GET $_SERVER是可用的
                // var_dump($_GET, $_SERVER);
            };
        };
        */
    }

    private function startRegister()
    {
        new Register('text://0.0.0.0:1236');
    }
}

建立新檔案路徑地址:application/workerman/WorkerEvents.php

檔案內容:

<?php


namespace app\workerman\controller;

use GatewayWorker\Lib\Gateway;

class WorkerEvents
{
    /**
     * 當客戶端連線時觸發
     * 如果業務不需此回撥可以刪除onConnect
     *
     * @param int $client_id 連線id
     */
    public static function onConnect($client_id)
    {
        $data['type'] = 'get_client_id';
        $data['data'] = ['client_id' => $client_id];
        // 向當前client_id傳送資料
        Gateway::sendToClient($client_id, json_encode($data, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 當客戶端發來訊息時觸發
     * @param int $client_id 連線id
     * @param mixed $message 具體訊息
     */
    public static  function onMessage($client_id, $message)
    {
        dump("收到訊息");
        dump($client_id);
        dump($message);
        //不作處理
    }

    /**
     * 當使用者斷開連線時觸發
     * @param int $client_id 連線id
     */
    public static function onClose($client_id)
    {
        dump("斷開連線".$client_id);
    }
}

啟動服務

php think workerman start

相關文章