Laravel广播活动私人

时间:2019-07-10 22:01:22

标签: laravel-5

我一直在尝试与Laravel和laravel-websockets合作的私人渠道

我克服了一些障碍,但是剩下的就是尝试进行身份验证:

preg_match(): Compilation failed: unrecognized character after (?< at offset 13

该错误似乎与:

 …/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php150 

我想相关的代码片段是echo配置并监听。

bootstrap.js

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    encrypted: false,
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true,
});

admin.blade.php

 Echo.private('messages.{{Auth::id()}}')
    .listen('NewMessageNotification', (e) => {
      console.log(e.message.message);
});

以防万一,channels.php

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

如果我删除了私人密码,一切正常。

1 个答案:

答案 0 :(得分:0)

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

});

问题是频道中的$ ...

这是正确的方法

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