node.js - How to handle asynchronous in Javascript? -


asynchronous in socket.io

i use 2 variables online (to count number of online people currently) , total variable (count total number of people visited). problem when reload page continuously online variable has not decreased, has increased.

io.on('connection', function(sock) {      sock.on('disconnect', function() {         sock.broadcast.emit('client-disconnect', --online);     })     io.sockets.emit('client-connection',{ onl: ++online, tol: ++total }); } 

the connections being left open not closed when page either refreshed or closed. listen page events able close connection before page removed:

window.onbeforeunload = function(e) { sock.disconnect(); };


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -