我对 Socket.IO 有一个非常奇怪的问题。出于某种原因,发射“缓冲区”似乎被一遍又一遍地复制,因为每个连接都会导致 n+1 个回复:
COM.ping();
undefined
0 "CONNECTION STATUS: OK - 0" app.js:9
COM.ping();
undefined
1 "CONNECTION STATUS: OK - 1" app.js:9
2 "CONNECTION STATUS: OK - 1" app.js:9
COM.ping();
undefined
3 "CONNECTION STATUS: OK - 2" app.js:9
4 "CONNECTION STATUS: OK - 2" app.js:9
5 "CONNECTION STATUS: OK - 2" app.js:9
COM.ping();
undefined
6 "CONNECTION STATUS: OK - 3" app.js:9
7 "CONNECTION STATUS: OK - 3" app.js:9
8 "CONNECTION STATUS: OK - 3" app.js:9
9 "CONNECTION STATUS: OK - 3" app.js:9
COM.ping();
undefined
10 "CONNECTION STATUS: OK - 4" app.js:9
11 "CONNECTION STATUS: OK - 4" app.js:9
12 "CONNECTION STATUS: OK - 4" app.js:9
13 "CONNECTION STATUS: OK - 4" app.js:9
14 "CONNECTION STATUS: OK - 4" app.js:9
代码相当简单。在客户端:
this.ping = function(){
socket.on('PING__',function (data){
console.log(count++,"CONNECTION STATUS: "+$(data)[0].status);
data = '';
});
socket.emit('__PING');
}
在服务器上:
var counter = 0;
io.sockets.on('connection', function (socket) {
socket.on('__PING', function (data) {
socket.emit('PING__' , {status:'OK - '+counter++});
});
});
因此,出于某种原因,当我尝试 ping 时,它以某种方式重复。我不确定为什么。有任何想法吗?