swoole监听redis数据的具体操作和代码是什么
Admin 2022-07-22 群英技术资讯 1005 次浏览
今天这篇我们来学习和了解“swoole监听redis数据的具体操作和代码是什么”,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解“swoole监听redis数据的具体操作和代码是什么”有一定的帮助。有这方面学习需要的朋友就继续往下看吧!
swoole如何监听redis数据?
Laravel使用swoole监听redis
开始之前,请先确保redis已经正确安装,并正常运行。
Laravel代码
在App\Events目录下新建RedisTest事件
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class RedisTest
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}App\Listeners\RedisTestListener 监听事件代码
<?php
namespace App\Listeners;
use App\Events\RedisTest;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
class RedisTestListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param RedisTest $event
* @return void
*/
public function handle(RedisTest $event)
{
$message = $event->message;
Log::info('the message received from subscribed redis channel msg_0: '.$message);
}
}App\Providers\EventServiceProvider 登记事件/监听关系
protected $listen = [
'App\Events\RedisTest' => [
'App\Listeners\RedisTestListener',
],
];监听命令
App\Console\Commands\RedisSubscribe 代码如下
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use swoole_redis;
use Illuminate\Support\Facades\Event;
use App\Events\RedisTest;
class RedisSubscribe extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'redis:subscribe';
/**
* The console command description.
*
* @var string
*/
protected $description = 'deamon process to subscribe redis broadcast';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$client = new swoole_redis;
$client->on('message', function (swoole_redis $client, $result) {
var_dump($result);
static $more = false;
if (!$more and $result[0] == 'message')
{
echo "trigger Event RedisTest\n";
Event::fire(new RedisTest($result[2]));
}
});
$client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) {
echo "connect\n";
$client->subscribe('msg_0');
});
}
}Laravel部分代码完成
==================================
supervisor 管理进程
在 /etc/supervisor/conf.d 文件夹下新建 echo.conf , 代码如下
[group:echos] programs=echo-queue,echo-redis [program:echo-queue] command=php artisan queue:work directory=/home/bella/Downloads/lnmp/echo1.0/echo user=bella autorestart=true redirect_stderr=true stdout_logfile=/home/bella/Downloads/lnmp/echo1.0/echo/storage/logs/queue.log loglevel=info [program:echo-redis] command=php artisan redis:subscribe directory=/home/bella/Downloads/lnmp/echo1.0/echo user=bella autorestart=true redirect_stderr=true stdout_logfile=/home/bella/Downloads/lnmp/echo1.0/echo/storage/logs/redis.log loglevel=info
完成后,执行以下命令重载
supervisorctl reload
=================================
进入redis 客户端,发布一个广播通知到 msg_0 频道
publish msg_0 "Hello Bella"
如果 laravel目录下的 storage\logs\laravel.log 最后的日志中记录了广播发送的通知,则redis监听功能实现
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在php中有两个函数,能够实现数字补零,str_pad()sprintf() 函数1: str_pad顾名思义这个函数是针对字符串来说的这个可以对指定的字符串填补任何其它的字符串例如:str_pad(带填补的字符串,填补后的长度,填补字符串,填补位置)其中填补后的长度必须是个正整数,填补位置有三个选项,左边:STR_PAD_LEFT,右边:STR_PAD_RIGHT,两
swoole保持不掉线的方法:Swoole扩展可以通过心跳机制来保持不掉线,这个机制保证服务器和客户端之间连接的有效性。Swoole扩展本身内置了这种机制,开发者只需要配置一个参数即可启用。
今天小编就为大家分享一篇关于PHP中rename()函数的妙用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
在本篇文章里小编给大家整理的是关于php服务器用什么系统的相关知识点内容,有兴趣的朋友们跟着学习参考下。
如何使用yii2生成验证码?验证码的使用可以说是很频繁的了,我们在做账号登陆的时候,经常需要做验证码验证,那么我们具体要如何做一个验证码呢?YII2中已经帮我们做好了封装,下面我们就来看看YII2框架中验证码的使用方法。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008