Skip to content
Snippets Groups Projects
Commit 07398758 authored by Markus Klose's avatar Markus Klose
Browse files

More comments

parent e3c0f5de
No related branches found
No related tags found
No related merge requests found
...@@ -59,6 +59,20 @@ io.on('connection', (socket) => { ...@@ -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) { async function addUserToActiveUsers(response, socket) {
return new Promise(resolve => { return new Promise(resolve => {
...@@ -73,6 +87,21 @@ async function addUserToActiveUsers(response, socket) { ...@@ -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) { async function deleteUserFromActiveUsers(response, socket) {
return new Promise(resolve => { return new Promise(resolve => {
...@@ -86,7 +115,20 @@ async function deleteUserFromActiveUsers(response, socket) { ...@@ -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) => { io.on('connection', (socket) => {
socket.on('chat message', (response) =>{ socket.on('chat message', (response) =>{
var msg = response.msg; var msg = response.msg;
...@@ -101,6 +143,20 @@ io.on('connection', (socket) => { ...@@ -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) => { io.on('connection', (socket) => {
socket.on('private message', (response) => { socket.on('private message', (response) => {
...@@ -146,9 +202,6 @@ io.on('connection', (socket) => { ...@@ -146,9 +202,6 @@ io.on('connection', (socket) => {
console.log("sender", sender); console.log("sender", sender);
/* for ()
if (activeUsers receivers[i].trim()) */
for (let i = 0; i<receivers.length; i++) { for (let i = 0; i<receivers.length; i++) {
io.emit(receivers[i].trim(), { io.emit(receivers[i].trim(), {
msg: groupMsg, msg: groupMsg,
...@@ -175,7 +228,6 @@ async function getKeyFromVal(receiver){ ...@@ -175,7 +228,6 @@ async function getKeyFromVal(receiver){
console.log("array[i] ", array[i][0]); console.log("array[i] ", array[i][0]);
if(array[i][1] == receiver) { if(array[i][1] == receiver) {
receiverSocketId = array[i][0]; receiverSocketId = array[i][0];
console.log("Receiver ID in if: ", receiverSocketId);
resolve(receiverSocketId); resolve(receiverSocketId);
} else { } else {
resolve("false"); resolve("false");
......
...@@ -19,15 +19,11 @@ require('dotenv').config(); ...@@ -19,15 +19,11 @@ require('dotenv').config();
* Function will check, whether username and password from request are the same, as in the database. * 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 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. * If not successful, statuscode 401 is sent to client.
* Uses bcrypt library.
* *
* * @listens fetch to"/login" from client
* @fires fetch to "/login"
* @fires function getCookie()
* @listens submit event of "Submit" Button
* *
* @param {Json} req Json Body from Client. * @param {Json} req Json Body from Client.
*
*
* @return {Json} res Response from server. * @return {Json} res Response from server.
*/ */
router.post('/', async(req, res) => { router.post('/', async(req, res) => {
......
...@@ -5,7 +5,28 @@ const express = require('express'); ...@@ -5,7 +5,28 @@ const express = require('express');
const router = express.Router() const router = express.Router()
const bodyParser = require('body-parser'); 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) => { router.post('/', async(req, res) => {
console.log(req.body); console.log(req.body);
bcrypt.hash(req.body.password, 10, function(err, hashedPass){ bcrypt.hash(req.body.password, 10, function(err, hashedPass){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment