Cleaned up 'src/app/channel'
This commit is contained in:
parent
985f8a250f
commit
5b5f495853
4 changed files with 160 additions and 123 deletions
|
|
@ -15,8 +15,69 @@ You should have received a copy of the GNU Affero General Public License
|
|||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||
|
||||
module.exports = class{
|
||||
constructor(name){
|
||||
constructor(server, name){
|
||||
this.server = server;
|
||||
this.name = name;
|
||||
this.userList = new Map();
|
||||
}
|
||||
|
||||
handleConnection(userDB, socket){
|
||||
//get current user object from the userlist
|
||||
var userObj = this.userList.get(userDB.user);
|
||||
|
||||
//If user is already connected
|
||||
if(userObj){
|
||||
//Add this socket on to the userobject
|
||||
userObj.sockets.push(socket.id);
|
||||
}else{
|
||||
//if this is the first connection, initialize the userObject w/ the current socked id
|
||||
userObj = {
|
||||
id: userDB.id,
|
||||
rank: userDB.rank,
|
||||
flair: userDB.flair,
|
||||
sockets: [socket.id]
|
||||
}
|
||||
}
|
||||
|
||||
//Set user entry in userlist
|
||||
this.userList.set(userDB.user, userObj);
|
||||
|
||||
//if everything looks good, admit the connection to the channel
|
||||
socket.join(socket.chan);
|
||||
|
||||
//Send out the userlist
|
||||
this.broadcastUserList(socket.chan);
|
||||
}
|
||||
|
||||
handleDisconnect(socket, reason){
|
||||
//If we have more than one active connection
|
||||
if(this.userList.get(socket.user.user).sockets.length > 1){
|
||||
//temporarily store userObj
|
||||
var userObj = this.userList.get(socket.user.user);
|
||||
|
||||
//Filter out disconnecting socket from socket list, and set as current socket list for user
|
||||
userObj.sockets = userObj.sockets.filter((id) => {
|
||||
return id != socket.id;
|
||||
});
|
||||
|
||||
//Update the userlist
|
||||
this.userList.set(socket.user.user, userObj);
|
||||
}else{
|
||||
//If this is the last connection for this user, remove them from the userlist
|
||||
this.userList.delete(socket.user.user);
|
||||
}
|
||||
|
||||
//and send out the filtered list
|
||||
this.broadcastUserList(socket.chan);
|
||||
}
|
||||
|
||||
broadcastUserList(){
|
||||
var userList = [];
|
||||
|
||||
this.userList.forEach((userObj, user) => {
|
||||
userList.push(user);
|
||||
});
|
||||
|
||||
this.server.io.in(this.name).emit("user-list", userList);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue