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 59
| <?php class Server { private $serv; public function __construct() { $this->serv = new swoole_server("0.0.0.0", 9501); $this->serv->set(array( 'worker_num' => 1, 'daemonize' => 1, 'max_request' => 10000, 'dispatch_mode' => 2, 'task_worker_num' => 8, "task_ipc_mode " => 3 , )); $this->serv->on('Receive', array($this, 'onReceive')); $this->serv->on('Task', array($this, 'onTask')); $this->serv->on('Finish', array($this, 'onFinish')); $this->serv->start(); }
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { $serv->task( $data ); }
public function onTask($serv,$task_id,$from_id, $data) { $array = json_decode( $data , true ); if ($array['url']) { return $this->httpGet( $array['url'] , $array['param'] ); }
}
public function onFinish($serv,$task_id, $data) { }
protected function httpGet($url,$data){ if ($data) { $url .='?'.http_build_query($data) ; } $curlObj = curl_init(); curl_setopt($curlObj, CURLOPT_URL, $url); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curlObj, CURLOPT_HEADER, 0); $response = curl_exec($curlObj); curl_close($curlObj); return $response; }
} $server = new Server();
|