javascript - socket.io - check if a client is in an specific room -
i check if client subscribed specific room. have array saved sockets , index username. have if clause don't work :
if(io.sockets.manager.rooms['/' + data.to].indexof(users[partner].socket.id) >= 0) {
so maybe have working code.
update: code:
/* mysql setup here connection */ var count = 0; var messages = 0; var users = {}; var io = require('socket.io')(3000); io.sockets.on('connection', function(client) { count++; client.emit('send login'); client.on('login user',function (data) { if(data.name in users) { } else { client.u_name = data.name; client.u_id = data.id; users[client.u_id] = {'name':client.username,'socket':client}; io.sockets.emit('online users', { 'count': count }); } }); client.on('disconnect', function(){ count--; delete users[client.username]; io.sockets.emit('online users', { 'count': count }); }); client.on('join',function(room){ client.join(room); }); client.on('leave',function(room){ client.leave(room); }); client.on('new message',function (data) { if(client.u_id in users) { connection.query('insert chats_msg(u_from,chat_id,msg) values(?,?,?)', [client.u_id,data.to,data.msg], function(err, rows, fields) { if (err) console.log(colors.red('error @ query')); query('update chats set last_msg=?,last_time=current_timestamp,last_from=? id=?',[data.msg,client.u_id,data.to]); }); io.sockets.in(data.to).emit('message',{'msg':data.msg,'from':client.u_name}); connection.query('select * chats id=? limit 1', data.to, function(err, rows, fields) { if (err) console.log(colors.red('error @ query')); if(rows[0].u_1 == client.u_id) { var partner = rows[0].u_2; } else { var partner = rows[0].u_1; } if(io.sockets.manager.rooms['/' + data.to].indexof(users[partner].socket.id) >= 0) { users[partner].socket.emit('error msg','new message'); } else { users[partner].socket.emit('error msg','yeahh'); } }); messages++; } else { client.emit('error msg','client or not online'); } }); });
using socket id can do:
io.sockets.adapter.sids[socket.id][roomname]
it true if socket in room , undefined if not
(version 1.4.5)
Comments
Post a Comment