在linux下安装很简单:

  1. pecl install swoole

用这种方式可以自动下载安装。
我采取的是第二种方式噢。
安装完成,需要更改php.ini的配置,将

  1. extension=swoole.so

放在这个配置中,然后重启nginx。完成以后执行

  1. php -m

查看是否有swoole。有swoole说明安装成功。
新建一个server.php

  1. <?php
  2. class Server
  3. {
  4. private $serv;
  5. public function __construct() {
  6. $this->serv = new swoole_server("127.0.0.1", 9501);
  7. $this->serv->set(array(
  8. 'worker_num' => 1, //一般设置为服务器CPU数的1-4倍
  9. // 'daemonize' => 1, //以守护进程执行
  10. 'max_request' => 10000,
  11. 'dispatch_mode' => 2,
  12. 'task_worker_num' => 8, //task进程的数量
  13. "task_ipc_mode " => 3 , //使用消息队列通信,并设置为争抢模式
  14. "package_eof" => PHP_EOL, // 设置EOF
  15. "log_file" => "log/taskqueueu.log" ,//日志
  16. ));
  17. $this->serv->on('Receive', array($this, 'onReceive'));
  18. // bind callback
  19. $this->serv->on('Task', array($this, 'onTask'));
  20. $this->serv->on('Finish', array($this, 'onFinish'));
  21. $this->serv->start();
  22. }
  23. public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
  24. echo "Get Message From Client {$fd}:{$data}n";
  25. // send a task to task worker.
  26. $serv->send($fd, "Server: ".$data);
  27. $serv->task( $data );
  28. }
  29. public function onTask($serv,$task_id,$from_id, $data) {
  30. $array = json_decode( $data , true );
  31. file_put_contents("test.txt", "onTask......\r\n", FILE_APPEND);
  32. file_put_contents("test.txt", $data."\r\n", FILE_APPEND);
  33. sleep(6);
  34. file_put_contents("test.txt", "onTask after sleep......\r\n", FILE_APPEND);
  35. if ($array['url']) {
  36. return $this->httpGet( $array['url'] , $array['param'] );
  37. }
  38. }
  39. public function onFinish($serv,$task_id, $data) {
  40. //echo "Task {$task_id} finishn";
  41. $temp = json_encode($data);
  42. file_put_contents("test.txt", "onFinish.{$temp}.....\r\n", FILE_APPEND);
  43. echo "Task {$task_id} finish\n";
  44. echo "Result: {$data}\n";
  45. //echo "Result: {$data}n";
  46. }
  47. protected function httpGet($url,$data){
  48. if ($data) {
  49. $url .='?'.http_build_query($data) ;
  50. }
  51. $curlObj = curl_init(); //初始化curl,
  52. curl_setopt($curlObj, CURLOPT_URL, $url); //设置网址
  53. curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); //将curl_exec的结果返回
  54. curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, FALSE);
  55. curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, FALSE);
  56. curl_setopt($curlObj, CURLOPT_HEADER, 0); //是否输出返回头信息
  57. $response = curl_exec($curlObj); //执行
  58. curl_close($curlObj); //关闭会话
  59. return $response;
  60. }
  61. }
  62. $server = new Server();

再弄一个cleint.php

  1. <?php
  2. class Client
  3. {
  4. private $client;
  5. public function __construct() {
  6. $this->client = new swoole_client(SWOOLE_SOCK_TCP);
  7. }
  8. public function connect() {
  9. if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
  10. echo "Connect Error";
  11. }
  12. $data = array(
  13. "url" => "http://test.dev/a.php" , //这个是在服务器去请求的
  14. "param" => array(
  15. "username"=>'test',
  16. "password" => 'test'
  17. )
  18. );
  19. $json_data = json_encode($data);
  20. $this->client->send( $json_data );
  21. }
  22. public function ret()
  23. {
  24. return $this->client->recv();
  25. }
  26. }
  27. echo 'this'.PHP_EOL;
  28. $client = new Client();
  29. $client->connect();
  30. //从服务器接收数据
  31. $data = $client->ret();
  32. if (!$data)
  33. {
  34. die("recv failed.");
  35. }
  36. var_dump($data);
  37. echo 'dfdfdfd';

可以在test.dev下访问到的a.php

  1. <?php
  2. /**
  3. * @Author: Administrator
  4. * @Date: 2017-04-17 14:07:44
  5. * @Last Modified by: Administrator
  6. * @Last Modified time: 2017-04-20 15:33:44
  7. */
  8. json_encode($_REQUEST);
  9. file_put_contents("test.txt", "This is another something.".json_encode($_REQUEST), FILE_APPEND);
  10. echo json_encode( ['test' => 'test-hello']);

然后启动: php server.php
在浏览器访问 client.php
就可以看到效果啦。

分类: web

标签:   php