2

我正在尝试了解如何在用户加入频道后捕获事件后端。我的意思是在对 Private 或 Presence 通道进行身份验证之后。

例如,在那之后:

Broadcast::channel('chat.{roomId}', function ($user, $roomId) {
    if ($user->canJoinRoom($roomId)) {
        return ['id' => $user->id, 'name' => $user->name];
    }
});

成功进行,我希望有可能捕捉到一个事件以知道用户加入了频道(不仅仅是前端)。

关于如何处理它的任何提示?

参考:https ://laravel.com/docs/6.x/broadcasting#joining-presence-channels

4

1 回答 1

0

为了在后端处理事件,客户端需要在加入频道时进行通信。

axios.post('/channel/join')
     .then(res => res.data)
     .then(data => console.log);

在后端,您可以在 (eg) 中有一条路线routes/web.php

Route::post('/channel/join', 'ChannelController@joined');

然后,ChannelController.php您可以处理该操作并执行您想要的操作:

public function joined(Request $request)
{
    $user = auth()->user();

    broadcast(new UserJoinedChannel($user))->toOthers();

    return ['status' => 'Channel joined successfully!'];
}
于 2020-08-25T23:59:37.560 回答