socket.io: 断开事件未触发

39

我制作了一个简单的实时访问者计数器。

你可以从此存储库下载它。

问题在于,即使是在浏览器关闭后,服务器上的断开连接事件也永远不会被触发。

server.js 是:

(function () {
var app, count, express, io;

express = require('express');
io = require('socket.io');

app = module.exports = express.createServer();

app.configure(function () {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(require('stylus').middleware({
        src: __dirname + '/public'
    }));
    app.use(app.router);
    return app.use(express.static(__dirname + '/public'));
});

app.configure('development', function () {
    return app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});
app.configure('production', function () {
    return app.use(express.errorHandler());
});

io = require('socket.io').listen(app);

count = 0;

io.sockets.on('connection', function (socket) {
    count++;
    io.sockets.emit('count', {
        number: count
    });
});

io.sockets.on('disconnect', function () {
    console.log('DISCONNESSO!!! ');
    count--;
    io.sockets.emit('count', {
        number: count
    });
});


app.get('/', function (req, res) {
    return res.render('index', {
        title: 'node.js express socket.io counter'
    });
});
if (!module.parent) {
    app.listen(10927);
    console.log("Express server listening on port %d", app.address().port);
}

}).call(this);

客户端上的脚本是:

    script(type='text/javascript')

        var socket = io.connect();

        socket.on('count', function (data) {
            $('#count').html( data.number );
        });
3个回答

56

将断开连接的代码放在连接代码块中,并对其进行适当编辑,如下所示:

io.sockets.on('connection', function (socket) {
    count++;
    io.sockets.emit('count', {
        number: count
    });

    socket.on('disconnect', function () {
        console.log('DISCONNESSO!!! ');
        count--;
        io.sockets.emit('count', {
            number: count
        });
    });
});

这种方法可以检测到特定套接字(具体是您在连接时传递给匿名函数的套接字)何时断开连接。


你有没有注意到我在上面写了socket.on? - swiecki
6
如果使用xhr-polling进行传输,断开连接可能会有相当长的延迟。 - Brett Pennings
XHR-Polling 也会影响 connect 事件吗? - Samarth Agarwal
正如@NoNameProvided所提到的,断开连接事件应该使用once而不是on - Pier-Luc Gendreau

3

万一其他人也犯了这样傻的错误:请确保您定义的任何 socket 中间件在结尾处调用 next(),否则不会运行其他 socket 处理程序。

// make sure to call next() at the end or...
io.use(function (socket, next) {
    console.log(socket.id, "connection middleware");
    next(); // don't forget this!
});

// ...none of the following will run:

io.use(function (socket, next) {
    console.log(socket.id, "second middleware");
    next(); // don't forget this either!
});

io.on("connection", function (socket) {
    console.log(socket.id, "connection event");
    socket.once("disconnect", function () {
        console.log(socket.id, "disconnected");
    });
});

这个 next() 真的让我花了好几个小时才找到。它可以在套接字断开连接后立即执行操作,这正是我所需要的。应该让它更加显眼。 - NME New Media Entertainment

3
自 Socket.IO 1.0 版本起,可以使用 io.engine.clientsCount 属性。该属性可以告诉您当前应用程序有多少个打开的连接。
io.sockets.on('connection', function (socket) {
    io.sockets.emit('count', {
        number: io.engine.clientsCount
    });

    socket.once('disconnect', function () {
        io.sockets.emit('count', {
            number: io.engine.clientsCount
        });
    });
});

注意:使用.once而不是.on,监听器将自动从socket中删除,这对我们很有好处,因为断开事件每个socket只会触发一次。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接