socket.emit并非所有客户端触发
我是与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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我发现了,问题不是在客户端,而是在服务器端。.因此,我从 socket.io编辑了
。
标题 到
socket.emit并未触发所有客户端。
我试图仅将事件发射到触发事件的特定插座上。
socket.emit< - 仅由 - &gt触发;连接的插座。
Soluton:使用io.emit发送给所有客户
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