如何使用服务器端的webrtc和nodejs创建在线用户列表

时间:2013-07-04 14:57:00

标签: node.js webrtc

我正在使用webrtc制作音频,视频和聊天应用程序,我需要将所有用户保留在服务器端的用户列表中。需要帮助如何完成这项工作。

此外,如何从系统中退出用户时从列表中删除用户。

需要帮助来实现这一点。

 webRTC.rtc.on('connect', function(rtc) {
 //Client connected
 });

 webRTC.rtc.on('send answer', function(rtc) {
 //answer sent
 });

 webRTC.rtc.on('disconnect', function(rtc) {
 //Client disconnect
     //console.log(webRTC);
 });

 webRTC.rtc.on('chat_msg', function(data, socket) {
     var roomList = webRTC.rtc.rooms[data.room] || [];

     for (var i = 0; i < roomList.length; i++) {
         var socketId = roomList[i];

         if (socketId !== socket.id) {
             var soc = webRTC.rtc.getSocket(socketId);

             if (soc) {
                 soc.send(JSON.stringify({
                     "eventName": "receive_chat_msg",
                     "data": {
                     "messages": data.messages,
                     "id": data.id,
                     "from": data.from,
                     "status": data.status,
                     "email": data.email
                     }
                 }), function(error) {
                     if (error) {
                     console.log(error);
                 }
                 });
             }
         }
     }
 });

2 个答案:

答案 0 :(得分:2)

当我使用webrtc.io模块时,以下是帮助我创建用户列表并保持存在的方法。

webRTC.rtc.on('join_room', function(data, socket) {
// Will get info who joined along with his socket id
}

并且

webRTC.rtc.on('room_leave', function(room, socketid) {
// Will get info who left the room
}

答案 1 :(得分:1)

Node.js代码:

var users = {};
io.sockets.on('connection', function (socket) {
    socket.emit('connect', true);

    socket.on('message', function (data) {
        socket.broadcast.emit('message', data);
    });

    socket.on('new-user', function (username) {
        users[username] = username;
    });

    socket.on('check-presence', function (username) {
        var isUserPresent = !! users[username];
        socket.emit('presence', isUserPresent);
    });

    socket.on('remove-user', function (username) {
        var user = users[username];
        if (user) delete users[username];
    });
});

这也可以(node.js):

var users = {};
io.sockets.on('connection', function (socket) {
    var UserName;

    socket.emit('connect', true);
    socket.on('message', function (data) {
        socket.broadcast.emit('message', data);
    });

    socket.on('new-user', function (username) {
        users[username] = username;
        UserName = username;
    });

    socket.on('check-presence', function (username) {
        var isUserPresent = !! users[username];
        socket.emit('presence', isUserPresent);
    });

    // removing user on "disconnect"
    socket.on('disconnect', function () {
        var user = users[UserName];
        if (user) delete users[UserName];
    });
});

第一种情况;客户端代码:

var socket = io.connect();

socket.on('connect', function () {
    socket.emit('new-user', 'username');
});

function removeUser() {
    socket.emit('remove-user', 'username');
}

window.onbeforeunload = function () {
    removeUser();
};

// if someone pressed "F5" key to refresh the page
window.onkeyup = function (e) {
    if (e.keyCode == 116)
        removeUser();
};

// if someone leaves via <a href>
var anchors = document.querySelectorAll('a'),
    length = anchors.length;
for (var i = 0; i < length; i++) {
    var a = anchors[i];
    if (a.href.indexOf('#') !== 0 && a.getAttribute('target') != '_blank')
        a.onclick = function () {
            removeUser();
        };
}

第二种情况;客户端代码:

var socket = io.connect();

socket.on('connect', function () {
    socket.emit('new-user', 'username');
});

您也可以查看状态:

socket.on('presence', isUserPresent) {
    // boolean: user is present or not
});

socket.emit('check-presence', 'username');