我一直困扰着很多天……
我将Laravel-echo与socket.io一起使用,当我尝试使用公共频道时,效果很好,但是当我尝试使用私有或在线状态频道时,出现此错误:
“客户端无法通过身份验证,HTTP状态为404”
=> App \ Providers \ BroadcastServiceProvider :: class,已经取消注释
我的Vue页面:
Echo.private('post').listen('comments', (e) => {
this.messages.push(e[0]) ;
});
bootstrap.js
import Echo from 'laravel-echo'
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001' ,
});
事件:
class comments implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
private $comment , $receiverId ;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($comment , $receiverId)
{
$this->comment = $comment ;
$this->$receiverId = $receiverId ;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
//i try PrivateChannel('post.'+$this->receiverId ); ...
return new PrivateChannel('post');
}
public function broadcastWith(){
return [ $this->comment ] ;
}
}
路线/通道:
Broadcast::channel('post', function ($user , $id) {
return true ; // i already try this (int) $user->id !== null;
});
BroadcastServiceProvider:
public function boot()
{
require base_path('routes/channels.php');
Broadcast::routes();
}
我使用php artisan queue:为队列工作
对于Redis,我在计算机上安装了redis-server.exe
请帮助! !!!!