使用htmx的websockets扩展响应的HTML元素被添加了连字符,标记被注解掉

lf3rwulv  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(302)

我用的是htmx websockets extension
我的页面上有一个球员名单。
我想通过WebSocket从我的后端发送一条消息,将一个玩家添加到列表中。
我发送消息<li id="player-list" hx-swap-oob="beforeend" type="1">player 2</ li>
添加到HTML的内容是:

  1. "player 2"
  2. <!-- li-->

字符串
这就好像HTML li元素被清理了,<li>...</li>被改为"..."<!-- li-->
我的HTML页面:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://unpkg.com/[email protected]"></script>
  5. <script src="https://unpkg.com/htmx.org/dist/ext/ws.js"></script>
  6. <title>Hat Game: Your Room Code</title>
  7. <link rel="stylesheet" href="/stylesheets/style.css" />
  8. </head>
  9. <body>
  10. <body>
  11. <h1>Your room code</h1>
  12. <p>
  13. Give this code to everyone in your group:
  14. <span id="room-code">RSTWAG</span>.
  15. </p>
  16. <p>
  17. As everyone enters the room code, you will see their names populate below.
  18. When your entire group is there, tap <b><em>Everybody's in</em></b
  19. >.
  20. </p>
  21. <div id="players-in-the-room">
  22. <h2>Players in the room</h2>
  23. <ol id="player-list" hx-ext="ws" ws-connect="/ws-roster/?roomcode=RSTWAG">
  24. <li type="1">fur</li>
  25. </ol>
  26. </div>
  27. <div id="ws-messages"></div>
  28. </body>
  29. </body>
  30. </html>


出于测试目的,我通过WebSocket发送消息:

  1. aws --profile hat-game-prod apigatewaymanagementapi post-to-connection \
  2. --data $(echo -n '<li id="player-list" hx-swap-oob="beforeend" type="1">player 2</ li>' | base64) \
  3. --connection-id ${CONNECTION_ID} \
  4. --endpoint-url https://qu3vq8riw2.execute-api.us-east-1.amazonaws.com/ws-roster


文本player 2被添加到正确的位置,但不是在HTML标签中:


的数据
根据我在开发工具中看到的消息,似乎消息是按预期接收的:



我希望我的<ol>元素看起来像这样:

  1. <ol id="player-list" hx-ext="ws" ws-connect="/ws-roster/?roomcode=RSTWAG">
  2. <li type="1">fur</li>
  3. <li type="1">player 2</li>
  4. </ol>


而不是

  1. <ol id="player-list" hx-ext="ws" ws-connect="/ws-roster/?roomcode=RSTWAG">
  2. <li type="1">fur</li>
  3. "player 2"
  4. <!-- li-->
  5. </ol>


在我通过WebSocket发送li元素之后。
我在Chrome和Firefox上都试过,结果都一样。
repo是在https://github.com/douglasnaphas/hatgame,虽然我相信相关的代码包括在上面。

csbfibhn

csbfibhn1#

htmx Discord上的用户rtrjl解释说,正确的方法是让我的WebSocket消息中的顶级元素是ol元素,而不是li元素。
下面的工作,正如我在Discord上的cross-posted question上所建议的那样:

  1. aws --profile hat-game-prod apigatewaymanagementapi post-to-connection \
  2. --data $(echo -n '<ol id="player-list" hx-swap-oob="beforeend"> <li> type="1">player 2 </li> </ol>' | base64) \
  3. --connection-id ${CONNECTION_ID} \
  4. --endpoint-url https://qu3vq8riw2.execute-api.us-east-1.amazonaws.com/ws-roster

字符串

相关问题