Laravel 广播不写入 redis 但直接调用 redis 工作

标签 laravel redis broadcast

我尝试了 sereval tuto 来尝试设置 Laravel Redis NodeJs Socket。

而且它不起作用。

到目前为止: - 我的广播触发了,正如我在 Laravel 日志中看到的那样 - 我的 redis 服务器工作(我在我的家庭 Controller 中添加了对 Redis 的调用,我可以在日志和 redis-cli 监视器中看到该值 - 我还安装了 Horizo​​n,我可以在 Redis-cli 监视器中看到它的调用)

但是当我的广播触发时,我在 redis-cli 监视器中看不到任何东西

我已经尝试过显而易见的事情(缓存/配置清除/重启/...)

非常感谢您的帮助!

我的家庭 Controller 中的 Redis 调用:工作

Redis::set("foo","bar");
$value = Redis::get("foo");
Log::debug('Redis value :'.$value);

.环境

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=siview
DB_USERNAME=root
DB_PASSWORD=


QUEUE_DRIVER=sync
QUEUE_CONNECTION=sync

CACHE_DRIVER=file

SESSION_DRIVER=file
SESSION_LIFETIME=120


BROADCAST_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

数据库.php

 'redis' => [
        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
            'read_write_timeout' => 60,
        ],

        'cache' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

         'pubsub' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 2,
        ],




    ],

广播.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Broadcaster
    |--------------------------------------------------------------------------
    |
    | This option controls the default broadcaster that will be used by the
    | framework when an event needs to be broadcast. You may set this to
    | any of the connections defined in the "connections" array below.
    |
    | Supported: "pusher", "redis", "log", "null"
    |
    */

    'default' => env('BROADCAST_DRIVER', 'redis'),

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => true,
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'pubsub',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

];

事件

<?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;
use App\User;


class SendMessageEvent  implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    private $user;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('App.User.' . $this->user->id);
    }

    public function broadcastWith()
    {
        return ['message' => 'Salut ' . $this->user->name];
    }
}

路由 channel

<?php

/*
|--------------------------------------------------------------------------
| Broadcast Channels
|--------------------------------------------------------------------------
|
| Here you may register all of the event broadcasting channels that your
| application supports. The given channel authorization callbacks are
| used to check if an authenticated user can listen to the channel.
|
*/

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return true;//(int) $user->id === (int) $id;
});

路由网络

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Auth::routes();

    Route::get('/send/{user}', function(\App\User $user){

    event(new \App\Events\SendMessageEvent($user));

    });

    Route::get('/home', 'HomeController@index')->name('home');

Laravel.log 显示我的广播

    [2018-11-04 09:39:42] local.INFO: Broadcasting [App\Events\SendMessageEvent] on channels [private-App.User.1] with payload:
    {
        "message": "Salut nathalie",
        "socket": null
    }  

显示我的家庭 Controller 测试的 redis-cli 监视器

1541324518.159040 [0 127.0.0.1:59334] "SELECT" "0"
1541324518.159776 [0 127.0.0.1:59334] "SET" "foo" "bar"
1541324518.160360 [0 127.0.0.1:59334] "GET" "foo"

最佳答案

我发现了,有点奇怪:

我正在使用 wamp 在 Windows 上工作。

wamp 服务器不是由 Laravel 服务器启动的。

奇怪的是一切似乎都运行良好(没有错误,页面呈现,auth 正在工作......)

我刚开始使用 laravel:php artisan serve 现在我可以在 Redis-cli 监视器中看到:

1541328214.873429 [0 127.0.0.1:52962] "SELECT" "2"
1541328214.874260 [2 127.0.0.1:52962] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\\\Events\\\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
1541328661.366923 [0 127.0.0.1:53078] "SELECT" "2"
1541328661.367643 [2 127.0.0.1:53078] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\\\Events\\\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"
1541328679.554077 [0 127.0.0.1:53083] "SELECT" "2"
1541328679.555197 [2 127.0.0.1:53083] "PUBLISH" "private-App.User.1" "{\"event\":\"App\\\\Events\\\\SendMessageEvent\",\"data\":{\"message\":\"Salut nathalie\",\"socket\":null},\"socket\":null}"

关于Laravel 广播不写入 redis 但直接调用 redis 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139618/

相关文章:

ruby-on-rails-3 - 在 Heroku 上运行调度程序或延迟作业的最佳实践是什么?

iOS音视频直播解决方案

android - 短信接收广播接收器中止

php - 在 laravel 5.4 中检索软删除的用户帖子

php - 理解 Laravel : Please explain "->with(' i', ($request->input ('page' , 1) - 1) * 5); ”

php - 验证用户实例不工作 Laravel 5.0

php - 使用 pecl 安装 redis

php - 在 laravel 中获取与自身类的关系时出现非法偏移类型错误

nosql - 如何浏览/查看存储在 Redis 中的值

android - 权限拒绝 : not allowed to send broadcast android. intent.action.HEADSET_PLUG