繁体   English   中英

如何使用laravel-echo-server在laravel广播中通过私人频道接收套接字

[英]How to receive socket via private channel in laravel broadcasting using laravel-echo-server

我使用 Laravel 5.7 和 redis/laravel-echo-server 进行广播。 前端是 vuejs,目前在公共频道广播也在工作。 但是在专用通道中前端接收套接字不起作用

以下是我的代码库。

此代码部分是用于身份验证的路由代码

Broadcast::channel('notification-{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

这是活动部分

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

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $user;
    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('notification-' . $this->user->id);
    }

    public function broadcastWith()
    {
        return ['data' => 'this is message'];
    }

}

这是前端代码

//bootstrap.js
 window.Echo = new Echo({
    broadcaster: "socket.io",
    host: window.location.hostname + ":6001"
 });

// 接收Vue文件

export default {
  mounted() {
    Echo.private("notification-" + window.Laravel.user).listen(
      "NotificationEvent",
      e => {
        console.log("there");
      }
    );
  }
};

加入私人频道没问题,但我无法收到来自 Laravel 活动的任何消息

Route::get('test-broadcast', function () {
    $user = \App\User::findOrFail(1);
    broadcast(new NotificationEvent($user));
});

[2019-02-22 10:46:28] local.INFO: Broadcasting [App\Events\PrivateNotificationProcedure] on channels [private-notification-procedure-1] with payload:
{
    "socket": null
}  

我不确定出了什么问题。

非常感谢

运行命令php artisan config:clear它对我有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM