Reputation: 4714
In socket.io the disconnect event doesn't fire when the transport xhr-polling is active. If I switch the transport to websockets it works fine, but in xhr-polling its doesn't work.
/* Basics */
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(1337, null);
io.set('transports', ['xhr-polling']);
// routing
app.get('/', function (req, res) {
res.sendfile("index.html");
app.use(express.static(__dirname));
});
io.sockets.on('connection', function (socket)
socket.on('disconnect', function() {
console.log('LOL');
});
});
In the following code the disconnect doesn't fire, but if I delete the line -
io.set('transports', ['xhr-polling']);
It works perfectly, so why it doesn't work with xhr-polling? but only with websockets?
How can I fix this? Am I missing something?
Thanks in Advance ;)
Upvotes: 5
Views: 1589
Reputation: 96
I had the same problem, and then I used the option {'sync disconnect on unload' : true} in the client socket:
var socket = io.connect('http://yourServerDomain.com', {'sync disconnect on unload' : true});
it forces the the socket to send the disconnect event immediatly.
Upvotes: 1
Reputation: 9884
It's impossible to immediately detect disconnects using XHR-Polling since the client connects repeatedly for each message.
Socket.io gets around this by setting an inactivity timeout on xhr-polling sockets.
This is the relevant documentation: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
See the heartbeat interval
configuration option. The default is 25 seconds.
Websocket maintains a persistant tcp socket between the browser and the server so socket.io can usually detect immediately when the socket is disconnected.
Upvotes: 1