我在 Laravel 5.1 上并遵循此处的指南:http: //laravel.com/docs/5.1/redis#pubsub
我创建了一个简单的 socket.io 服务器,在客户端我向我的频道发送了一条消息,socket.io 服务器能够记录该消息。
但是,我运行了我为 redis pub/sub 制作的命令,当客户端发出消息时它没有收到任何内容。过了一会儿,抛出一个错误:
[Predis\Connection\ConnectionException] 从服务器读取行时出错。[tcp://127.0.0.1:6379]
然后我尝试在命令中使用发布方法,它可以工作。socket.io 服务器能够记录消息。
这是我的控制台命令
<?php
namespace App\Console\Commands;
use Illuminate\Support\Facades\Redis;
use Illuminate\Console\Command;
class ChannelSub extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'channel:sub';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Redis::subscribe(['my-channel'], function($message) {
$this->info($message);
});
}
}
试试看
php artisan channel:sub
我在 Laravel 中使用 predis/predis 来支持 Redis。