socket.io - 为一个房间播放

时间:2014-05-07 18:52:24

标签: node.js socket.io

为什么房间广播不起作用? 我觉得我还是按照文档说的。 如何检查房间里有客户?

io.sockets.on('connection', function (socket) {

socket.secure = true;

socket.on('connection', function (id_user) {
    socket.broadcast.emit('connection', id_user);

    var condition = { id_user: id_user };
    var query = dbh.query('SELECT `id_channel` FROM `TABG_pm_channel` WHERE ?', condition, function(err, rows, fields) {
        rows.forEach(function(row) {
            socket.join(row.id_channel);
            console.log('ajout de l\'user #'+id_user+' au salon #'+row.id_channel);
        });
    });

    console.log('Connection of user id '+id_user);
});

socket.on('disco', function (id_user) {
    socket.broadcast.emit('disco', id_user);
    console.log('Disconnection of user id '+id_user);
});

socket.on('privmsg', function (msg) {
    msg.text = sanitizer.sanitize(msg.text);

    post  = {id: '', id_channel: msg.id_channel, msg: msg.text, invitation: 0, id_sender: msg.id_sender, date: date()};
    dbh.query('INSERT INTO TABG_pm SET ?', post);

    socket.broadcast.to(post.id_channel).emit('new_privmsg', msg.text);
});

1 个答案:

答案 0 :(得分:0)

io.sockets.clients('room')将为您提供会议室中所有客户的实例。

https://github.com/LearnBoost/socket.io/wiki/Rooms