socket.emit并非所有客户端触发

发布于 2025-01-24 11:40:18 字数 1681 浏览 2 评论 0原文

我是与socket.io做出反应的新手,并且我被困在前端的检索数据上。我要做的是创建一个小游戏,而第一步是拥有一个可以加入多个用户的大厅页面。


这是我遇到问题的部分,因为,如果示例,如果我使用用户测试

大厅创建游戏,则仅使用用户测试

TAB1:

| ---测试--- |

但是,如果我打开一个新选项卡并加入使用用户test1的当前选项卡,我的新选项卡是

TAB2:

| ---测试测试1 --- |

我要进行的问题是,第一个选项卡只有第一个用户加入了游戏

TAB1:

| ---测试--- |

此事件没有触发此选项卡。下一个标签也发生了相同的方案,依此类推。

node.js代码


socket.on("getUsers", async (data) => {
  if (!data.gameCode) {
    universalError("GameCode doesn't exist");
    return;
  }

  const existentGame = await pubClient.get(data.gameCode);

  if (!existentGame) {
    universalError("The Current Game Does Not Exist");
    return;
  }

  currentGame = JSON.parse(existentGame);
  socket.emit("gameUsers", {
    gameUsers: currentGame,
  });
});

反应代码

const equals = (a, b) => JSON.stringify(a) === JSON.stringify(b); 

function() {
  const socket = useContext(SocketContext);
  const [host, setHost] = useState("");
  const [users, setUsers] = useState([]);

  useEffect(() => {
    if (!currentGameCode) return;

    socket.emit("getUsers", { gameCode: currentGameCode });
  }, []);

  socket.on("gameUsers", (data) => {
    setHost(data.gameUsers.host);

    console.log(users, data.gameUsers.users);

    if (!equals(users, data.gameUsers.users)) {
      setUsers(data.gameUsers.users);
    }
  });

  return(
  ...
  )
}

我尝试将socket.on放在使用效果函数之前,还将套接字插入具有多个依赖关系的使用效率内部,但我对此没有成功。从我看到的内容来看,用户在socket.on(“ gameusers”)中仅每个选项卡中输入一次,这就是他加入游戏的时候。.每次新用户都会加入游戏,但我的套接字.on在这种情况下不会触发。关于我做错什么的想法吗?预期的谢谢!

I am new to react with socket.io, and I am stuck on the retrieving data permanently on frontend. What I'm trying to do is to create a little game and first step is to have a lobby page where multiple users can join.


Here is the part where I have problems because, if example, if I create a game with user Test

Lobby is now just with user Test

Tab1:

| --- Test --- |

But if I open a new tab and join to the current tab with the user Test1, my new tab is

Tab2:

| --- Test Test1 --- |

The problem that I ecounter is that the first tab only have the first user joined in the game

Tab1:

| --- Test --- |

The event didn't trigger for this tab. The same scenario happends for the next tabs and so on.

Node.js code


socket.on("getUsers", async (data) => {
  if (!data.gameCode) {
    universalError("GameCode doesn't exist");
    return;
  }

  const existentGame = await pubClient.get(data.gameCode);

  if (!existentGame) {
    universalError("The Current Game Does Not Exist");
    return;
  }

  currentGame = JSON.parse(existentGame);
  socket.emit("gameUsers", {
    gameUsers: currentGame,
  });
});

React code

const equals = (a, b) => JSON.stringify(a) === JSON.stringify(b); 

function() {
  const socket = useContext(SocketContext);
  const [host, setHost] = useState("");
  const [users, setUsers] = useState([]);

  useEffect(() => {
    if (!currentGameCode) return;

    socket.emit("getUsers", { gameCode: currentGameCode });
  }, []);

  socket.on("gameUsers", (data) => {
    setHost(data.gameUsers.host);

    console.log(users, data.gameUsers.users);

    if (!equals(users, data.gameUsers.users)) {
      setUsers(data.gameUsers.users);
    }
  });

  return(
  ...
  )
}

I tried to put the socket.on before useEffect function, also insert the socket.on inside a useEffect with multiple dependency but I had no success with that. From what I saw, the user enter in socket.on("gameUsers") just once per tab and that is when he join the game.. The call on the backend is made everytime a new user join to the game, but my socket.on doesn't trigger in that case. Any ideas of what I'm doing wrong? Anticipated thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神回复 2025-01-31 11:40:18

好的,我发现了,问题不是在客户端,而是在服务器端。.因此,我从 socket.io编辑了

标题 到

socket.emit并未触发所有客户端


我试图仅将事件发射到触发事件的特定插座上。

socket.emit< - 仅由 - &gt触发;连接的插座。

Soluton:使用io.emit发送给所有客户

 io.on("connection", (socket) => { ... });

Ok, I figured it out, the problem was not on the client side but on server side.. So I edited the title from

Socket.io doesn't trigger event for previous tabs - React

to

Socket.emit doesn't trigger for all clients.


I was trying to emit the event only to the particular socket which triggered the event.

socket.emit < - was triggered JUST by - > socket.on at which was connected to.

SOLUTON: Use io.emit to send to all client

 io.on("connection", (socket) => { ... });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文