自定义进程实现redis订阅

实现代码

  1. namespace App;
  2. use EasySwoole\Core\Swoole\Process\AbstractProcess;
  3. use Swoole\Process;
  4. class Subscribe extends AbstractProcess
  5. {
  6. public function run(Process $process)
  7. {
  8. // TODO: Implement run() method.
  9. $redis = new \Redis();
  10. $redis->connect('127.0.0.1');
  11. $redis->subscribe(['ch1'],function (){
  12. var_dump(func_get_args());
  13. });
  14. }
  15. public function onShutDown()
  16. {
  17. // TODO: Implement onShutDown() method.
  18. }
  19. public function onReceive(string $str, ...$args)
  20. {
  21. // TODO: Implement onReceive() method.
  22. }
  23. }

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

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