我有用户订阅聊天用户,用户可以通过套接字使自己在线或离线,
其中一位用户具有管理员的角色,他可以通过 Http 请求 User.update 强制用户在线或离线,并且我还添加了 User.publishUpdate 以提醒订阅者。
问题是当经理将用户从离线状态变为在线状态时,用户似乎在线到其他用户期望我们更新的用户自己,当我在sails中启用自动监视时问题消失了。
这里的套接字代码:
var query= {authType: 'google'}; //TODO this is temp, currently the staff will see only staff users, until we fix the performance issue
if (user.dealershipCompany) {
query = {
or: [{
authType: 'google',
dealershipCompany: null
}, {
authType: 'local',
dealershipCompany: user.dealershipCompany
}]
};
}
sails.log.info('Chat Users Query:\n', query);
User.find(query).exec(function (err, chatUsers) {
if (err) {
return callback(err, {
projectedUser: null,
porjectedChatUsers: null
});
} else {
// remove the logged in users to avoid self messaging
_.remove(chatUsers, {
id: userId
});
}
// Subscribe the connected socket to changes in the online state of chatUsers
User.subscribe(req.socket, chatUsers, 'update');
});
这里用户更新:
update: function (req, res) {
var id = req.param('id');
User.findOne(id).exec(function (err, user) {
if (err)
res.serverError(err)
else if (user) {
var firstName = req.param('firstName') || user.firstName;
var lastName = req.param('lastName') || user.lastName;
var role = req.param('role') || user.role;
var email = req.param('email') || user.email;
var online = req.param('online') || user.online;
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.role = role;
user.online = online;
user.save(function (err, updatedUser) {
if (err)
res.serverError(err);
User.publishUpdate(updatedUser.id, {
online: updatedUser.online
}, req);
res.ok(updatedUser);
});
}
else {
res.notFound({error: 'User with id: ' + id + ' Could not be found'});
}
});
},