Skip to content

Commit

Permalink
feat: Select a user randomly from available users (wdlsvnit#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
jainkeval committed Mar 13, 2018
1 parent 8e1472a commit 33c2d1f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 52 deletions.
2 changes: 1 addition & 1 deletion globals.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// All globals here.

global.onlineUsers = [];
global.availableUsers = [];
global.rooms = [];
global.queue = [];
8 changes: 4 additions & 4 deletions public/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

socket.on('ack', (d) => {
console.log(`Received: ${d}`);
socket.emit('privateRoom', {
"room": "private room"
});
});

let message = document.querySelector('#message');
Expand All @@ -15,9 +18,6 @@
let newbtn = document.querySelector('#newbtn');
let close = document.querySelector('#close');
let cancel = document.querySelector('#cancel');
socket.emit('privateRoom', {
"room": "private room"
});

socket.on('toast', (data) => {
toastr.options = {
Expand Down Expand Up @@ -148,4 +148,4 @@
socket.disconnect();
});

})();
})();
117 changes: 70 additions & 47 deletions socketserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,92 @@ const moment = require('moment');
require('./globals.js');
module.exports = (io, app) => {
io.on('connection', (socket) => {
// Make a user object and add it to the onlineUsers list and rooms too(maybe we can add to room once we have the partner.)
socket.emit('ack', { id: socket.id, msg: "User connected" });
onlineUsers.push(socket);
socket.on('privateRoom', (user) => {
let unfilledRooms = rooms.filter((room) => {
if (!room.isFilled) {
console.log(room.roomID);
return room;
}
});
console.log(`Unfilled Rooms: ${JSON.stringify(unfilledRooms[0])}`);
try {
// join the existing room.
socket.join(unfilledRooms[0].roomID);
let index = rooms.indexOf(unfilledRooms[0]);
rooms[index].isFilled = true;
unfilledRooms[0].isFilled = true;
socket.emit('private ack', { "message": "Added to privateRoom", "roomID": unfilledRooms[0].roomID });
socket.roomID = unfilledRooms[0].roomID;
io.sockets.in(socket.roomID).emit('toast', { "message": "You are connected with a stranger!"})
console.log(`Joined existing room: ${unfilledRooms[0].roomID}`);
console.log(`--------------------------------------------`);
}
catch(e) {
// dont have unfilled rooms. Thus creating a new user.
let uID = uniqueID();
console.log(`Created new room: ${uID}`);
rooms.push({ "roomID": uID, "isFilled": false });
socket.join(uID);
socket.roomID = uID;
console.log(`Socket joined in room with id: ${uID}`);
socket.emit('private ack', { "message": "Added to privateRoom", "roomID": uID });
// console.log(`Current status of rooms: ${rooms}`);
console.log(`--------------------------------------------`);
let windowID = socket;
//push the user to avilable users list
availableUsers.push(socket);
let resolveAfter5Seconds = () => {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 5000);
});
}
// create an unique id here.
// let uID = uniqueID();
// console.log(uID);
// rooms.push({ "roomID": uID, "isFilled": false });
// // Maintain a global room array which would store the room ids.
// socket.join(uID);
// // emit the room id to the frontend side.
// socket.emit('private ack', { "message": "Added to privateRoom", "roomID": uID });
});
async function asyncCall() {
let result = await resolveAfter5Seconds();
//get index of randomly selected user from the available users list
let selected = Math.floor(Math.random()*availableUsers.length);
//store the user in Socket
socket = availableUsers[selected];
//remove the randomly selected user from the available users list
availableUsers.splice(selected,1);
// Make a user object and add it to the onlineUsers list and rooms too(maybe we can add to room once we have the partner.)

// create an unique id here.
// let uID = uniqueID();
// console.log(uID);
// rooms.push({ "roomID": uID, "isFilled": false });
// // Maintain a global room array which would store the room ids.
// socket.join(uID);
// // emit the room id to the frontend side.
// socket.emit('private ack', { "message": "Added to privateRoom", "roomID": uID });
socket.emit('ack', { id: socket.id, msg: "User connected" });
onlineUsers.push(socket);

socket.on('privateRoom', (user) => {
let unfilledRooms = rooms.filter((room) => {
if (!room.isFilled) {
console.log(room.roomID);
return room;
}
});
console.log(`Unfilled Rooms: ${JSON.stringify(unfilledRooms[0])}`);
try {
// join the existing room.
socket.join(unfilledRooms[0].roomID);
let index = rooms.indexOf(unfilledRooms[0]);
rooms[index].isFilled = true;
unfilledRooms[0].isFilled = true;
socket.emit('private ack', { "message": "Added to privateRoom", "roomID": unfilledRooms[0].roomID });
socket.roomID = unfilledRooms[0].roomID;
io.sockets.in(socket.roomID).emit('toast', { "message": "You are connected with a stranger!"})
console.log(`Joined existing room: ${unfilledRooms[0].roomID}`);
console.log(`--------------------------------------------`);
}
catch(e) {
// dont have unfilled rooms. Thus creating a new user.
let uID = uniqueID();
console.log(`Created new room: ${uID}`);
rooms.push({ "roomID": uID, "isFilled": false });
socket.join(uID);
socket.roomID = uID;
console.log(`Socket joined in room with id: ${uID}`);
socket.emit('private ack', { "message": "Added to privateRoom", "roomID": uID });
// console.log(`Current status of rooms: ${rooms}`);
console.log(`--------------------------------------------`);
}
});

}
asyncCall();

socket.on('sendMessage', (data) => {
let timeStamp = moment().format('LT');
io.sockets.in(data.room).emit('newMessage', { "message": data , "senderId": socket.id, "timeStamp": timeStamp});
io.sockets.in(data.room).emit('newMessage', { "message": data , "senderId": windowID.id, "timeStamp": timeStamp});
});

socket.on('typing', (data) => {
io.sockets.in(data.room).emit('addTyping', { "senderId": socket.id, "typingStatus": data.typingStatus });
io.sockets.in(data.room).emit('addTyping', { "senderId": windowID.id, "typingStatus": data.typingStatus });
});

// Disconnect the user
socket.on('disconnect', () => {
let index = onlineUsers.indexOf(socket);
onlineUsers.splice(index,1);
index = rooms.findIndex(x => x.roomID == socket.roomID);
index = rooms.findIndex(x => x.roomID == windowID.roomID);
if(index >= 0){
if(rooms[index].isFilled == true){
let warning = { "title": "Stranger is disconnected!", "message": "Please click on 'New' button to connect to someone else." };
io.sockets.in(socket.roomID).emit('alone', { "warning": warning, "roomID": socket.roomID });
io.sockets.in(windowID.roomID).emit('alone', { "warning": warning, "roomID": windowID.roomID });
rooms.splice(index,1);
}
else{
Expand Down

0 comments on commit 33c2d1f

Please sign in to comment.