【问题标题】:how to override pusher config values?如何覆盖推送器配置值?
【发布时间】:2021-01-06 05:26:05
【问题描述】:

我需要为我的网络应用程序的特定部分使用不同的 Pusher 帐户,因此我尝试使用以下内容覆盖配置:

   public function send(Request $request)
    {
        $general_pusher = config('broadcasting.connections.pusher');
        $message = Message::create([
            'from' => auth()->id(),
            'to' => $request->contact_id,
            'text' => $request->text
        ]);
        \Config::set('broadcasting.connections.pusher', config('cfg.internal_chat_pusher'));
        logger(config('broadcasting.connections.pusher'));
        broadcast(new NewMessage($message));
        \Config::set('broadcasting.connections.pusher', config('cfg.internal_chat_pusher'));
        return response()->json($message);
    }

然后,我尝试使用 Laravel 应该引发错误的随机值进行测试,但没有错误并且消息被发送和接收,但使用原始/旧推送器配置值。

如您所见,我使用了logger 指令,它给了我以下信息:

[2020-09-19 14:06:09] local.DEBUG: array (
  'driver' => 'pusher',
  'key' => '1',
  'secret' => '1',
  'app_id' => '1',
  'options' => 
  array (
    'cluster' => '1',
    'useTLS' => true,
  ),
)  

但是,即使 logger 命令输出了应该给出错误的值,我也没有收到任何错误,并且消息可以完美地发送和接收。

如何为特定事件使用不同的推送者帐户?

更新:

我尝试编辑 brodcasting.php 如下:

 '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'),
                'useTLS' => true,
            ],
        ],

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

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

        'null' => [
            'driver' => 'null',
        ],
        'internal_chat_pusher' => [
            'driver' => 'pusher',
            'key' => env('INTERNAL_CHAT_PUSHER_APP_KEY'),
            'secret' => env('INTERNAL_CHAT_PUSHER_APP_SECRET'),
            'app_id' => env('INTERNAL_CHAT_PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('INTERNAL_CHAT_PUSHER_APP_CLUSTER', 'ap2'),
                'useTLS' => true,
            ]
            ],

    ],


];

然后在控制器内部的发送函数中:

 public function send(Request $request)
    {
        $message = Message::create([
            'from' => auth()->id(),
            'to' => $request->contact_id,
            'text' => $request->text
        ]);
        Broadcast::driver('internal_chat_pusher');
        broadcast(new NewMessage($message));
        return response()->json($message);
    }
}

但仍然使用原始/旧帐户配置发送消息。

【问题讨论】:

    标签: laravel laravel-5 laravel-5.8 pusher


    【解决方案1】:

    这两个选项都应该有效,但在 brodcasting.php 中使用单独的驱动程序是一种更简洁的方法。

    您的代码是正确的,但您还必须记住从命令行:

    php artisan queue:restart
    

    这将考虑新代码。我自己被这个烧了十几次。

    【讨论】:

      猜你喜欢
      • 2016-10-14
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      相关资源
      最近更新 更多