disconnect事件不会被触发

disconnect事件不会被触发

本文介绍了socket.io:disconnect事件不会被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您可以从。



发生什么事情是断开连接事件(即使在浏览器关闭后)在服务器上也不会被触发。



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


解决方案

将您的断开代码放在连接块上,编辑它有点像这样:

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

这样你就可以检测一个特定的套接字(特别是你传递给你的匿名函数的套接字连接运行)断开连接。


I have made a simple realtime visitor counter.

You can download it from this repository.

What happens is that disconnect event (even after browser closing) on server is never fired.

server.js is:

(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 on the client is:

    script(type='text/javascript')

        var socket = io.connect();

        socket.on('count', function (data) {
            $('#count').html( data.number );
        });
解决方案

Put your on disconnect code inside your on connect block and edit it a bit like so:

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

This way you're detecting when a specific socket (specifically the socket you pass to your anonymous function that is run on connection) is disconnected.

这篇关于socket.io:disconnect事件不会被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:34