Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cloud_Computing
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alexander Tim Hobelsberger
Cloud_Computing
Commits
07398758
Commit
07398758
authored
2 years ago
by
Markus Klose
Browse files
Options
Downloads
Patches
Plain Diff
More comments
parent
e3c0f5de
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
index.js
+57
-5
57 additions, 5 deletions
index.js
routes/login.js
+2
-6
2 additions, 6 deletions
routes/login.js
routes/register.js
+22
-1
22 additions, 1 deletion
routes/register.js
with
81 additions
and
12 deletions
index.js
+
57
−
5
View file @
07398758
...
@@ -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
"
);
...
...
This diff is collapsed.
Click to expand it.
routes/login.js
+
2
−
6
View file @
07398758
...
@@ -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
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
routes/register.js
+
22
−
1
View file @
07398758
...
@@ -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
){
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment