Reputation: 5090
I am having a very strange issue with Socket.IO. For some reason, it appears that the emit "buffer" is being duplicated over and over, as every connection results in n+1 replies:
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
The code for this is fairly simple. On the client:
this.ping = function(){
socket.on('PING__',function (data){
console.log(count++,"CONNECTION STATUS: "+$(data)[0].status);
data = '';
});
socket.emit('__PING');
}
On the Server:
var counter = 0;
io.sockets.on('connection', function (socket) {
socket.on('__PING', function (data) {
socket.emit('PING__' , {status:'OK - '+counter++});
});
});
So, for some reason, when I attempt to ping, it is somehow reiterating. I'm not sure why. Any ideas?
Upvotes: 0
Views: 202
Reputation: 5090
Figured it out. Really stupid logic error:
this.ping = function(){
socket.on('PING__',function (data){
console.log(count++,"CONNECTION STATUS: "+$(data)[0].status);
data = '';
});
socket.emit('__PING');
}
Should have been:
var events = {};
this.ping = function(){
if(typeof events.ping === 'undefined'){
events.ping = socket.on('PING__',function (data){
console.log("CONNECTION STATUS: "+$(data)[0].status);
data = '';
});
}
socket.emit('__PING');
}
It was messing up because I was binding the event handler over and over lol.
Upvotes: 1