Вопрос по node.js, events – socket.io: событие отключения не сработало

30

Я сделал простой счетчик посетителей в реальном времени.

Error: User Rate Limit ExceededError: User Rate Limit Exceeded.

Что происходит, так это то, что событие отключения (даже после закрытия браузера) на сервере никогда не запускается.

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   ответа
48

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

Error: User Rate Limit ExceededonceError: User Rate Limit Exceededon
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Giovanni Bitliner
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

next()

// 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");
    });
});
3

From Socket.IO 1.0Error: User Rate Limit Exceededio.engine.clientsCountError: User Rate Limit Exceeded

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

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

Error: User Rate Limit Exceeded.onceError: User Rate Limit Exceeded.onError: User Rate Limit ExceededsocketError: User Rate Limit Exceeded

Похожие вопросы