swooleMeTcp.php 3.11 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
<?php

namespace App\Console\Commands;

use App\Http\Controllers\Auth\SwooleCommandMeTcpController;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
class swooleMeTcp extends Command
{
    public $tcp;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'swooleMeTcp {action?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'swooleMeTcp';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        switch ($action) {
            case 'close':
                break;
            default:
                $this->start();//开启tcp服务
                break;
        }

    }

    //开启
    public function start()
    {
冯超鹏's avatar
冯超鹏 committed
57
        $url = config('public.swooletcpurl');
冯超鹏's avatar
冯超鹏 committed
58
        $this->tcp = new \swoole_server('0.0.0.0', 9503);
冯超鹏's avatar
冯超鹏 committed
59 60 61
        $this->tcp->addlistener($url, 9504, SWOOLE_SOCK_TCP); // 添加 TCP端口监听
        $this->tcp->addlistener($url, 9505, SWOOLE_SOCK_TCP); // 添加 TCP端口监听
        $this->tcp->addlistener($url, 9506, SWOOLE_SOCK_TCP); // 添加 TCP端口监听
冯超鹏's avatar
冯超鹏 committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        $this->tcp->set([
            'worker_num' => 2,//设置启动的 Worker 进程数
            'max_request' =>30,//最大任务数
            'max_connection' => 50,
            'daemonize'=>0,//守护进程
            'backlog' => 128,
            'heartbeat_check_interval' => 30,
            'heartbeat_idle_time' => 65,
        ]);
//监听连接进入事件
        $this->tcp->on('Connect', function ($serv, $fd) {
            $data = [
                'stats[得到当前 Server 的活动 TCP 连接数]' => $this->tcp->stats(),
                'getClientInfo[获取连接的信息]' =>$this->tcp->getClientInfo($fd,1,true)
                
            ];//链接信息写入.log
            Log::channel('slack')->info($data);
            $serv->send($fd,'连接成功'.','. 'id=>'. $fd);
        });
//监听数据接收事件
        $this->tcp->on('Receive', function ($serv, $fd, $from_id, $data) {
冯超鹏's avatar
冯超鹏 committed
83 84 85 86 87
            //验证数据格式
            if($this->formatData($data)){
                $swooletcp = new SwooleCommandMeTcpController();
                $swooletcp->swooletcplist($data);
            }
冯超鹏's avatar
冯超鹏 committed
88 89 90 91 92 93 94 95 96 97
        });

//监听连接关闭事件
        $this->tcp->on('Close', function ($serv, $fd) {
            Log::channel('slack')->info('连接已断开'. ',' . 'id=>' . $fd);
        });

//启动服务器
        $this->tcp->start();
    }
冯超鹏's avatar
冯超鹏 committed
98 99 100 101 102 103 104 105 106 107 108
    public function formatData($str){
        $str = preg_replace("/[^a-z,A-Z,0-9,\/,\.,-]/",'', $str);
        $str = rtrim(trim($str),'/');
        $str = str_replace('null', -1, $str);
        preg_match("/^[a-z,A-Z,0-9,\-]{6,32}\/(\d{1,2})\/(\-?)([0-9,\.]{1,8})$/", $str, $out);
        if(count($out) > 0){
            return $str;
        }else{
            return false;
        }
    }
冯超鹏's avatar
冯超鹏 committed
109 110

}