admin 管理员组文章数量: 1086019
I would like to check if an client is subscribed to an specific room. I have an array where I saved the sockets and as index the username. I already have an if clause but it don't work :
if(io.sockets.manager.rooms['/' + data.to].indexOf(users[partner].socket.id) >= 0) {
So maybe someone have an working code.
UPDATE: code:
/* MYSQL SETUP HERE FOR 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 INTO 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 at Query'));
query('UPDATE chats SET last_msg=?,last_time=CURRENT_TIMESTAMP,last_from=? WHERE 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 * FROM chats WHERE id=? LIMIT 1', data.to, function(err, rows, fields) {
if (err) console.log(colors.red('ERROR at 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 you are not online');
}
});
});
I would like to check if an client is subscribed to an specific room. I have an array where I saved the sockets and as index the username. I already have an if clause but it don't work :
if(io.sockets.manager.rooms['/' + data.to].indexOf(users[partner].socket.id) >= 0) {
So maybe someone have an working code.
UPDATE: code:
/* MYSQL SETUP HERE FOR 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 INTO 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 at Query'));
query('UPDATE chats SET last_msg=?,last_time=CURRENT_TIMESTAMP,last_from=? WHERE 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 * FROM chats WHERE id=? LIMIT 1', data.to, function(err, rows, fields) {
if (err) console.log(colors.red('ERROR at 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 you are not online');
}
});
});
Share
Improve this question
edited Jan 28, 2016 at 11:21
Daniel O.
asked Jan 28, 2016 at 11:13
Daniel O.Daniel O.
1964 silver badges16 bronze badges
2
- 1 Could you please post some more code (where do you save the user into the array, for example)? With this piece we can only guess... – nils Commented Jan 28, 2016 at 11:19
- Added some more code – Daniel O. Commented Jan 28, 2016 at 11:21
1 Answer
Reset to default 8Using the socket id you can do:
io.sockets.adapter.sids[socket.id][roomname]
it will be true if is the socket is in room and undefined if it is not
(version 1.4.5)
本文标签: javascriptsocketiocheck if a client is in an specific roomStack Overflow
版权声明:本文标题:javascript - socket.io - check if a client is in an specific room - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744075605a2529292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论