In your server code change
socket.to("room1").broadcast.emit("user-connected", data);
to
socket.to("room1").emit("user-connected", data);
And in your client code you don't need extra socket.on("room1")
. So your final client code will be like this:
useEffect(() => {
const socket = io("http://localhost:3000");
socket.emit("new-user", "hello world");
socket.on("user-connected", (data) => {
console.log(data);
});
return () => {
socket.disconnect();
};
}, []);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…