我的socket.io实现示例差不多完成了。 唯一缺少的是断开连接事件的功能。
我试过把它放在io连接功能里面,外面,一切都浮现在我的脑海里。 可悲的是,断开功能根本没有被触发。
继承我的路线文件代码,其中包含所述功能:
/* GET /backend */
router.get('/', function(req, res, next) {
if (req.session && req.session.user) { // Check if session exists
var currentUser = req.session.user[0].name; // save username according to session
if(!req.session.user[0].io){
req.app.get('io').on('connection', function(socket){ // Gets executed when user connects to /backend/
req.session.user[0].io = true; // Sets the io session to true
console.log("IO SESSION SET : "+ req.session.user[0].io +" for user " + currentUser);
});
if(req.session.user[0].io){
req.app.get('io').on('disconnect', function(){
req.session.user[0].io = false;
console.log("IO SESSION SET : "+ req.session.user[0].io +" for user " + currentUser);
req.app.get('io').emit('wentOffline', currentUser); // emit wentOffline to client js
});
}
req.app.get('io').emit('cameOnline', currentUser); // emit function cameOnline to client js
}
// res.render('backend', { title: 'Backend' });
// console.log(req.session.user[0].name);
/*
-------------------------
#########################
IF EVERYTHING FAILS - THE CODE BELOW IS THE NORMAL RENDER WITHOUT SOCKET IO
#########################
-------------------------
*/
// lookup the user in the DB by pulling their email from the session
User.getUser({ name: req.session.user.name }, function (err, user) {
if (!user) {
console.log("No User found");
// if the user isn't found in the DB, reset the session info and
// redirect the user to the login page
// console.log(user);
req.session.reset();
res.redirect('/login');
} else {
// expose the user to the template
res.locals.user = user;
// render the dashboard page
function passOnlineUsers(callback){ // Looks for All users who are online
User.getOnlineUsers({}, function(err, res, fields){
// console.log(res);
if (err) throw err;
callback(err, res);
});
}
passOnlineUsers(function(err, result){
res.render('backend', { title: 'Backend', viewer: req.session.user[0], online: result });
// res.render('backend', { title: 'Backend', viewer: "", online: result });
});
}
});
} else {
res.redirect('/login');
}
});
如果这里需要它也是带有io代码的加载客户端文件:
<script>
var socket = io();
// socket.emit('cameOnline', "new User");
socket.on('cameOnline', function(name){
console.log(name + " connected.");
var $li = $('<li/>');
$li.addClass(name);
if($('#onlineUsers .'+name).length == 0){
console.log(name + " is about to be added");
$li.text(" "+ name);
$('#onlineUsers').append($li);
}else{
console.log("no new user");
}
// $('#onlineUsers').append($("<span>").text(" " + name)).addClass(name);
});
socket.on('wentOffline', function(name){
console.log(name +" is about to go offline");
var toDel = $('#onlineUsers li.'+name);
toDel.remove();
});
</script>
断开连接功能以外的所有功能都可正常工作。 当用户上线时,cameOnline函数会被触发,并将用户添加到后端的列表中。
虽然当用户断开连接时 - 没有任何反应。
有什么想法吗? OO
答案 0 :(得分:1)
disconnect
事件处理程序应附加到socket
,而不是主io
对象。
所以这样的事情应该有效:
req.app.get('io').on('connection', function(socket){
...
socket.on('disconnect', function(){
...
});
});
(我也会验证.emit
。我认为它们也必须在socket
对象上调用,而不是io
。