监听redis订阅
*/
public function onWorkerStart()
{
Redis::subscribe('asyncpub',function ($data){
if(!empty($data)){
$data = json_decode($data, true);
switch ($data['datatype']){
case 'request':
$this->https_curl($data['url'], [], 'GET');
break;
case 'cmd':
exec($data['commend']['cli'].' '.implode(' ',$data['commend']['param']));
break;
default:
}
Log::info("订阅消息接收参数",['data'=>$data]);
}
});
}
报错信息
这里粘贴报错
worker[async:122068] exit with status 64000
RedisException: read error on connection to 127.0.0.1:6379 in webman/vendor/illuminate/redis/Connections/PhpRedisConnection.php:461
Stack trace:
webman/vendor/illuminate/redis/Connections/PhpRedisConnection.php(461): Redis->subscribe()
有没有可能 127.0.0.1:6379 本地环境redis没有启动
肯定是启动了啊,但是由于是阻塞运行导致长时间的连接超时了,后面换成new 对象来调用就正常了 ~
new 对象和 使用静态有啥区别嘛? 是不是没有设置 一直阻塞 导致的 阻塞也是有超时默认时间的
不太清楚,用Redis::subscribe调用订阅 就一直繁忙 然后框架抛出异常64000,我是换成
public function onWorkerStart()
{
$redis = new \Workerman\Redis\Client('redis://127.0.0.1:6379');
$redis->subscribe('asyncpub',function ($channel, $message){
if(!empty($message)){
$message = json_decode($message, true);
switch ($message['datatype']){
case 'request':
$this->https_curl($message['url'], [], 'GET');
break;
case 'cmd':
shell_exec($message['commend']['cli'].' '.implode(' ',$message['commend']['param']));
break;
default:
}
}
Log::info("订阅消息接收参数",['message'=>$message]);
});
}
这种写法