我正在尝试在用户向所有朋友创建帖子时广播该事件。 Laravel Echo没有听取这些事件。我可以在Pusher
网站上看到日志。所以事件正在发送给推动者。
这是我的时间轴代码:
<template>
<div>
<div class="row">
<status-box :authUser="authUser"></status-box>
</div>
<div class="row">
<div class="col-lg-8">
<div v-if="statuses.length">
<status v-for="status in statuses" :status="status"></status>
</div>
<div v-else>
<p>No posts to show yet.</p>
</div>
</div>
</div>
</div>
<script>
import EventHub from './../eventhub.js';
export default {
data() {
return {
statuses: [],
authUser: null,
};
},
methods: {
pushNewStatus(status) {
status.parent_id = null;
this.statuses.unshift(status);
// Broadcasting the event to so that user's friends can see the post in realtime
Echo.private('status.' + status.id)
.listen('StatusWasPosted', (e) => {
this.pushNewStatus(e.status);
});
}
},
mounted() {
EventHub.$on('status-posted', this.pushNewStatus)
axios.get('/chatty/getStatuses/')
.then(({data}) => {
this.statuses = data.statuses;
this.authUser = data.user_name;
})
.catch(error => console.log(error));
}
}
这是频道授权逻辑:
Broadcast::channel('status.{statusId}', function ($user, $statusId) {
$status = Status::find($statusId);
$status_owner = User::find($status->user_id);
return $user->isFriendsWith($status_owner);
});
以下是StatusWasPosted
事件类
public function broadcastOn() {
return new PrivateChannel('status.' . $this->status->id);
}
创建新状态时,我在状态compoenet中发出status-posted
。
我哪里错了?