我正在使用 Laravel 5.1 并遵循此处的指南: http://laravel.com/docs/5.1/redis#pubsub
我创建了一个简单的 socket.io 服务器,并在客户端向我的 channel 发送了一条消息,socket.io 服务器能够记录该消息。
但是,我运行了我为 redis 发布/订阅所做的命令,当客户端发出消息时它没有收到任何东西。过了一会儿,抛出一个错误:
[Predis\Connection\ConnectionException] Error while reading line from the server. [tcp://127.0.0.1:6379]
然后我尝试在命令中使用 publish 方法,它起作用了。 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。
最佳答案
Laravel 5.2 对 config/database.php 的更改
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'subscribe' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_write_timeout' => 0
],
],
关于php - redis pub/sub 订阅返回连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30913364/