初识 swoole【下】

2019-08-01 15:19:43   PHP

  PHP   Swoole  

前言:接初识swoole【上】,这篇主要是异步问题

有收获的话请加颗小星星,没有收获的话可以 反对 没有帮助 举报三连

6、异步任务

  1. <?php
  2. // new
  3. $server = new swoole_server(HOST, "9505",SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
  4. //设置异步任务的工作进程数量
  5. $server->set(['task_worker_num' => 4]);
  6. // connect 连接
  7. $server->on("connect", function (swoole_server $server, $fd) {
  8. dump("{$fd}连接");
  9. $server->send($fd, "欢迎{$fd}大山驴\n");
  10. });
  11. // receive 回调
  12. $server->on("receive", function (swoole_server $server, $fd, $from_id, $data) {
  13. //投递异步任务
  14. $task_id = $server->task($data);
  15. dump("触发异步任务ID={$task_id}");
  16. $server->send($fd, "服务端回复:{$data}\n");
  17. foreach ($server->connections as $connection) {
  18. if ($connection != $fd){
  19. $server->send($connection, "{$fd}说{$data}");
  20. }
  21. }
  22. });
  23. // task 处理异步任务
  24. $server->on("task", function (swoole_server $server, $task_id, $from_id, $data){
  25. dump("新的异步任务[ID={$task_id}]");
  26. //返回任务执行的结果
  27. $server->finish("{$data}完成了");
  28. });
  29. // finish 处理异步任务的结果
  30. $server->on("finish", function (swoole_server $server, $task_id, $data){
  31. dump("异步任务[{$task_id}]已经完成[{$data}]");
  32. });
  33. // close
  34. $server->on("close", function (swoole_server $server, $fd) {
  35. dump("{$fd}关闭");
  36. foreach ($server->connections as $connection) {
  37. if ($connection != $fd){
  38. $server->send($connection, "{$fd}断开连接");
  39. }
  40. }
  41. });
  42. // start
  43. $server->start();
  1. php async.php
  2. telnet 127.0.0.1 9505

7、同步TCP客户端

  1. <?php
  2. // new 同步
  3. $client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
  4. // connect
  5. if (! $client->connect(HOST, 9501, 0.5)){
  6. dump("连接失败");
  7. }
  8. // send
  9. if (! $client->send("你个山驴逼\n")) {
  10. dump("发送失败");
  11. }
  12. // receive
  13. if (! $data = $client->recv()) {
  14. dump("接收失败");
  15. }
  16. dump($data);
  17. // close
  18. $client->close();
  1. php tcp_server.php
  2. php tcp_sync_client.php

8、异步TCP客户端

  1. <?php
  2. // new 异步
  3. $client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
  4. // connect
  5. $client->on("connect", function (\Swoole\Client $cli){
  6. $cli->send("你个山驴逼\n");
  7. });
  8. // receive
  9. $client->on("receive", function (\Swoole\Client $cli, $data){
  10. dump("接收:{$data}");
  11. });
  12. // error
  13. $client->on("error", function (\Swoole\Client $cli){
  14. dump("连接失败");
  15. });
  16. // close
  17. $client->on("close", function (\Swoole\Client $cli){
  18. dump("连接关闭");
  19. });
  20. $client->connect(HOST, 9501, 0.5);
  1. php tcp_server.php
  2. php tcp_async_client.php

9、异步客户端

mysql

  1. // 异步mysql客户端
  2. $db = new \Swoole\Mysql();
  3. $config = [
  4. 'host' => '127.0.0.1',
  5. 'user' => 'root',
  6. 'password' => 'root',
  7. 'database' => 'mac',
  8. 'port' => '3307',
  9. ];
  10. $db->connect($config, function (\Swoole\Mysql $db, $rs) {
  11. $db->query('SELECT * FROM user', function (\Swoole\Mysql $db, $rs) {
  12. dump($rs);
  13. $db->close();
  14. });
  15. });

http

  1. // 异步http客户端
  2. $cli = new Swoole\Http\Client('127.0.0.1', 80);
  3. $cli->setHeaders(array('User-Agent' => 'swoole-http-client'));
  4. $cli->setCookies(array('test' => 'value'));
  5. $cli->get('/login', function (\Swoole\Http\Client $cli) {
  6. dump($cli->statusCode);
  7. dump($cli->cookies);
  8. dump($cli->headers);
  9. });

10、协程客户端

  1. // 协程mysql客户端
  2. $http = new swoole_http_server(HOST, 9506);
  3. $http->on('request', function ($request, $response) {
  4. $db = new \Swoole\Coroutine\Mysql();
  5. $db->connect([
  6. 'host' => '127.0.0.1',
  7. 'user' => 'root',
  8. 'password' => 'root',
  9. 'database' => 'mac',
  10. 'port' => '3307',
  11. ]);
  12. $data = $db->query('select * from user');
  13. dump($data);
  14. $response->end(json_encode($data));
  15. });
  16. $http->start();
  1. php coroutine_client.php
  2. curl -XGET "127.0.0.1:9506"