推送器在提交一次时发送了两次消息。

huangapple go评论128阅读模式
英文:

pusher sends message twice when submitted once

问题

pusher在提交时发送了两次消息。我没有看到任何重复调用的函数,但这仍然发生了。

我使用的是React v18.1.0
我使用的是Golang 1.18

以下是我的React前端代码:

  1. import React, { useState, useEffect } from "react";
  2. import Pusher from "pusher-js";
  3. function App() {
  4. const [username, setUsername] = useState('username');
  5. const [messages, setMessages] = useState([]);
  6. const [message, setMessage] = useState('');
  7. let allMessages = [];
  8. useEffect(() => {
  9. Pusher.logToConsole = true;
  10. const pusher = new Pusher('-', {
  11. cluster: '-'
  12. });
  13. const channel = pusher.subscribe('chat');
  14. channel.bind('message', function (data) {
  15. allMessages.push(data);
  16. setMessages(allMessages);
  17. });
  18. }, []);
  19. const submit = async e => {
  20. e.preventDefault();
  21. await fetch('http://localhost:8000/api/messages', {
  22. method: 'POST',
  23. headers: {'Content-Type': 'application/json'},
  24. body: JSON.stringify({
  25. username,
  26. message
  27. })
  28. });
  29. setMessage('');
  30. }
  31. return (
  32. <div className="container">
  33. <div className="d-flex flex-column align-items-stretch flex-shrink-0 bg-white">
  34. <div
  35. className="d-flex align-items-center flex-shrink-0 p-3 link-dark text-decoration-none border-bottom">
  36. <input className="fs-5 fw-semibold" value={username}
  37. onChange={e => setUsername(e.target.value)}/>
  38. </div>
  39. <div className="list-group list-group-flush border-bottom scrollarea">
  40. {messages.map(message => {
  41. return (
  42. <div className="list-group-item list-group-item-action py-3 lh-tight">
  43. <div className="d-flex w-100 align-items-center justify-content-between">
  44. <strong className="mb-1">{message.username}</strong>
  45. </div>
  46. <div className="col-10 mb-1 small">{message.message}</div>
  47. </div>
  48. )
  49. })}
  50. </div>
  51. </div>
  52. <form onSubmit={e => submit(e)}>
  53. <input className="form-control" placeholder="Write a message" value={message}
  54. onChange={e => setMessage(e.target.value)}
  55. />
  56. </form>
  57. </div>
  58. );
  59. }
  60. export default App;

这是我的Golang后端代码:

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "github.com/gofiber/fiber/v2/middleware/cors"
  5. "github.com/pusher/pusher-http-go"
  6. )
  7. func main() {
  8. app := fiber.New()
  9. app.Use(cors.New())
  10. pusherClient := pusher.Client{
  11. AppID: "1421095",
  12. Key: "-",
  13. Secret: "-",
  14. Cluster: "ap2",
  15. Secure: true,
  16. }
  17. app.Post("/api/messages", func(c *fiber.Ctx) error {
  18. var data map[string]string
  19. if err := c.BodyParser(&data); err != nil {
  20. return err
  21. }
  22. pusherClient.Trigger("chat", "message", data)
  23. return c.JSON([]string{})
  24. })
  25. app.Listen(":8000")
  26. }

我在网上搜索了很多,但是没有找到解决方案。以上是我编写的所有代码。

英文:

pusher is sending message two times on submit. i dont see any function being repeated or called two times but this still happens

