英文:
socket.emit() that I use inside the function is not working
问题
在我使用React和Node.js开发的应用程序中,函数中的socket发射功能由于某种原因不起作用,当我们按下按钮时,需要将数据保存在后端并实时发送这些数据给对方,简而言之,会有一个聊天部分。
这里您可以看到我在React中编写的代码
const sendMessage = async () => {
if (message.trim() !== "") {
try {
const { data } = await axios.post(
"http://localhost:5000/api/messages/",
{
content: message,
chatId: chatId,
},
config
);
setMessage("");
if (data) {
const newData = {
...data,
createdAt: new Date(),
};
socket.emit("new message", { newMessage: newData, room: chatId });
setResponse((prev) => {
return {
...prev,
messages: [...prev.messages, newData],
};
});
}
} catch (err) {
console.log(err);
}
}
};
实际上,这段代码几小时前还能正常工作,但现在由于某种我找不到的原因停止工作了。
这里您可以看到我已连接的socket
useEffect(() => {
socket = io("http://localhost:5000/");
socket.on("connected", () => {
return;
});
socket.emit("join chat", chatId);
}, []);
我面临以下情况,我在useEffect中执行了一个测试发射操作,它有效,但我无法使其在这个函数中工作。
这里您可以看到后端部分
const io = new Server(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
socket.on("join chat", (room) => {
socket.join(room);
});
socket.on("new message", ({ newMessage, room }) => {
socket.to(room).emit("message received", newMessage);
});
server.listen(5000, () => {
console.log("working on 5000");
});
});
如果您能帮助我解决这个问题,我将不胜感激。提前谢谢!
英文:
In my application that I developed with React and node.js, the socket emit in the function is not working for some reason, when we press the button, we need to save the data in the backend and send this data to the other party in real time, in short, there will be a chat part.
Here you can see the code I wrote in React
const sendMessage = async () => {
if (message.trim() !== "") {
try {
const { data } = await axios.post(
"http://localhost:5000/api/messages/",
{
content: message,
chatId: chatId,
},
config
);
setMessage("");
if (data) {
const newData = {
...data,
createdAt: new Date(),
};
socket.emit("new message", { newMessage: newData, room: chatId });
setResponse((prev) => {
return {
...prev,
messages: [...prev.messages, newData],
};
});
}
} catch (err) {
console.log(err);
}
}
};
Actually, this code was working a few hours ago, but now it stopped working for some reason I can't find.
and here you can see that I've connected the socket
useEffect(() => {
socket = io("http://localhost:5000/");
socket.on("connected", () => {
return;
});
socket.emit("join chat", chatId);
}, []);
I'm in the following situation, I performed a test emit operation in useEffect and it worked, but I can't get it to work in this function
Here you can see the back end part
const io = new Server(server, {
cors: {
origin: " http://localhost:3000",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
socket.on("join chat", (room) => {
socket.join(room);
});
socket.on("new message", ({ newMessage, room }) => {
socket.to(room).emit("message recieved", newMessage);
});
server.listen(5000, () => {
console.log("working on 5000");
});
});
I would be very grateful if you could help me solve this problem. Thank you in advance
答案1
得分: 0
我最终解决了这个问题,这是由于我的粗心导致的错误。当我发送数据时,它是以base64格式发送的,但由于超出了限制,发射过程未能进行。我只是在服务器端增加了大小限制。
英文:
I finally solved the problem, it was an error due to my carelessness, I was sending a data in base64 format when I sent it and the emit process did not take place because it was overrun, I only increased the size limit on the server side.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论