我无法让我的事件在 laravel 7.x、pusher、laravel-echo 上运行。我已经正确设置了所有内容,但仍然收到 401 错误。我还在 .env 文件中将broadcast_driver 设置为推送器。如果有人可以提供帮助,我将不胜感激。评论事件.php<?phpnamespace App\Events;use Illuminate\Broadcasting\Channel;use Illuminate\Broadcasting\InteractsWithSockets;use Illuminate\Broadcasting\PresenceChannel;use Illuminate\Broadcasting\PrivateChannel;use Illuminate\Contracts\Broadcasting\ShouldBroadcast;use Illuminate\Foundation\Events\Dispatchable;use Illuminate\Queue\SerializesModels;class CommentEvent implements ShouldBroadcast{ use Dispatchable, InteractsWithSockets, SerializesModels; public $comment; /** * Create a new event instance. * * @return void */ public function __construct($comment) { $this->comment =$comment; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new Channel('comment-channel'); } public function broadcastAs() { return 'newComment'; }}CommentController.php<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Events\CommentEvent;use App\Comment;class CommentController extends Controller{ public function index(){ return view('comments'); } public function fetchComments(){ $comments =Comment::all(); return response()->json($comments); } public function store(Request $request){ $comment =Comment::create($request->all()); event(new CommentEvent($comment)); return response()->json('ok'); }}
1 回答
慕姐4208626
TA贡献1852条经验 获得超7个赞
确保将$user传递到通道路由回调中。将此代码替换为您的频道路由。那么你就可以走了:)
Broadcast::channel('comment-channel', function ($user) { return true; });
- 1 回答
- 0 关注
- 109 浏览
添加回答
举报
0/150
提交
取消