laravel-websockets/src/HttpApi/Controllers/TriggerEventController.php

58 lines
1.9 KiB
PHP
Raw Normal View History

2018-11-21 11:13:40 +00:00
<?php
2018-11-27 15:42:19 +00:00
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
2018-11-21 11:13:40 +00:00
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
use Illuminate\Http\Request;
2018-11-21 11:13:40 +00:00
2018-11-27 20:15:29 +00:00
class TriggerEventController extends Controller
2018-11-21 11:13:40 +00:00
{
2020-08-18 17:21:22 +00:00
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
2018-11-21 11:13:40 +00:00
public function __invoke(Request $request)
{
2018-11-26 08:03:04 +00:00
$this->ensureValidSignature($request);
2018-11-22 17:13:43 +00:00
2020-08-27 11:13:17 +00:00
$channels = $request->channels ?: [];
foreach ($channels as $channelName) {
2018-12-01 11:26:08 +00:00
$channel = $this->channelManager->find($request->appId, $channelName);
2018-11-21 14:42:04 +00:00
2020-08-27 11:13:17 +00:00
$payload = (object) [
2018-12-01 11:26:08 +00:00
'channel' => $channelName,
2020-08-27 11:13:17 +00:00
'event' => $request->name,
'data' => $request->data,
];
2020-08-27 11:35:29 +00:00
if ($channel) {
$channel->broadcastToEveryoneExcept(
$payload, $request->socket_id, $request->appId
);
} else {
// If the setup is horizontally-scaled using the Redis Pub/Sub,
// then we're going to make sure it gets streamed to the other
// servers as well that are subscribed to the Pub/Sub topics
// attached to the current iterated app & channel.
// For local setups, the local driver will ignore the publishes.
$this->replicator->publish($request->appId, $channelName, $payload);
}
2018-11-26 21:11:47 +00:00
2020-08-17 18:06:51 +00:00
DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [
'channel' => $channelName,
'event' => $request->json()->get('name'),
'payload' => $request->json()->get('data'),
]);
2018-12-03 12:33:20 +00:00
2018-12-03 15:23:20 +00:00
StatisticsLogger::apiMessage($request->appId);
2018-11-21 14:42:04 +00:00
}
2018-11-21 23:12:23 +00:00
2018-11-21 11:13:40 +00:00
return $request->json()->all();
}
2018-12-04 21:22:33 +00:00
}