Laravel - 与公共渠道不同,私人渠道不起作用

发布于 2025-01-18 13:27:51 字数 2598 浏览 2 评论 0原文

我正在使用 Laravel 9.1,并尝试在私人频道上广播,但与公共频道不同,它由于某种原因不起作用!

我有一个这样的事件:

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

   public string $message;

   public function __construct($message = null)
   {
       $this->message = $message;
   }

   public function broadcastOn()
   {
       return new PrivateChannel('notification.' . auth()->user()->username);
   }
}

当我在 broadcastOn 中返回一个 Channel 实例时,一切都运行良好,但是当我将其更改为 PrivateChannel 时,它不再起作用了。

channels.php

Broadcast::channel('notification.{username}', function ($user, $username) {
    return true;
});

我已经尝试了 soketiwebsockets;在这两个方面,我得到了相同的结果。

配置

BroadcastServiceProvider.php 文件中,我尝试了所有这些可能性:

  • Broadcast::routes();
  • Broadcast::routes(['middleware' => ['auth:api']]);

它们都不起作用。

config/broadcasting.php

      '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'),
              'host' => env('PUSHER_HOST', '127.0.0.1'),
              'port' => env('PUSHER_PORT', 6001),
              'scheme' => env('PUSHER_SCHEME', 'http'),
              'useTLS' => false,
              'encrypted' => false,
          ],
      ],

注意

user

当我在使用 \Illuminate\Support\Facades\Log::info($ ); 它不记录任何内容!

Broadcast::channel('notification.{username}', function ($user, $username) {
    \Illuminate\Support\Facades\Log::info($user);
    return true;
});

套接字服务器

当我在公共频道上广播时,websockets 控制台返回:

Connection id 76490676.561659290 sending message {"channel":"notification.admin","event":"App\\Events\\newMessage","data":"{\"message\":\"[2022\\\/04\\\/03 06:26:53]: Pigeon had finished. 'As if it wasn't very civil of you to learn?' 'Well, there was mouth enough for it now, I suppose, by being drowned in my size; and as Alice could not be denied, so she tried.\",\"date\":\"2022-04-03T06:26:53.988364Z\"}"}

但是在私人频道中,当我尝试广播某些内容时,它不会返回或记录任何内容完全没有; 所以看起来由于某种原因,Laravel 甚至没有将请求发送到套接字服务器。

I'm using Laravel 9.1, and trying to broadcast on private channels, but unlike the public channels, it doesn't work for some reason!

I have an event like this:

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

   public string $message;

   public function __construct($message = null)
   {
       $this->message = $message;
   }

   public function broadcastOn()
   {
       return new PrivateChannel('notification.' . auth()->user()->username);
   }
}

When in broadcastOn I return an instance of Channel, everything works pretty well, but when I change it to PrivateChannel, it doesn't work anymore.

channels.php

Broadcast::channel('notification.{username}', function ($user, $username) {
    return true;
});

I've tried both soketi and websockets; In both, I had the same result.

Configs

In the the BroadcastServiceProvider.php file, I've tried all these possibilities:

  • Broadcast::routes();
  • Broadcast::routes(['middleware' => ['auth:api']]);

none of them worked.

config/broadcasting.php

      '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'),
              'host' => env('PUSHER_HOST', '127.0.0.1'),
              'port' => env('PUSHER_PORT', 6001),
              'scheme' => env('PUSHER_SCHEME', 'http'),
              'useTLS' => false,
              'encrypted' => false,
          ],
      ],

Note

Authentication Routes Don't Execute

When I try to log something in the authentication scope in the channels.php file before returning a boolean using \Illuminate\Support\Facades\Log::info($user); it doesn't log anything!

Broadcast::channel('notification.{username}', function ($user, $username) {
    \Illuminate\Support\Facades\Log::info($user);
    return true;
});

Socket Servers

When I broadcast on public channels, websockets console returns:

Connection id 76490676.561659290 sending message {"channel":"notification.admin","event":"App\\Events\\newMessage","data":"{\"message\":\"[2022\\\/04\\\/03 06:26:53]: Pigeon had finished. 'As if it wasn't very civil of you to learn?' 'Well, there was mouth enough for it now, I suppose, by being drowned in my size; and as Alice could not be denied, so she tried.\",\"date\":\"2022-04-03T06:26:53.988364Z\"}"}

But in private channels, when I try to broadcast something, it doesn't return or log anything at all; so it looks for some reason, Laravel even doesn't send the request to the socket server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文