i am using React v18.1.0
i am using Golang 1.18

  1. import React, { useState, useEffect } from &quot;react&quot;;
  2. import Pusher from &quot;pusher-js&quot;;
  3. function App() {
  4. const [username, setUsername] = useState(&#39;username&#39;);
  5. const [messages, setMessages] = useState([]);
  6. const [message, setMessage] = useState(&#39;&#39;);
  7. let allMessages = [];
  8. useEffect(() =&gt; {
  9. Pusher.logToConsole = true;
  10. const pusher = new Pusher(&#39;-&#39;, {
  11. cluster: &#39;-&#39;
  12. });
  13. const channel = pusher.subscribe(&#39;chat&#39;);
  14. channel.bind(&#39;message&#39;, function (data) {
  15. allMessages.push(data);
  16. setMessages(allMessages);
  17. });
  18. }, []);
  19. const submit = async e =&gt; {
  20. e.preventDefault();
  21. await fetch(&#39;http://localhost:8000/api/messages&#39;, {
  22. method: &#39;POST&#39;,
  23. headers: {&#39;Content-Type&#39;: &#39;application/json&#39;},
  24. body: JSON.stringify({
  25. username,
  26. message
  27. })
  28. });
  29. setMessage(&#39;&#39;);
  30. }
  31. return (
  32. &lt;div className=&quot;container&quot;&gt;
  33. &lt;div className=&quot;d-flex flex-column align-items-stretch flex-shrink-0 bg-white&quot;&gt;
  34. &lt;div
  35. className=&quot;d-flex align-items-center flex-shrink-0 p-3 link-dark text-decoration-none border-bottom&quot;&gt;
  36. &lt;input className=&quot;fs-5 fw-semibold&quot; value={username}
  37. onChange={e =&gt; setUsername(e.target.value)}/&gt;
  38. &lt;/div&gt;
  39. &lt;div className=&quot;list-group list-group-flush border-bottom scrollarea&quot;&gt;
  40. {messages.map(message =&gt; {
  41. return (
  42. &lt;div className=&quot;list-group-item list-group-item-action py-3 lh-tight&quot;&gt;
  43. &lt;div className=&quot;d-flex w-100 align-items-center justify-content-between&quot;&gt;
  44. &lt;strong className=&quot;mb-1&quot;&gt;{message.username}&lt;/strong&gt;
  45. &lt;/div&gt;
  46. &lt;div className=&quot;col-10 mb-1 small&quot;&gt;{message.message}&lt;/div&gt;
  47. &lt;/div&gt;
  48. )
  49. })}
  50. &lt;/div&gt;
  51. &lt;/div&gt;
  52. &lt;form onSubmit={e =&gt; submit(e)}&gt;
  53. &lt;input className=&quot;form-control&quot; placeholder=&quot;Write a message&quot; value={message}
  54. onChange={e =&gt; setMessage(e.target.value)}
  55. /&gt;
  56. &lt;/form&gt;
  57. &lt;/div&gt;
  58. );
  59. }
  60. export default App;

this is my backend code in Golang

  1. package main
  2. import (
  3. &quot;github.com/gofiber/fiber/v2&quot;
  4. &quot;github.com/gofiber/fiber/v2/middleware/cors&quot;
  5. &quot;github.com/pusher/pusher-http-go&quot;
  6. )
  7. func main() {
  8. app := fiber.New()
  9. app.Use(cors.New())
  10. pusherClient := pusher.Client{
  11. AppID: &quot;1421095&quot;,
  12. Key: &quot;-&quot;,
  13. Secret: &quot;-&quot;,
  14. Cluster: &quot;ap2&quot;,
  15. Secure: true,
  16. }
  17. app.Post(&quot;/api/messages&quot;, func(c *fiber.Ctx) error {
  18. var data map[string]string
  19. if err := c.BodyParser(&amp;data); err != nil {
  20. return err
  21. }
  22. pusherClient.Trigger(&quot;chat&quot;, &quot;message&quot;, data)
  23. return c.JSON([]string{})
  24. })
  25. app.Listen(&quot;:8000&quot;)
  26. }

ive searched alot online but i can really find a solution.
above is all the code ive written to create the app

答案1

得分: 0

好的,以下是翻译好的内容:

好的,我知道了。我在index.js中将我的App.js包裹在React Strict Mode中。这导致我的页面渲染了两次,因此useEffect函数运行了两次。所以如果有人遇到这个问题,请确保不要使用React Strict Mode。

英文:

OK I got it. I had my App.js wrapped in React Strict Mode in index.js. It was causing my Page to render 2 times this the useEffect function was running two times. So if anyone is having this issue, make sure you are not using React Strict Mode

huangapple
  • 本文由 发表于 2022年6月9日 22:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72562168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定