POST http:// localhost / broadcasting / auth 404(Not Found)错误显示

时间:2017-11-21 15:35:55

标签: laravel vuejs2 laravel-5.4 pusher laravel-echo

我正在尝试将我的应用程序连接到私人频道上的推送器。

但是我在控制台中收到以下错误:

  

POST http://localhost/broadcasting/auth 404(未找到)           Pusher:无法检索身份验证信息。必须对404客户端进行身份验证才能加入私人或在线渠道

enter image description here

ChatEvent.php

<?php

namespace App\Events;

use App\User;

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 ChatEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;


   public $message;
   public $user;



    public function __construct($message, User $user)
    {

       $this ->message = $message;

        $this ->user = $user;


    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('chat');
    }
}

App.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

import Vue from 'vue'
import VueChatScroll from 'vue-chat-scroll'
Vue.use(VueChatScroll)

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example', require('./components/Example.vue'));

Vue.component('chat-message', require('./components/ChatMessage.vue'));


const app = new Vue({
    el: '#app',

   data:{ 

    message:'',

     chat:{     

        message:[]

     } 

      },

   methods: {  send() { 

if(this.message.length !=0)
{

    this.chat.message.push(this.message);

    this.message= '';

}
   } },


   mounted()

   {

       Echo.private('chat')
    .listen('ChatEvent', (e) => {
        console.log(e.order.name);
    });

   } 

});

channels.php

<?php



Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});


Broadcast::channel('chat', function() {

return true;

});

ChatController.php

<?php

namespace App\Http\Controllers;

use App\Events\ChatEvent;
Use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class ChatController extends Controller
{


  public function __construct()

    {
        $this->middleware('auth');
    }


  public function chat()


  {

 return view('chat');

}


//public function send(Request $request)

//{

 // $user = User::find(Auth::id());

 // event(new ChatEvent($request -> $message, $user));

//}


public function send()

{

    $message = "Hello";

  $user = User::find(Auth::id());

  event(new ChatEvent( $message, $user));

}




}

bootstrap.js

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap-sass');
} catch (e) {}

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Next we will register the CSRF Token as a common header with Axios so that
 * all outgoing HTTP requests automatically have it attached. This is just
 * a simple convenience so we don't have to attach every token manually.
 */

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

import Echo from 'laravel-echo'

 window.Pusher = require('pusher-js');

 window.Echo = new Echo({
     broadcaster: 'pusher',
     key: '815bcd2378a647ffaad7',
      cluster: 'ap2',
    encrypted: false
 });

导致此错误的原因是什么?任何解决方案

7 个答案:

答案 0 :(得分:1)

我正在使用redis并遇到同样的问题,通过更改我的laravel-echo-server.json解决了这个问题 "authHost": "http://localhost/your-app-name/public", "authEndpoint": "/broadcasting/auth", 希望这有帮助

答案 1 :(得分:1)

我遇到了这个问题Client can not be authenticated, got HTTP status 404,并为我解决了这个问题, 问题出在laravel-echo-server配置文件中 laravel-echo-server.json

我在lar port 8000上运行了我的laravel应用,而laravel-echo-server的端口为port 80,所以我要做的就是将"authHost": "http://localhost"更改为  "authHost": "http://localhost:8000"中的laravel-echo-server.json

希望有一天能对某人有所帮助

答案 2 :(得分:1)

在我想运行代码的情况下,在xamp或/和真正的在线服务器上,(在http://127.0.0.1:8000/was上工作..)帮助:

 window.Echo = new Echo({
   authEndpoint : '/*******/public/broadcasting/auth',
    broadcaster: 'pusher',
    key: '********',
    cluster: '***',
    encrypted: true
});

在文件js / app.js中将正确的url路径添加到index.php 此外,在Xampp上使用“ / public”,在实时服务器上不使用。 我知道这做得不好,但是可以。怎么有正确的主意?

答案 3 :(得分:0)

404错误表示您未发送呼叫的路由。我有一个类似的问题,它通过将authEndpoint更改为&#39; http://my_virtual_host/broadcasting/auth&#39;来解决。解决问题后,您将收到403错误。禁止用户收听频道。解决方案是在客户端包含带laravel-echo配置的令牌。

auth: {
            headers: {
                Authorization: 'Bearer ' + this.props.token,
            }
        },

答案 4 :(得分:0)

在路径文件夹

的channel.php文件中添加频道路由
Broadcast::channel('your-channel-name', function ($user, $id) {
    return true;
});

通过返回true

,所有用户都可以访问它

答案 5 :(得分:0)

检查您的 config/app.php 文件,并取消注释 App\Providers\BroadcastServiceProvider::class 的注释,然后再次检查希望它可以工作。

答案 6 :(得分:0)

这对我有用。
在应用程序中取消注释 App\Providers\BroadcastServiceProvider::class