祝你今天愉快 !
我有一个问题:我设置了Laravel Echo&Pusher但是得到了这个错误,不知道要解决:(
我检查了我的app-key,app-cluster但都是正确的。
有人能帮我吗?
app.js
const app = new Vue({
el: '#app',
data: {
messages: []
},
methods:{
addMessage(message){
this.messages.push(message);
axios.post('/messages', message).then(response => {
console.log(response);
});
}
},
created(){
axios.get('/messages').then(response => {
this.messages = response.data;
});
Echo.channel('chatroom')
.listen('MessageEvent', (e) => {
console.log(e);
});
}
})
bootstrap.js
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: '************',
cluster: 'ap1',
encrypted: false
});
MessageEvent事件
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message, $user;
public function __construct(Message $message, User $user)
{
$this->message = $message;
//query
$this->user = $user;
}
public function broadcastOn()
{
return new PresenceChannel('chatroom');
}
channels.php
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('chatroom', function ($user, $id) {
return $user;
});
错误403或500 /使用Laravel版本> 5.3&Pusher进行广播/验证,您需要更改资源/ assets / js / bootstrap.js中的代码
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your key',
cluster: 'your cluster',
encrypted: true,
auth: {
headers: {
Authorization: 'Bearer ' + YourTokenLogin
},
},
});
并在app / Providers / BroadcastServiceProvider.php中更改
Broadcast::routes()
同
Broadcast::routes(['middleware' => ['auth:api']]);
要么
Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT
它对我有用,希望对你有所帮助。
我认为你需要给出一个真实的观点,如果你已经使用了laravel echo,只需转到资源/ assets / js / bootstrap.js
只需在窗口中添加以下行
Echo = new Echo({
authEndpoint : 'http://localhost/projectName/public/broadcasting/auth',
});
因为你没有从事件中传递,所以删除$ id
Broadcast::channel('chatroom', function ($user) {
return true;
});
如果您正在使用localhost,请确保您的.env文件已正确设置
尝试设置
APP_URL=http://localhost
DB_HOST=localhost
并运行
php artisan config:cache
希望这会帮助你。