test websocket connection

This commit is contained in:
Čarodej
2022-05-20 16:48:25 +02:00
parent ab65aa8859
commit 63da860df5
8 changed files with 176 additions and 5 deletions
@@ -0,0 +1,21 @@
<?php
namespace Domain\Settings\Controllers;
use Domain\Settings\Events\TestWebsocketConnectionEvent;
use Illuminate\Http\JsonResponse;
class TestWebsocketConnectionController
{
public function __invoke(): JsonResponse
{
TestWebsocketConnectionEvent::dispatch(
auth()->user()
);
return response()->json([
'type' => 'success',
'message' => 'The websocket test event was successfully dispatched.',
]);
}
}
@@ -0,0 +1,40 @@
<?php
namespace Domain\Settings\Events;
use App\Users\Models\User;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class TestWebsocketConnectionEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(
public User $user,
) {
}
/**
* The event's broadcast name.
*/
public function broadcastAs(): string
{
return 'TestWebsocketConnection';
}
/**
* Get the channels the event should broadcast on.
*/
public function broadcastOn(): PrivateChannel
{
return new PrivateChannel("App.Users.Models.User.{$this->user->id}");
}
}