【发布时间】:2017-06-06 10:52:25
【问题描述】:
我阅读了有关此问题的其他问题,但没有找到该问题的解决方案。例如,我看到您可以 removeListeners 或将套接字代码移出单击处理程序,但它似乎不适合我的问题。
我正在使用套接字 + ajax。我的代码逻辑如下:
//client side
button.onclick = function () {Communicate(info);return false;};
function Communicate(info) {
// ajax call
}
// server side -> receive ajax
app.post("/...",function(req,res) {
// do stuff (...)
// send global socket call
io.sockets.emit("GlobalCall",xx);
// return ajax call to the user who triggered this
res.send(...));
});
// client side again
socket.on("GlobalCall",function(x)) {
// this is called twice sometimes for some reason... It should be called only once each time the server emits ...
}
正如我对代码的评论,服务器只发出一次,但“GlobalCall”有时会发生两次。 有什么帮助吗?谢谢
【问题讨论】:
-
有什么问题?错误是什么?
-
@robkuz 尽管我只在服务器端发出一次,但在客户端,socket.on("GlobalCall"...) 有时会被调用两次
-
你能不能在前端的socket.on之前添加一个console.log,检查这段代码运行了多少次,不是在你发出时而是在注册时运行
-
@AsifSaeed 好吧,它在全球范围内,所以没有什么关系。我发现了问题,它与套接字没有直接关系......
标签: javascript ajax node.js sockets