1

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

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

POST http://localhost/broadcasting/auth 404(未找到)推送器:无法检索身份验证信息。404客户端必须经过身份验证才能加入私人或在线频道

在此处输入图像描述

聊天事件.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');
    }
}

应用程序.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);
    });

   } 

});

频道.php

<?php



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


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

return true;

});

聊天控制器.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));

}




}

引导程序.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
 });

这个错误的原因可能是什么?任何解决方案

4

7 回答 7

2

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

我的 laravel 应用程序正在运行,port 8000而端口laravel-echo-serverport 80所以我所要做的就是更改"authHost": "http://localhost""authHost": "http://localhost:8000"laravel-echo-server.json

希望有一天它会帮助某人

于 2018-11-12T14:25:45.683 回答
2

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

于 2018-05-10T17:50:47.243 回答
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,此外在带有“/public”的 Xampp 和没有的实时服务器上。我知道这做得不好,但它工作。如何有正确的想法?

于 2020-01-18T17:15:49.663 回答
1

在 routes 文件夹的 channel.php 文件中添加通道路由

Broadcast::channel('your-channel-name', function ($user, $id) {
    return true;
});

这样所有用户都可以通过返回 true 来访问它

于 2018-03-28T11:32:45.947 回答
0

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

auth: {
            headers: {
                Authorization: 'Bearer ' + this.props.token,
            }
        },
于 2018-03-28T08:31:52.633 回答
0

检查您的config/app.php文件并取消注释App\Providers\BroadcastServiceProvider::class并再次检查希望它会起作用。

于 2019-07-19T15:15:49.647 回答
0

这对我有用。
取消注释 App\Providers\BroadcastServiceProvider::class, in app co

于 2021-07-16T22:25:28.447 回答