zehongTcp.php 2.87 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 57 58
<?php

namespace App\Console\Commands;

use App\Http\Controllers\Auth\ZehongTcpController;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;

class zehongTcp extends Command
{
    public $tcp;

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

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

    /**
     * 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()
    {
        $url = config('public.swooletcpurl');
Administrator's avatar
Administrator committed
59
        $this->tcp = new \swoole_server('0.0.0.0', 9506, SWOOLE_PROCESS);
冯超鹏's avatar
冯超鹏 committed
60 61 62 63 64 65 66 67 68 69 70 71 72
        $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(),
冯超鹏's avatar
冯超鹏 committed
73
                'getClientInfo[获取连swooletcplist接的信息]' => $this->tcp->getClientInfo($fd, 1, true)
冯超鹏's avatar
冯超鹏 committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

            ];//链接信息写入.log
            Log::channel('slack')->info($data);
            $serv->send($fd, '连接成功' . ',' . 'id=>' . $fd);
        });
//监听数据接收事件
        $this->tcp->on('Receive', function ($serv, $fd, $from_id, $data) {
            //验证数据格式
            $swooletcp = new ZehongTcpController();
//            $swooletcp->swooletcplist($data);
            $serv->send($fd,$swooletcp->swooletcplist($data));
        });

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

//启动服务器
        $this->tcp->start();
    }

    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;
        }
    }

Administrator's avatar
Administrator committed
109
}