1

我正在使用 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);
                 }
                 });
             }
         }
     }
 });
4

2 回答 2

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
}

于 2013-08-01T14:45:47.733 回答
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');
于 2013-07-05T13:08:56.167 回答