我是学生和初学者,如果我听不懂,请提前道歉。我尝试将Symfony 5与mercure一起使用,但我在向集线器发送更新时遇到一些困难。我将文档检查到Symfony Mercure doc,并认为这是一个很好的部分,但不是全部。
我已经在我的Windows10上安装了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));
}我希望在有人访问recap时发送通知: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的app dev的网络中检查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但是当我尝试访问url http://127.0.0.1:8000/commande/recapitulatif时,我遇到了一个错误(发送更新失败)和(“https://127.0.0.1:8000/.well-known/mercure".”的SSL连接错误)。
更新:我改变了
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.1401未授权返回“http://127.0.0.1:8000/.well-known/mercure".”)
也许我需要把我的应用程序放到https上,以便与mercure建立ssl连接?
另一种尝试。在.env和js文件中,我将https更改为http,并替换了http。之后,我修改了控制器中的代码,如下所示:
$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"如果有人能帮助我了解我的问题所在,非常感谢。
发布于 2021-08-10 19:54:11
我假设您已经运行并安装了单独的Mercure二进制文件。您不能同时在端口8000上运行Mercure二进制文件和Symfony。根据您的版本(我正在运行Mercure v0.9.0,因为我遵循了一个教程),您也许能够创建一个像这样的Makefile
mercure_server:
JWT_KEY=my_key ADDR=localhost:3000 ALLOW_ANONYMOUS=1 CORS_ALLOWED_ORIGINS=http://localhost:8000 ./bin/mercure然后运行make mercure_server
这里假设您的项目的bin目录中有mercure二进制文件。此命令不适用于最新版本的Mercure,因为它不是环境变量的设置方式。
新版本的Mercure二进制文件运行的是经过修改的Caddy服务器版本,从我目前所读到的内容来看,它与Symfony的兼容性并没有得到很好的说明。如果你手头拮据,我会考虑降级到v0.9.0,因为周围有更多可用的资源。
https://stackoverflow.com/questions/68143814
复制相似问题