<?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'); $this->tcp = new \swoole_server('0.0.0.0', 9503, SWOOLE_PROCESS); $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) { //验证数据格式 $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; } } }