自定义进程实现redis订阅

实现代码

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Tioncico
  5. * Date: 2018/10/18 0018
  6. * Time: 10:28
  7. */
  8. namespace App\Process;
  9. use EasySwoole\Component\Process\AbstractProcess;
  10. use Swoole\Process;
  11. class Subscribe extends AbstractProcess
  12. {
  13. public function run($arg)
  14. {
  15. // TODO: Implement run() method.
  16. $redis = new \Redis();//此处为伪代码,请自己建立连接或者维护
  17. $redis->connect('127.0.0.1');
  18. $redis->subscribe(['ch1'],function (){
  19. var_dump(func_get_args());
  20. });
  21. }
  22. public function onShutDown()
  23. {
  24. // TODO: Implement onShutDown() method.
  25. }
  26. public function onReceive(string $str, ...$args)
  27. {
  28. // TODO: Implement onReceive() method.
  29. }
  30. }

接下来,需要做的事情,就是到EasySwooleEvent.php的主服务创建事件中,注册该进程即可。

  1. use App\Process;
  2. use EasySwoole\Core\Swoole\Process\ProcessManager;
  3. \EasySwoole\Component\Process\Manager::getInstance()->addProcess(new Subscribe('sub'));