Skip to content
Snippets Groups Projects
Commit 494b4b4e authored by Alexander Tim Hobelsberger's avatar Alexander Tim Hobelsberger
Browse files

Kommentare

parent 0a68b5a3
No related branches found
No related tags found
No related merge requests found
...@@ -249,7 +249,7 @@ io.on('connection', (socket) => { ...@@ -249,7 +249,7 @@ io.on('connection', (socket) => {
}) })
}) })
async function getKeyFromVal(receiver){ /* async function getKeyFromVal(receiver){
return new Promise( () => { return new Promise( () => {
var array = Array.from(activeUsers); var array = Array.from(activeUsers);
console.log("usersArray: " + array); console.log("usersArray: " + array);
...@@ -264,7 +264,7 @@ async function getKeyFromVal(receiver){ ...@@ -264,7 +264,7 @@ async function getKeyFromVal(receiver){
} }
} }
}) })
} } */
//Logs message from Socket to Console //Logs message from Socket to Console
io.on('connection', (socket) => { io.on('connection', (socket) => {
...@@ -273,21 +273,49 @@ io.on('connection', (socket) => { ...@@ -273,21 +273,49 @@ io.on('connection', (socket) => {
}); });
}); });
//Send Image File
/**
* Summary:
* Function for sending image files
*
*
* Description:
* By triggering the 'sendImage' Event, the function emits the given files url to client
*
* @listens socket.on() 'sendImage' event
*/
io.on("connection", (socket) => { io.on("connection", (socket) => {
socket.on('sendImage', (file, callback) => { socket.on('sendImage', (file, callback) => {
io.emit('image', file); io.emit('image', file);
}); });
}); });
//Send Video File /**
* Summary:
* Function for sending video files
*
*
* Description:
* By triggering the 'sendVideo' Event, the function emits the given files url to client
*
* @listens socket.on() 'sendVideo' event
*/
io.on("connection", (socket) => { io.on("connection", (socket) => {
socket.on('sendVideo', (file, callback) => { socket.on('sendVideo', (file, callback) => {
io.emit('video', file); io.emit('video', file);
}); });
}); });
//Send Audio File /**
* Summary:
* Function for sending audio files
*
*
* Description:
* By triggering the 'sendAudio' Event, the function emits the given files url to client
*
* @listens socket.on() 'sendAudio' event
*/
io.on("connection", (socket) => { io.on("connection", (socket) => {
socket.on('sendAudio', (file, callback) => { socket.on('sendAudio', (file, callback) => {
io.emit('audio', file); io.emit('audio', file);
......
...@@ -8,6 +8,25 @@ const bodyParser = require('body-parser'); ...@@ -8,6 +8,25 @@ const bodyParser = require('body-parser');
const { verify } = require('crypto'); const { verify } = require('crypto');
require('dotenv').config(); require('dotenv').config();
/**
* Summary:
* Handles authorization of client.
*
* Description:
* The function receives clients webtoken over the authorization part of the request header. By using JWT as technology, we can
* verify the token of the client. The signature of the client has to be valid, otherwise the function sends 401 as status code.
* JWT provides a assymetric encryption process.
*
*
* @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.get('/', async(req, res) => { router.get('/', async(req, res) => {
authHeader = req.headers.authorization; authHeader = req.headers.authorization;
......
...@@ -12,7 +12,7 @@ require('dotenv').config(); ...@@ -12,7 +12,7 @@ require('dotenv').config();
/** /**
* Summary: * Summary:
* Function tto verify the Login of a client. * Function to verify the Login of a client.
* *
* Description: * Description:
* Username and password of client get checked, whether they exist in the database. * Username and password of client get checked, whether they exist in the database.
......
...@@ -272,14 +272,18 @@ ...@@ -272,14 +272,18 @@
}); });
/**
* Summary:
* Provides url of the given file to share it in the chatroom
// Alex schreib nen Kommentar *
* Description:
* The functions reads the type of the given file and fires the specific socket.emit event for each file type.
* The function emits the file Object URL over the specific event event
*
*/
function upload(files) { function upload(files) {
var fileObject = files[0]; var fileObject = files[0];
var url = URL.createObjectURL(fileObject); var url = URL.createObjectURL(fileObject);
console.log(files[0].type.split("/")[0]);
var type = files[0].type.split("/")[0]; var type = files[0].type.split("/")[0];
if(type == "image"){ if(type == "image"){
socket.emit("sendImage", url); socket.emit("sendImage", url);
...@@ -292,9 +296,16 @@ ...@@ -292,9 +296,16 @@
} }
} }
//Receive Multimedia Files from Server /**
* Summary:
* Receives image files
*
* Description:
* By triggering the image event, the function creates a image object wich takes the file url as source. After setting width and height
* of the image field, the object gets appended to the HTML file
*
*/
socket.on('image', function(file) { socket.on('image', function(file) {
console.log(file);
var image = new Image(); var image = new Image();
image.height = 400; image.height = 400;
image.width = 400; image.width = 400;
...@@ -302,6 +313,15 @@ ...@@ -302,6 +313,15 @@
messages.appendChild(image); messages.appendChild(image);
}); });
/**
* Summary:
* Receives video files
*
* Description:
* By triggering the video event, the function creates a image object wich takes the file url as source. After setting width and height
* of the video field, the object gets appended to the HTML file
*
*/
socket.on('video', function(file) { socket.on('video', function(file) {
const videoHTML = document.createElement('video'); const videoHTML = document.createElement('video');
videoHTML.src = file; videoHTML.src = file;
...@@ -311,6 +331,15 @@ ...@@ -311,6 +331,15 @@
messages.appendChild(videoHTML); messages.appendChild(videoHTML);
}); });
/**
* Summary:
* Receives audio files
*
* Description:
* By triggering the audio event, the function creates a image object wich takes the file url as source. After setting the attributes of the html audio
* tag, the element gets appendet to the HTML file
*
*/
socket.on('audio', function(file) { socket.on('audio', function(file) {
const audioHTML = document.createElement('audio'); const audioHTML = document.createElement('audio');
audioHTML.id = 'audioPlayer'; audioHTML.id = 'audioPlayer';
...@@ -320,20 +349,20 @@ ...@@ -320,20 +349,20 @@
messages.appendChild(audioHTML); messages.appendChild(audioHTML);
}); });
/** /**
* Summary: * Summary:
* Function for getting values from cookie. * Function for getting values from cookie.
* *
* Description: * Description:
* This function gets the name of a key and returns it's value from a cookie. * This function gets the name of a key and returns it's value from a cookie.
* *
* *
* @ * @
* *
* @param {String} name name of the key, it's value is wished. * @param {String} name name of the key, it's value is wished.
* *
* @return {String} value of the key in cookie. * @return {String} value of the key in cookie.
*/ */
function getCookie(name) { function getCookie(name) {
const value = `; ${document.cookie}`; const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`); const parts = value.split(`; ${name}=`);
......
...@@ -22,6 +22,22 @@ ...@@ -22,6 +22,22 @@
const form = document.getElementById('login-form'); const form = document.getElementById('login-form');
form.addEventListener('submit', submitLoginData); form.addEventListener('submit', submitLoginData);
/**
* Summary:
* Handles Login of Client.
*
* Description:
* Logindata of Client, username and password are sent to server.
* The Server responses status code 200 if the client is registred and a valid web token provided by JWT.
* The token gets saved as a cookie. If the status code is 200, the authentication function authToken() gets called.
*
*
* @fires fetch to "/login"
* @fires function getCookie()
* @fires function authToken()
* @listens submit event of "Submit" Button
*/
async function submitLoginData(event) { async function submitLoginData(event) {
event.preventDefault(); event.preventDefault();
...@@ -59,6 +75,20 @@ ...@@ -59,6 +75,20 @@
.catch(error => console.log('error', error)); .catch(error => console.log('error', error));
} }
/**
* Summary:
* Function for getting values from cookie.
*
* Description:
* This function gets the name of a key and returns it's value from a cookie.
*
*
* @
*
* @param {String} name name of the key, it's value is wished.
*
* @return {String} value of the key in cookie.
*/
function getCookie(name) { function getCookie(name) {
const value = `; ${document.cookie}`; const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`); const parts = value.split(`; ${name}=`);
...@@ -66,6 +96,19 @@ ...@@ -66,6 +96,19 @@
if (parts.length === 2) return parts.pop().split(';').shift(); if (parts.length === 2) return parts.pop().split(';').shift();
} }
/**
* Summary:
* Handles the authentication of clients web token
*
* Description:
* The provided web token get set as a header for the http GET request. If the token is valid, the server sends back a status code of 200.
* If the status code is 200, the function redirects in the chatroom
*
*
* @fires fetch to "/authenticate"
* @fires function setCookie()
* @listens for call in submitLoginData()
*/
async function authToken(){ async function authToken(){
var token = getCookie("token").split(','); var token = getCookie("token").split(',');
var myHeaders = new Headers(); var myHeaders = new Headers();
...@@ -80,23 +123,19 @@ ...@@ -80,23 +123,19 @@
fetch("http://localhost:3000/authenticate", requestOptions) fetch("http://localhost:3000/authenticate", requestOptions)
.then((response) => { .then((response) => {
// Weiterleitung an Chatroom, bei erfolreicher Anmeldung (StatusCode == 200)
if (response.status === 200) { if (response.status === 200) {
// Rückmeldung an Client
var divErfolgreich = document.createElement('div'); var divErfolgreich = document.createElement('div');
divErfolgreich.textContent = "Erfolgreich eingeloggt! Sie werden in den Chatroom weitergeleitet"; divErfolgreich.textContent = "Erfolgreich eingeloggt! Sie werden in den Chatroom weitergeleitet";
document.body.appendChild(divErfolgreich); document.body.appendChild(divErfolgreich);
// Speichern des Username in Cookie
setCookie(username.value.trim()); setCookie(username.value.trim());
/* socket.emit('userLogin', {
name : username.value.trim()
}); */
// Kurzer Timeout, dass der Client die Nachricht lesen kann
setTimeout(function() { setTimeout(function() {
window.location.href = "/chatroom"; window.location.href = "/chatroom";
}, 2000); }, 2000);
} }
// Bei nicht erfolgreichem Login (StatusCode != 200) werden die Eingabefelder geleert und dem Client eine Rückmeldung gegeben
else { else {
username.value = ""; username.value = "";
password.value = ""; password.value = "";
...@@ -109,6 +148,13 @@ ...@@ -109,6 +148,13 @@
.catch(error => console.log('error', error)); .catch(error => console.log('error', error));
} }
/**
* Summary:
* Sets cookie by given name
*
*
* @param {String} name name of the cookie
*/
function setCookie(name) { function setCookie(name) {
document.cookie = "username=" + name; document.cookie = "username=" + name;
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<title>Document</title> <title>Document</title>
</head> </head>
<body> <body>
<!-- Start Seite, mit Buttons, die zur Loginseite/ Registrierungsseite weiterleiten--> <!--Page to redirect to login or registration formular-->
<h1>Guten Tag!</h1> <h1>Guten Tag!</h1>
<a href="/login">Login</a> <a href="/login">Login</a>
......
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