socket.io发射给特定的用户,并给他们x的时间来回应?

我有一个在线聊天。 它使用房间。 如果用户发送消息而另一个用户不在线,则应增加“错过的消息”计数器。 我试图用setTimeout创build一个超时,如果它们发出一个事件清除超时。 然而chatOnline不会像我预期的那样触发,这会导致它总是报告用户处于脱机状态,并且将missed_texts列的计数器递增,因为它不相关。 我如何检索,如果用户从socket.io在线。 我的目标是避免在数据库中存储存在信息,这可能会很快失去控制。

我试过的代码:

 socket.on('chatSend',function(data){ if(socket.client.user.room_id !== null){ //were storing some crap in the socket object for easy retrieval. data.user_id = socket.client.user.id; data.room_id = socket.client.user.room_id; data.timestamp = ~~(new Date() / 1000); //insert message into chat table r.table('chat').insert(data).run().then(function(res){ //retrive generated record from table r.table('chat').get(res.generated_keys[0]).run().then(function(data2){ io.sockets.in(data2.room_id).emit('chatNew',data);//emit to all users in room log('chatSend'); //attempt to see if the other user is online getOtherUser(data2.room_id,socket.client.user.id,function(tid){ log('other user id: %d',tid); //all users automatically join a room matching their user id when connecting. //unsure how to see if the user is online. this doesnt work. //this is what i need help with. retrieving the other users socket resource if they are online, //and if they are not then return null or false, etc so i can work with that. var othersocket = io.sockets.in(tid); //if timeout completes before they respond, they are not online. var tmptime = setTimeout(function(){ log('other user not online.'); othersocket.removeListener('chatOnline',tmpfunc); missedTexts(data2.room_id,tid,'INCR'); },5000); var tmpfunc = function(){ clearTimeout(tmptime); //remove the listener othersocket.removeListener('chatOnline',tmpfunc); }; //emit chatOnline to other user socket //when they respond, cleartimeout, resulting in counter not being incremented. othersocket.on('chatOnline',tmpfunc); othersocket.emit('chatOnline'); }); }); }); } });