react.js和websocket.io最终以无限循环而最终
我想为纸牌游戏写游戏大厅。 使用react.js,node.js和websocket.io来实现此目的。 到目前为止,一切都很好。球员在同一大厅连接。 但是我想在大厅STH中打印(玩家1:史蒂文,玩家2:弗兰克,...)。 我最终进入了一个无限的循环,从几个小时开始就试图解决。 所以也许有人可以帮助我。我猜它一直通过Usestate进行重新渲染,但我不知道如何防止它。
相关前端代码:
const Lobby = (props) => {
const socket = props.socket;
const player = {
room: props.room,
name: props.name,
};
const [playerList, setPlayerList] = useState([]);
socket.emit("joined_lobby", player);
console.log(`${playerList}`);
useEffect(() => {
socket.on("add_user", (data) => {
setPlayerList([...playerList, data.name]);
});
}, []);
相关服务器代码:
io.on("connection", (socket) => { console.log(`Player with ID:\[${socket.id}\] Connected`);
socket.on("join_room", (data) => {
socket.join(data);
console.log(`Player with ID:\[${socket.id}\] Joined the room ${data}`); });
socket.on("joined_lobby", (data) => {
socket.to(data.room).emit("add_user", data); });
socket.on("disconnect", () => {
console.log(`Player with ID:\[${socket.id}\] Disonnected`); }); });
i want to write a game Lobby for a card game.
Using React.js, Node.js and Websocket.io for achieving this.
As so far all went fine. Players are connected in the same Lobby.
But i want to print in the Lobby sth Like (Player 1: Steven, Player 2: Frank, ...).
I ended up in an infinite loop, i am trying to solve since hours.
So maybe someone can help me. It keeps re-rendering by useState i guess, but i don't know how to prevent it from.
Relevant Frontend Code:
const Lobby = (props) => {
const socket = props.socket;
const player = {
room: props.room,
name: props.name,
};
const [playerList, setPlayerList] = useState([]);
socket.emit("joined_lobby", player);
console.log(`${playerList}`);
useEffect(() => {
socket.on("add_user", (data) => {
setPlayerList([...playerList, data.name]);
});
}, []);
Relevant Server Code:
io.on("connection", (socket) => { console.log(`Player with ID:\[${socket.id}\] Connected`);
socket.on("join_room", (data) => {
socket.join(data);
console.log(`Player with ID:\[${socket.id}\] Joined the room ${data}`); });
socket.on("joined_lobby", (data) => {
socket.to(data.room).emit("add_user", data); });
socket.on("disconnect", () => {
console.log(`Player with ID:\[${socket.id}\] Disonnected`); }); });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想说我目前正在经历相同的挑战,寻找解决方案
i would like to say i am currently passing through quite the same challenge, looking out for the solution
听起来您只想运行
socket.emit(“ joined_lobby”,player);
每次'player'。如果是这样,请将其移至使用效果,然后将其运行,仅在房间或播放器名称更改时才能运行。
编辑:您可能还需要将props.socket放在deps中 - 我不知道该生命周期是如何工作的
Sounds like you only want to run
socket.emit("joined_lobby", player);
once per 'player'.If so, move it into a useEffect, and make it so it runs only if the room or player name changes.
EDIT: you may need to put props.socket in the deps too - I don't know how the lifecycle of that one works
我也遇到了与您相同的错误,在解决了2个小时的错误之后,我发现我打开了2个帐户以互相聊天,而我没有刷新的2个帐户,导致它是Infinity Connect
I also got the same error as you and after 2 hours of fixing the error I found out that I opened 2 accounts to chat with each other and one of those 2 I didn't refresh, causing it to be infinity connect