我是学生和初学者,如果我不明白,请提前道歉。我尝试将 Symfony 5 与 mercure 一起使用,但我很难将更新发送到集线器。我检查了 Symfony Mercure 文档的文档,并考虑对此做出很好的贡献,但不是全部。
我已经在我的 Windows 10 上安装了 docker,我直接通过 Symfony cli 安装 Mercure,就像在文档上写的一样。我的参数是cli安装的默认参数。.env
###> symfony/mercure-bundle ###
# See https://symfony.com/doc/current/mercure.html#configuration
# The URL of the Mercure hub, used by the app to publish updates (can be a local URL)
MERCURE_URL=https://127.0.0.1:8000/.well-known/mercure
# The public URL of the Mercure hub, used by the browser to connect
MERCURE_PUBLIC_URL=https://127.0.0.1:8000/.well-known/mercure
# The secret used to sign the JWTs
MERCURE_JWT_SECRET="secret"
###< symfony/mercure-bundle ###
我使用真正的 JWT 机密只是为了示例 ([JWT.io]) 2。
{
"mercure": {
"publish": [
"*"
]
}
}
用我的秘密密码。
我用 eventSource 创建了一个 js 文件。
const eventSource = new EventSource('http://127.0.0.1:8000/.well-known/mercure?topic=' + encodeURIComponent('http://127.0.0.1:8000/commande/recapitulatif'),
);
eventSource.onmessage = event => {
// Will be called every time an update is published by the server
alert("Commande");
console.log(JSON.parse(event.data));
}
我希望在有人访问重述订单时发送通知:http: //127.0.0.1 :8000/commande/recapitulatif 。在我的控制器中,当地址http://127.0.0.1:8000/commande/recapitulatif被调用时,我添加:
$update = new Update(
'http://127.0.0.1:8000/commande/recapitulatif',
json_encode(['status' => 'Commande'])
);
$hub->publish($update);
在我需要通知并添加 js 代码的页面上,如果我在 chrome 的应用程序开发人员的网络中检查与 Mercure 的连接,我可以看到:
Request URL: http://127.0.0.1:8000/.well-known/mercure?topic=http%3A%2F%2F127.0.0.1%3A8000%2Fcommande%2Frecapitulatif
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:8000
Referrer Policy: strict-origin-when-cross-origin
但是当我尝试访问网址http://127.0.0.1:8000/commande/recapitulatif时出现错误(无法发送更新)和(“https://127.0.0.1:8000/ 的 SSL 连接错误” .well-known/mercure”。)
更新:我改变
MERCURE_URL=https://127.0.0.1:8000/.well-known/mercure
MERCURE_PUBLIC_URL=https://127.0.0.1:8000/.well-known/mercure
至
MERCURE_URL=http://127.0.0.1:8000/.well-known/mercure
MERCURE_PUBLIC_URL=http://127.0.0.1:8000/.well-known/mercure
现在出现错误(未能发送更新)我有(HTTP/1.1 401 Unauthorized 返回“http://127.0.0.1:8000/.well-known/mercure”。)
可能我需要将我的应用程序放到 https 以便与 mercure 建立 ssl 连接?
另一个尝试。我替换 hhttp 我将 https 更改为 http 到我的 .env 和 js 文件。之后,我像这样更改控制器中的代码:
$discovery->addLink($request);
$response = new JsonResponse([
'@id' => 'http://127.0.0.1:8000/commande/recapitulatif',
'status' => 'Order',
]);
$response->headers->setCookie(
$authorization->createCookie($request, ['http://127.0.0.1:8000/commande/recapitulatif'])
);
现在我没有错误,但没有通知我的浏览器。
我想要通知的页面的日志:
[Application] Jun 26 17:33:45 |DEBUG | SECURI Stored the security token in the session. key="_security_main"
[Web Server ] Jun 26 19:33:45 |INFO | SERVER GET (200) /favicon.ico ip="127.0.0.1"
[Application] Jun 26 17:33:46 |INFO | REQUES Matched route "_wdt". method="GET" request_uri="http://127.0.0.1:8000/_wdt/8f2711" route="_wdt" route_parameters={"_controller":"web_profiler.controller.profiler::toolbarAction","_route
":"_wdt","token":"8f2711"}
[Web Server ] Jun 26 19:33:47 |INFO | SERVER GET (200) /_wdt/8f2711 ip="127.0.0.1"
以及我需要发送通知的日志:
[Application] Jun 26 17:36:06 |DEBUG | SECURI Stored the security token in the session. key="_security_main"
[Web Server ] Jun 26 19:36:06 |INFO | SERVER POST (200) /commande/recapitulatif host="127.0.0.1:8004" ip="127.0.0.1" scheme="https"
[Application] Jun 26 17:36:07 |INFO | REQUES Matched route "_wdt". method="GET" request_uri="http://127.0.0.1:8000/_wdt/d13a0f" route="_wdt" route_parameters={"_controller":"web_profiler.controller.profiler::toolbarAction","_route
":"_wdt","token":"d13a0f"}
[Web Server ] Jun 26 19:36:08 |INFO | SERVER GET (200) /_wdt/d13a0f ip="127.0.0.1"
但如果我像这样添加更新:
$update = new Update(
'http://127.0.0.1:8000/commande/recapitulatif',
json_encode(['status' => 'Commande']), true
);
$hub->publish($update);
我的日志是这样的:
[Application] Jun 26 18:02:39 |INFO | HTTP_C Request: "POST http://127.0.0.1:8000/.well-known/mercure"
[Application] Jun 26 18:02:39 |INFO | HTTP_C Response: "401 http://127.0.0.1:8000/.well-known/mercure"
[Application] Jun 26 18:02:39 |CRITICA| REQUES Uncaught PHP Exception Symfony\Component\Mercure\Exception\RuntimeException: "Failed to send an update." at C:\laragon\www\Projet\Restaurant\RelaisDesVoutes\vendor\symfony\mercure\src\H
ub.php line 104
[Web Server ] Jun 26 20:02:39 |INFO | MERCUR Topic selectors not matched, not provided or authorization error
[Web Server ] Jun 26 20:02:39 |WARN | SERVER POST (401) /.well-known/mercure host="127.0.0.1:8004" ip="127.0.0.1" scheme="https"
[Application] Jun 26 18:02:39 |DEBUG | SECURI Stored the security token in the session. key="_security_main"
[Web Server ] Jun 26 20:02:39 |ERROR | SERVER POST (500) /commande/recapitulatif host="127.0.0.1:8004" ip="127.0.0.1" scheme="https"
如果有人可以帮助我了解我的问题出在哪里,非常感谢。