我试图创建一个简单的聊天应用程序,我试图听私人频道,但我不能听它。不知道我在哪里犯的错误我的代码如下。
文件:chat.vue
export default {
props: {
user: {
type: Object,
required: true
}
},
data() {
return {
selectedContact: null,
messages: [],
contacts: []
};
},
mounted() {
window.Echo.private(`chats.${this.user.id}`)
.listen('NewChatMessage', (e) => {
console.log('heared');
// this.handleIncoming(e.message);
});
console.log('mounted');
axios.get('/chat/contacts')
.then((response) => {
this.contacts = response.data;
});
},
methods: {
startConversationWith(contact) {
axios.get(`/chat/conversation/${contact.id}`)
.then((response) => {
this.messages = response.data;
this.selectedContact = contact;
});
},
saveNewMessage(text) {
this.messages.push(text);
},
handleIncoming(message) {
if(this.selectedContact && this.selectedContact.id == message.from) {
this.saveNewMessage(message);
}
}
},
components: {Conversation, ContactsList}
}
文件名:routes/channels.php
Broadcast::channel('chats.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
文件名:app/events/newchatmessage.php
class NewChatMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $chat;
public $broadcastQueue = 'chat';
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Chat $chat)
{
$this->chat = $chat;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('chats.' . $this->chat->to);
}
public function broadcastWith()
{
return ["message" => $this->chat];
}
}
在保存时,我这样称呼事件 broadcast(new NewChatMessage($chat));
我的堆栈如下所示,
操作系统:windows 10
服务器:apache2
数据库:mysql
缓存:redis
1条答案
按热度按时间vsmadaxz1#
我发现laravel在redis的config/database.php文件中有一个前缀options config。我在redis的选项中注解了前缀。然后它开始工作。
这可能对将来的人有所帮助。