How can I close the socket connection on the client side?
I am using:
i.e.: call localhost/test
-- server side
var test = io .of('/test') .on('connection', function (socket) { console.log('open socket: ' + socket); socket.on('disconnect', function () { console.log('disconnected event'); //socket.manager.onClientDisconnect(socket.id); --> endless loop with this disconnect event on server side //socket.disconnect(); --> same here }); });
-- client side
var socket = io.connect('http://localhost:3000/test'); socket.on('disconnect', function () { console.log('disconnect client event....'); }); socket.emit('getInitData', function (data) { .. do something with data });
If I load the test-page I need some values from the server (getInitData).
On the first page visit I get the data once, on a reload or second visit I get it twice and so on.
The connection on the server side is beeing closed automatically on page reload and if you leave the page.
But on the client side the connection is still open.
How can I close the connection on the client side or check if there is already a open connection?
UPDATE
I tried now the following: (client side)
window.onbeforeunload = function(e) { socket.disconnect(); };
This triggers on the client side the disconnect event, but I still get the twice or tripple response.
You can check the socket. connected property: var socket = io. connect(); console.
All you have to do is updating the remote session store on node server when a new login/logout happens in your php server. Show activity on this post. The excellent passport framework for express uses secure cookies to validate identity. There is even a module to access it from socket.io.
Did you try:
socket.disconnect()
on client?
For socket.io version 1.4.5:
On server:
socket.on('end', function (){ socket.disconnect(0); });
On client:
var socket = io(); socket.emit('end');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With