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 ;)
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.
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