如何在服务器端使用webrtc和nodejs创build在线用户列表
我正在使用webrtc来制作audio,video和聊天应用程序,我需要将所有用户保留在服务器端的用户列表中。 需要帮助如何做到这一点。
另外,如何从系统中注销用户?
需要帮助来实现这一点。
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); } }); } } } });
正如我使用webrtc.io模块,下面是帮助我创build用户列表和维护状态的方法。
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
}
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');