Hello,
Je suis débutant sur symfony 5
J'ai un souci avec l'utilisation de mercure sur projet Symfony,
j'ai installé mercure à la racine de mon projet, cependant quand je test l'envoi d'une requête à mercure, j'ai l'erreur
Couldn't connect to server for "http://localhost:3000/.well-known/mercure"
alors que l'url http://localhost:3000/.well-known/mercure est bien accessible en local.
Configuration .env.local :
1 2
| MERCURE_PUBLISH_URL=http://localhost:3000/.well-known/mercure
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.obDjwCgqtPuIvwBlTxUEmibbBf0zypKCNzNKP7Op2UM |
configuration .env :
1 2 3 4 5 6
| ###> symfony/mercure-bundle ###
# See https://symfony.com/doc/current/mercure.html#configuration
MERCURE_PUBLISH_URL=http://localhost:3000/.well-known/mercure
# The default token is signed with the secret key: !ChangeMe!
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.obDjwCgqtPuIvwBlTxUEmibbBf0zypKCNzNKP7Op2UM
###< symfony/mercure-bundle ### |
Création token JWT via JWT.io :
Commande pour lancer mercure :
./mercure --jwt-key='!ChangeMe!' --addr='localhost:3000' --allow-anonymous --cors-allowed-origins='*'
Mon controller :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?php
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mercure\PublisherInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class InterestController
* @package App\Controller
*/
class InterestController extends AbstractController
{
/**
* @Route("/profil/{user_id}/interets", name="user_interests", requirements={"user_id":"\d+"})
* @ParamConverter("user", options={"mapping": {"user_id" : "id"}})
* @ParamConverter("profil", options={"mapping": {"user_id" : "user_id"}})
*/
public function index()
{
return $this->render('interest/index.html.twig', [
'controller_name' => 'InterestController',
]);
}
/**
* @Route("/ping", name="ping", methods={"POST"})
* @param PublisherInterface $publisher
* @return Response
*/
public function __invoke(PublisherInterface $publisher): Response
{
$update = new Update(
'http://monsite.com/ping',
json_encode(['status' => 'OutOfStock'])
);
// The Publisher service is an invokable object
$publisher($update);
return new Response('published!');
}
} |
Test en front :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| {% extends 'base.html.twig' %}
{% block body %}
<form id="interest-form" action="{{ path('ping') }}" method="POST">
<a href="javascript:{}" onclick="document.getElementById('interest-form').submit();" class="send-interest">
</a>
</form>
{% endblock %}
{% block javascripts %}
<script>
// URL is a built-in JavaScript class to manipulate URLs
const eventSource = new EventSource('http://localhost:3000/.well-known/mercure?topic=' + encodeURIComponent('http://monsite.com/ping'));
eventSource.onmessage = event => {
// Will be called every time an update is published by the server
console.log(JSON.parse(event.data));
}
</script>
{% endblock %} |
Quand je test via l'outil POSTMAN aucun souci avec le Bearer Token :
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.obDjwCgqtPuIvwBlTxUEmibbBf0zypKCNzNKP7Op2UM
Malgrès mes efforts pour démêler la source du problème je suis dans l'impasse.. toujours cette erreur lorsque que je soumet mon formulaire de test #"interest-form
Couldn't connect to server for "http://localhost:3000/.well-known/mercure".
Coté serveur Mercure je récup :
127.0.0.1 - - [13/Jul/2020:17:24:32 -0400] "GET /.well-known/mercure?topic=http%3A%2F%2Fmonsite.com%2Fping HTTP/1.1" 200 2 "http://my_website.fr:81/profil/3" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/83.0.4103.116 Chrome/83.0.4103.116 Safari/537.36"
J'ai pourtant suivi la doc : https://symfony.com/doc/current/mercure.html
Partager