多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ class server { public function __construct() { //HTTP_SERVER_URL:监听的http请求地址,如何是监听内部http请求,最好写成内网地址,HTTP_SERVER_PORT:监听端 $serv = new Swoole\Http\Server(HTTP_SERVER_URL, HTTP_SERVER_PORT); $serv->set(array( 'worker_num' => 1, 'task_worker_num' =>2 , 'daemonize' => true,//守护进程,如果服务需要长时间开启,设为true 'heartbeat_check_interval' => 30,//心跳检测时间 'heartbeat_idle_time' => 65,//超过该时间回调close断开TCP 'log_file' => '/data/dt/swoole/error/nydp.log' )); $serv->on('request', array($this, 'request')); //接收http请求 $serv->on('task', array($this, 'task')); //开启一个task进程,必须调用finish进程 $serv->on('finish', array($this, 'finish')); $serv->on('workerStart', array($this, 'worker_start')); //开启一个work进程 $this->_serv = $serv; $this->_tcp = $serv->addListener(TCP_SERVER_URL, TCP_SERVER_PORT, SWOOLE_SOCK_TCP);//监听接受客户端数据的服务器端口 $this->_tcp->set(array( 'worker_num' => 2, 'task_worker_num' => 10, 'daemonize' => false, 'heartbeat_check_interval' => 60, 'heartbeat_idle_time' => 125, 'log_file' => '/data/dt/swoole/error/nydp.log' )); $this->_tcp->on('connect', array($this, 'connect')); $this->_tcp->on('receive', array($this, 'receive')); $this->_tcp->on('close', array($this, 'close')); $serv->start(); } public function connect($serv, $fd) { echo "you success connect client {$fd}\n"; } public function worker_start($serv, $work_id) { //在Worker进程开启时绑定定时器 //只有当worker_id为0时才添加定时器,避免重复添加 if ($work_id == 0) { swoole_timer_tick(30000, array($this, 'timer_send_msg'), $serv); //30秒发送一次数据到设备 } } public function receive($serv, $fd, $from_id, $data) { //接收到客户端传来的数据调用该方法 } public function request($request, $response) { //监听所有的http请求 $response->cookie("User", "Swoole"); $response->header("X-Server", "Swoole"); $response->end("<h1>Hello Swoole!</h1>"); $data['fd'] = isset($request->post['fd']) ? $request->post['fd'] : '';//获取post数据 } public function task($serv, $task_id, $from_id, $data){ //task 任务 } public function finish($serv, $task_id, $data) { //任务完成执行的函数 } public function close($serv, $fd) { //进程关闭执行的函数 } } $server = new server ();