laravel-websockets/src/Dashboard/Http/Controllers/SendMessage.php

56 lines
1.5 KiB
PHP
Raw Normal View History

2018-11-25 21:24:31 +00:00
<?php
2018-11-26 08:55:06 +00:00
namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers;
2018-11-25 21:24:31 +00:00
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId;
2018-11-25 21:24:31 +00:00
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
2020-03-04 09:58:39 +00:00
use Illuminate\Http\Request;
use Pusher\Pusher;
2018-11-25 21:24:31 +00:00
class SendMessage
{
2020-08-18 17:21:22 +00:00
/**
* Send the message to the requested channel.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
2018-11-25 21:24:31 +00:00
public function __invoke(Request $request)
2018-12-04 08:08:25 +00:00
{
$validated = $request->validate([
2020-08-18 17:21:22 +00:00
'appId' => ['required', new AppId],
'key' => 'required|string',
'secret' => 'required|string',
'channel' => 'required|string',
'event' => 'required|string',
'data' => 'required|json',
2018-12-04 08:08:25 +00:00
]);
$this->getPusherBroadcaster($validated)->broadcast(
[$validated['channel']],
$validated['event'],
json_decode($validated['data'], true)
);
return 'ok';
}
2020-08-18 17:21:22 +00:00
/**
* Get the pusher broadcaster for the current request.
*
* @param array $validated
* @return \Illuminate\Broadcasting\Broadcasters\PusherBroadcaster
*/
2018-12-04 08:08:25 +00:00
protected function getPusherBroadcaster(array $validated): PusherBroadcaster
2018-11-25 21:24:31 +00:00
{
$pusher = new Pusher(
2018-12-04 08:08:25 +00:00
$validated['key'],
$validated['secret'],
$validated['appId'],
2018-11-25 23:54:29 +00:00
config('broadcasting.connections.pusher.options', [])
2018-11-25 21:24:31 +00:00
);
2018-12-04 08:08:25 +00:00
return new PusherBroadcaster($pusher);
2018-11-25 21:24:31 +00:00
}
2018-11-26 07:56:56 +00:00
}