我正在我的 laravel 应用程序中实现一个 Vue 消息传递组件。如调试控制台所示,消息已成功发送到 Pusher,但我已经努力了三天让 laravel 收听,以便同一私人频道上的用户可以接收消息。
这是基于 github 上的这个应用程序 https://github.com/AfikDeri/Messenger-App-VueJS-and-Laravel
我已经仔细检查了可能有问题但找不到问题的代码的所有关键区域。
已清除 Laravel 中的缓存和配置,并多次重新编译 Javascript 代码。我正在测试 XAMPP 版本:7.2.12 / windows 7。
已尝试关闭 Windows 防火墙和 Avast Antivirus。
消息在 Pusher 调试控制台中被接收,并且也通过 axios 添加到数据库中,并被发送回发送客户端应用程序并推送到发送者的消息数组中。他们只是没有被收件人看到,我没有看到任何在 chrome 中广播事件的痕迹。
ChatApp.vue
<template>
<div class="chat-app">
<conversation :contact="selectedContact" :messages="messages" @new="saveNewMessage"></conversation>
<contacts-list :contacts="contacts" @selected="startConversationWith"></contacts-list>
</div>
</template>
<script>
export default{
props:{
user:{
type: Object,
required:true
}
},
data(){
return{
selectedContact: null,
messages: [],
contacts: [],
}
},
mounted(){
Echo.private(`messages.${this.user.id}`)
.listen('NewMessage', (e) => {
// console.log('Broadcast received.');
this.handleIncoming(e.message);
});
axios.get('/contacts')
.then((response) => {
console.log(response.data);
this.contacts = response.data;
});
},
methods:{
startConversationWith(contact){
axios.get(`/conversation/${contact.id}`)
.then((response) =>{
this.messages = response.data;
this.selectedContact = contact;
}
)
},
saveNewMessage(message){
this.messages.push(message);
},
handleIncoming(message) {
alert(JSON_stringify(message));
if (this.selectedContact && message.from_id == this.selectedContact.id) {
this.saveNewMessage(message);
return;
}
},
}
}
</script>
在 App\Events\NewMessage.php
namespace App\Events;
use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public function __construct(Message $message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new PrivateChannel('messages.' . $this->message->to_id);
}
public function broadcastWith()
{
$this->message->load('fromContact');
return ["message" => $this->message];
}
}
在 bootstrap.js 中
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
// key: process.env.MIX_PUSHER_APP_KEY,
//cluster: process.env.MIX_PUSHER_APP_CLUSTER,
key: '455936d5b071dd92ef25',
cluster: 'us3',
encrypted: false
});
require('./echo');
console.log(window.Echo.options);
在 App/config/app.php
Illuminate\Broadcasting\BroadcastServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
// Are uncommented
在 BroadcastServiceProvider
public function boot()
{
// Broadcast::routes();
/*changed this to fix a pusher auth error
only prob is it routes pusher to /home after authentication but without error */
Broadcast::routes(['middleware' => 'auth']);
require base_path('routes/channels.php');
}
在 channels.php 中:
Broadcast::channel('messages.{$id}', function ($user, $id) {
//dd($user->id, $id);
return $user->id === (int) $id;
});
在联系人控制器中
public function send(Request $request){
$message = new Message();
$message->from_id = auth()->user()->id;
$message->to_id = $request->contact_id;
$message->text = $request->text;
$message->conversation_id = 1;
if($message->save()) {
broadcast(new NewMessage($message));
return response()->json($message);
}
else{
return response()->json('failed');
}
在 chrome > 我看到这些消息
Pusher : State changed : connecting -> connected with new socket ID 52.5078004
registerEventsMessage called
//I think the next error is when I refreshed the browser forcing the Vue app to reconnect.
{event: "pusher:error", data: {code: 4009, message: "Connection not authorized within timeout"}}
data: {code: 4009, message: "Connection not authorized within timeout"}
event: "pusher:error"
//This appeared initially but not when messages were sent
No callbacks on presence-chat for pusher:subscription_error
No callbacks on private-messages.14 for pusher:subscription_error
当新消息发送到他们的频道但没有任何反应时,聊天应用程序应该在收件人聊天流中更新。我在 chrome 控制台中没有看到任何错误。用户 ID 和动态频道名称似乎可以正确呈现。
当我刷新浏览器时,我确实看到了这个错误,但在该消息似乎正常工作之后。推送控制台中没有显示错误,我可以看到那里收到了所有消息。
我对 Laravel 和 Vue 比较陌生,非常感谢有关如何排除故障的任何帮助或指导。