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->addlistener('0.0.0.0', 9504,SWOOLE_SOCK_TCP); // 添加 TCP端口监听 $this->tcp->addlistener('0.0.0.0', 9505,SWOOLE_SOCK_TCP); // 添加 TCP端口监听 $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) { Log::channel('slack')->info($data); //验证数据格式 // $serv->send($fd,$data); // if($this->formatData($data)){ // $swooletcp = new SwooleCommandMeTcpController(); // $swooletcp->swooletcplist($data); // } $swooletcp = new ZehongTcpController(); // $swooletcp->swooletcplist($data); $todata = $swooletcp->swooletcplist($data); Log::channel('slack')->info('返回值' . $todata); $serv->send($fd,$todata); }); //监听连接关闭事件 $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; } } }