Private
Public Access
1
0

chg: dev: replace the legacy gos/web-socket-bundle & replace it with Mercure protocol #4

This commit is contained in:
2026-04-09 22:00:53 +02:00
parent b55c223d8a
commit 7219471a86
33 changed files with 1198 additions and 2324 deletions

View File

@@ -12,7 +12,6 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
/**
@@ -29,8 +28,11 @@ class GameController extends AbstractController
{
public function __construct(
#[Autowire(env: 'APP_ENV')]
private readonly string $env,
private readonly RequestStack $request,
private readonly string $env,
#[Autowire(env: 'MERCURE_PUBLIC_URL')]
private readonly string $mercurePublicUrl,
#[Autowire(env: 'MERCURE_SUBSCRIBER_JWT')]
private readonly string $mercureSubscriberJwt,
) {
}
@@ -42,8 +44,9 @@ class GameController extends AbstractController
public function play(): Response
{
return $this->render('Game/play.html.twig', [
'env' => $this->env,
'ssl' => $this->request->getCurrentRequest()->isSecure() ? 'true' : 'false',
'env' => $this->env,
'mercure_hub_url' => $this->mercurePublicUrl,
'mercure_subscriber_jwt' => $this->mercureSubscriberJwt,
]);
}

View File

@@ -0,0 +1,98 @@
<?php declare(strict_types=1);
/**
* This file is part of the SplendidBear Websites' projects.
*
* Copyright (c) 2019 @ www.splendidbear.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller;
use App\Util\RpcManager;
use App\Util\TopicManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Class MercureController
*
* Handles HTTP API endpoints that replace the former WebSocket RPC and Topic handlers.
* Client → Server communication is via HTTP POST/GET.
* Server → Client broadcasting is via Mercure (SSE).
*
* @package App\Controller
* @author Lang <https://www.splendidbear.org>
* @category Class
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
* @link www.splendidbear.org
* @since 2026. 04. 09.
*/
class MercureController extends AbstractController
{
public function __construct(
private readonly TopicManager $topicManager,
private readonly RpcManager $rpcManager,
) {
}
/** POST /api/game/start — save the grid and create the PlayedGame record */
public function start(Request $request): JsonResponse
{
$data = $request->toArray();
$result = $this->rpcManager->saveGrid([$data['grid'], $data['gameAssoc']]);
return $this->json(['success' => $result]);
}
/** GET /api/game/connect/{gameAssoc} — return grid + current user info (base64 JSON) */
public function connect(string $gameAssoc): Response
{
$payload = $this->rpcManager->getConnectInformation($gameAssoc);
return new Response($payload, Response::HTTP_OK, ['Content-Type' => 'text/plain']);
}
/** POST /api/game/join/{gameAssoc} — register the player, broadcast subscription event via Mercure */
public function join(string $gameAssoc, Request $request): JsonResponse
{
$this->topicManager->subscribe($gameAssoc, $this->resolveUserName($request), $this->getUser());
return $this->json(['success' => true]);
}
/** POST /api/game/step/{gameAssoc} — persist the step and broadcast game event via Mercure */
public function step(string $gameAssoc, Request $request): JsonResponse
{
$this->topicManager->publish($gameAssoc, $this->resolveUserName($request), $request->toArray());
return $this->json(['success' => true]);
}
/** POST /api/game/leave/{gameAssoc} — broadcast disconnect event via Mercure */
public function leave(string $gameAssoc, Request $request): JsonResponse
{
$this->topicManager->unSubscribe($gameAssoc, $this->resolveUserName($request));
return $this->json(['success' => true]);
}
private function resolveUserName(Request $request): string
{
$user = $this->getUser();
if (null !== $user) {
return $user->getUserIdentifier();
}
$sessionId = $request->getSession()->getId();
if (empty($sessionId)) {
$sessionId = bin2hex(random_bytes(16));
}
return 'anon_' . $sessionId;
}
}