I found a weird problem, with the beforeunload event.
Look at this code -
Client JavaScript:
var socket = io.connect('http://localhost:1337');
socket.on('connect', function() {
window.addEventListener("beforeunload", function () {
socket.emit('out');
});
});
Server node.js:
/* 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('out', function() {
console.log('disconnected');
});
});
If I go ahead and start the server and go to localhost:1337 and close the tab, the server not gonna
log disconnected.
I figured out that if I delete the line - io.set('transports', ['xhr-polling']);
The server will log disconnected.
Why the server doesn't log disconnected in xhr-polling?
Thanks to everybody who tries to help.