diff --git a/index.js b/index.js index 5bf6744d141edbb9e5ee12dd4e32288918fe98fc..c4c610d8271a68ac734da001779e5fe08b040f38 100644 --- a/index.js +++ b/index.js @@ -59,6 +59,20 @@ io.on('connection', (socket) => { }); }); + +/** +* Summary: +* Function to add a user to map of users, who are currently online. +* +* Description: +* Function will take the username from response and append it together with the socketID to map activeUsers. +* after appending, ip.emit() will send the updated userlist to all users who are currently online. +* +* @param {socket} socket +* @param {Object} response +* @triggers io.emit() 'updateUserlist' event +* @listens socket.on() 'userLogin' event +*/ async function addUserToActiveUsers(response, socket) { return new Promise(resolve => { @@ -73,6 +87,21 @@ async function addUserToActiveUsers(response, socket) { }); } + +/** +* Summary: +* Function to delete a user from the map of users, who are currently online. +* +* Description: +* Function will take the socketID from the socket, of the client, who is disconneting. +* The socketID and its value (username) will be deleted from map activeUsers. +* After deleting the socketID and username io.emit() is called and the upodated userList is sent to all users who are currently online. +* +* @param {socket} socket +* @param {Object} response +* @triggers io.emit() 'updateUserlist' event +* @listens socket.on() 'disconnect' event +*/ async function deleteUserFromActiveUsers(response, socket) { return new Promise(resolve => { @@ -86,7 +115,20 @@ async function deleteUserFromActiveUsers(response, socket) { } -//Takes 'chat message' event on Web Socket Connection. Sends Message Back to Client by using emit() function +/** +* Summary: +* Function for forwarding chat messages. +* +* Description: +* Function will take the username of the sender and the message from the response. +* Server will append the current time and call io.emit() 'chat message' event. +* Json with the message, username of sender and current time is sent to all users who are currently online. +* +* +* @param {Object} response +* @triggers io.emit() 'chat message' event +* @listens socket.on() 'chat message' event +*/ io.on('connection', (socket) => { socket.on('chat message', (response) =>{ var msg = response.msg; @@ -101,6 +143,20 @@ io.on('connection', (socket) => { }) }); +/** +* Summary: +* Function for forwarding private chat messages. +* +* Description: +* Function will take the username of the sender, the private message, the name of the receiver from the response. +* Server will append the current time and call io.emit() 'chat message' event. +* +* +* +* @param {Object} response +* @triggers io.emit() 'chat message' event +* @listens socket.on() 'chat message' event +*/ io.on('connection', (socket) => { socket.on('private message', (response) => { @@ -146,9 +202,6 @@ io.on('connection', (socket) => { console.log("sender", sender); - /* for () - if (activeUsers receivers[i].trim()) */ - for (let i = 0; i<receivers.length; i++) { io.emit(receivers[i].trim(), { msg: groupMsg, @@ -175,7 +228,6 @@ async function getKeyFromVal(receiver){ console.log("array[i] ", array[i][0]); if(array[i][1] == receiver) { receiverSocketId = array[i][0]; - console.log("Receiver ID in if: ", receiverSocketId); resolve(receiverSocketId); } else { resolve("false"); diff --git a/routes/login.js b/routes/login.js index f8ed8c05568a3015a2640aadf75bd1a402633b09..85541610369ff2271e8757abe1cccb39676b5eea 100644 --- a/routes/login.js +++ b/routes/login.js @@ -19,15 +19,11 @@ require('dotenv').config(); * Function will check, whether username and password from request are the same, as in the database. * If successful, a JWT accessToke is created and statuscode 200 and the Accesstoke are sent to client. * If not successful, statuscode 401 is sent to client. +* Uses bcrypt library. * -* -* @fires fetch to "/login" -* @fires function getCookie() -* @listens submit event of "Submit" Button +* @listens fetch to"/login" from client * * @param {Json} req Json Body from Client. -* -* * @return {Json} res Response from server. */ router.post('/', async(req, res) => { diff --git a/routes/register.js b/routes/register.js index a4449a5db27429428c642a88ef286753ed71f7cd..ccd9ea30dd1d79a9b59155c30133d71f11778ffc 100644 --- a/routes/register.js +++ b/routes/register.js @@ -5,7 +5,28 @@ const express = require('express'); const router = express.Router() const bodyParser = require('body-parser'); - +/** +* Summary: +* Handles registration of client. +* +* Description: +* Function handles registration of client. +* Uses bcrypt library. +* Checks, whether username already exists or not. +* If username is not taken yet, the username und the hashed password are saved to the database using mongoDB and 200 is sent to client. +* if not successful 401 is sent to client. +* +* +* +* @listens fetch '/register' from client +* +* @param {Object} res Description. +* @param {Object} req Description of optional variable. +* +* +* @return {HTTP statusCode} 200 +* @return {HTTP statusCode} 401 +*/ router.post('/', async(req, res) => { console.log(req.body); bcrypt.hash(req.body.password, 10, function(err, hashedPass){