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
Automate
Agent sessions
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alexander Tim Hobelsberger
Cloud_Computing
Commits
4ba77a99
Commit
4ba77a99
authored
Nov 1, 2022
by
Alexander Tim Hobelsberger
Browse files
Options
Downloads
Patches
Plain Diff
Private Message
parent
8f61cfce
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
index.js
+15
-7
15 additions, 7 deletions
index.js
views/index.html
+46
-6
46 additions, 6 deletions
views/index.html
with
61 additions
and
13 deletions
index.js
+
15
−
7
View file @
4ba77a99
...
@@ -12,6 +12,7 @@ const loginRoute = require('./routes/login');
...
@@ -12,6 +12,7 @@ const loginRoute = require('./routes/login');
const
fs
=
require
(
'
fs
'
);
const
fs
=
require
(
'
fs
'
);
const
activeUsers
=
new
Map
();
const
activeUsers
=
new
Map
();
const
authenticationRoute
=
require
(
'
./routes/auth
'
);
const
authenticationRoute
=
require
(
'
./routes/auth
'
);
const
{
response
}
=
require
(
'
express
'
);
//DB Connection
//DB Connection
...
@@ -43,11 +44,8 @@ app.get('/login', (req, res) => {
...
@@ -43,11 +44,8 @@ app.get('/login', (req, res) => {
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
console
.
log
(
'
connection
'
);
var
socketId
=
socket
.
id
;
var
socketId
=
socket
.
id
;
// Triggered by emit() from client, when entering the chatroom
// Triggered by emit() from client, when entering the chatroom
// SocketID => name
// SocketID => name
socket
.
on
(
'
userLogin
'
,
function
(
response
)
{
socket
.
on
(
'
userLogin
'
,
function
(
response
)
{
...
@@ -89,18 +87,28 @@ io.on('connection', (socket) => {
...
@@ -89,18 +87,28 @@ io.on('connection', (socket) => {
var
name
=
response
.
name
;
var
name
=
response
.
name
;
var
dateTime
=
new
Date
().
toTimeString
();
var
dateTime
=
new
Date
().
toTimeString
();
console
.
log
(
"
index.js Msg:
"
,
msg
);
console
.
log
(
"
index.js name:
"
,
name
);
io
.
emit
(
'
chat message
'
,
{
io
.
emit
(
'
chat message
'
,
{
msg
:
msg
,
msg
:
msg
,
name
:
name
,
name
:
name
,
dateTime
:
dateTime
dateTime
:
dateTime
});
});
})
})
});
});
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
socket
.
on
(
'
private message
'
,
(
response
)
=>
{
var
private_msg
=
response
.
private_msg
;
var
receiver
=
response
.
receiver
;
var
sender
=
response
.
name
;
io
.
emit
(
receiver
,
{
private_msg
:
private_msg
,
receiver
:
receiver
,
sender
:
sender
})
})
})
//Logs message from Socket to Console
//Logs message from Socket to Console
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
socket
.
on
(
'
chat message
'
,
(
response
)
=>
{
socket
.
on
(
'
chat message
'
,
(
response
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
views/index.html
+
46
−
6
View file @
4ba77a99
...
@@ -6,14 +6,22 @@
...
@@ -6,14 +6,22 @@
body
{
margin
:
0
;
padding-bottom
:
3rem
;
font-family
:
-
apple-system
,
BlinkMacSystemFont
,
"Segoe UI"
,
Roboto
,
Helvetica
,
Arial
,
sans-serif
;
}
body
{
margin
:
0
;
padding-bottom
:
3rem
;
font-family
:
-
apple-system
,
BlinkMacSystemFont
,
"Segoe UI"
,
Roboto
,
Helvetica
,
Arial
,
sans-serif
;
}
#form
{
background
:
rgba
(
0
,
0
,
0
,
0.15
);
padding
:
0.25rem
;
position
:
fixed
;
bottom
:
0
;
left
:
0
;
right
:
0
;
display
:
flex
;
height
:
3rem
;
box-sizing
:
border-box
;
backdrop-filter
:
blur
(
10px
);
}
#form
{
background
:
rgba
(
0
,
0
,
0
,
0.15
);
padding
:
0.25rem
;
position
:
fixed
;
bottom
:
0
;
left
:
0
;
right
:
0
;
display
:
flex
;
height
:
3rem
;
box-sizing
:
border-box
;
backdrop-filter
:
blur
(
10px
);
}
#form_private
{
background
:
rgba
(
0
,
0
,
0
,
0.15
);
padding
:
0.25rem
;
position
:
fixed
;
bottom
:
3em
;
left
:
0
;
right
:
0
;
display
:
flex
;
height
:
3rem
;
box-sizing
:
border-box
;
backdrop-filter
:
blur
(
10px
);
}
#input
{
border
:
none
;
padding
:
0
1rem
;
flex-grow
:
1
;
border-radius
:
2rem
;
margin
:
0.25rem
;
}
#input
{
border
:
none
;
padding
:
0
1rem
;
flex-grow
:
1
;
border-radius
:
2rem
;
margin
:
0.25rem
;
}
#input_private
{
border
:
none
;
padding
:
0
1rem
;
flex-grow
:
1
;
border-radius
:
2rem
;
margin
:
0.25rem
;
}
#input_private_reciever
{
border
:
none
;
padding
:
0
1rem
;
border-radius
:
2rem
;
margin
:
0.25rem
;
}
#input
:focus
{
outline
:
none
;
}
#input
:focus
{
outline
:
none
;
}
#form
>
button
{
background
:
#333
;
border
:
none
;
padding
:
0
1rem
;
margin
:
0.25rem
;
border-radius
:
3px
;
outline
:
none
;
color
:
#fff
;
}
#form
>
button
{
background
:
#333
;
border
:
none
;
padding
:
0
1rem
;
margin
:
0.25rem
;
border-radius
:
3px
;
outline
:
none
;
color
:
#fff
;
}
#form_private
>
button
{
background
:
#333
;
border
:
none
;
padding
:
0
1rem
;
margin
:
0.25rem
;
border-radius
:
3px
;
outline
:
none
;
color
:
#fff
;
}
#messages
{
list-style-type
:
none
;
margin
:
0
;
padding
:
0
;
}
#messages
{
list-style-type
:
none
;
margin
:
0
;
padding
:
0
;
}
#messages
>
li
{
padding
:
0.5rem
1rem
;
}
#messages
>
li
{
padding
:
0.5rem
1rem
;
}
#messages
>
li
:nth-child
(
odd
)
{
background
:
#efefef
;
}
#messages
>
li
:nth-child
(
odd
)
{
background
:
#efefef
;
}
#messages_private
{
list-style-type
:
none
;
margin
:
0
;
padding
:
0
;
}
#messages_private
>
li
{
padding
:
0.5rem
1rem
;
}
#messages_private
>
li
:nth-child
(
odd
)
{
background
:
#efefef
;
}
#activeUsers
{
list-style-type
:
none
;
margin
:
0
;
padding
:
0
;
}
#activeUsers
{
list-style-type
:
none
;
margin
:
0
;
padding
:
0
;
}
#activeUsers
>
li
{
padding
:
0.5rem
1rem
;
}
#activeUsers
>
li
{
padding
:
0.5rem
1rem
;
}
#activeUsers
>
li
:nth-child
(
odd
)
{
background
:
#efefef
;
}
#activeUsers
>
li
:nth-child
(
odd
)
{
background
:
#efefef
;
}
...
@@ -24,9 +32,15 @@
...
@@ -24,9 +32,15 @@
<ul
id=
"activeUsers"
>
Active Users
</ul>
<ul
id=
"activeUsers"
>
Active Users
</ul>
<ul
id=
"messages"
></ul>
<ul
id=
"messages"
></ul>
<ul
id=
"messages_private"
></ul>
<form
id=
"form"
action=
""
>
<form
id=
"form"
action=
""
>
<input
id=
"input"
autocomplete=
"off"
/><button>
Send
</button>
<input
id=
"input"
autocomplete=
"off"
/><button>
Send
</button>
</form>
</form>
<form
id=
"form_private"
action=
""
>
<input
id=
"input_private"
autocomplete=
"off"
/>
<input
id=
"input_private_reciever"
autocomplete=
"off"
placeholder=
"Receiver"
/>
<button>
Send private Message
</button>
</form>
<input
id=
"file"
type=
"file"
/>
<input
id=
"file"
type=
"file"
/>
<script
src=
"/socket.io/socket.io.js"
></script>
<script
src=
"/socket.io/socket.io.js"
></script>
<script>
<script>
...
@@ -35,15 +49,46 @@
...
@@ -35,15 +49,46 @@
var
activeUsers
=
document
.
getElementById
(
'
activeUsers
'
);
var
activeUsers
=
document
.
getElementById
(
'
activeUsers
'
);
var
messages
=
document
.
getElementById
(
'
messages
'
);
var
messages
=
document
.
getElementById
(
'
messages
'
);
var
form
=
document
.
getElementById
(
'
form
'
);
var
form
=
document
.
getElementById
(
'
form
'
);
var
form_private
=
document
.
getElementById
(
'
form_private
'
);
var
input
=
document
.
getElementById
(
'
input
'
);
var
input
=
document
.
getElementById
(
'
input
'
);
var
fileUpload
=
document
.
getElementById
(
"
file
"
);
var
fileUpload
=
document
.
getElementById
(
"
file
"
);
var
receiver
=
document
.
getElementById
(
"
input_private_reciever
"
);
var
input_private
=
document
.
getElementById
(
"
input_private
"
);
var
username
=
getCookie
(
"
username
"
);
var
username
=
getCookie
(
"
username
"
);
socket
.
emit
(
'
userLogin
'
,
{
socket
.
emit
(
'
userLogin
'
,
{
name
:
username
name
:
username
});
});
form_private
.
addEventListener
(
'
submit
'
,
function
(
e
){
e
.
preventDefault
();
if
(
input_private
.
value
&&
receiver
.
value
){
socket
.
emit
(
"
private message
"
,
{
private_msg
:
input_private
.
value
,
receiver
:
receiver
.
value
,
name
:
username
});
input_private
.
value
=
''
;
receiver
.
value
=
''
;
input_private
.
focus
;
receiver
.
focus
;
}
})
socket
.
on
(
username
,
function
(
response
){
console
.
log
(
"
Private Message:
"
,
response
.
private_msg
,
"
From:
"
,
response
.
sender
);
/*
var item = document.createElement('li');
var nameMsg = name + ": " + msg + " --" + dateTime;
item.textContent = nameMsg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight); */
})
//Event Listener to trigger emit function. Sends 'chat message' event and Message to Server
//Event Listener to trigger emit function. Sends 'chat message' event and Message to Server
form
.
addEventListener
(
'
submit
'
,
function
(
e
)
{
form
.
addEventListener
(
'
submit
'
,
function
(
e
)
{
e
.
preventDefault
();
e
.
preventDefault
();
...
@@ -51,7 +96,6 @@
...
@@ -51,7 +96,6 @@
upload
(
fileUpload
.
files
);
upload
(
fileUpload
.
files
);
}
}
if
(
input
.
value
)
{
if
(
input
.
value
)
{
var
username
=
getUsernameFromCookie
();
socket
.
emit
(
"
chat message
"
,
socket
.
emit
(
"
chat message
"
,
{
msg
:
input
.
value
,
{
msg
:
input
.
value
,
name
:
username
name
:
username
...
@@ -68,10 +112,6 @@
...
@@ -68,10 +112,6 @@
var
date
=
response
.
dateTime
;
var
date
=
response
.
dateTime
;
var
dateTime
=
date
.
split
(
"
"
)[
0
];
var
dateTime
=
date
.
split
(
"
"
)[
0
];
console
.
log
(
"
index.html msg:
"
,
msg
);
console
.
log
(
"
Index.html name:
"
,
name
);
console
.
log
(
"
Index.html dateTime:
"
,
dateTime
);
var
item
=
document
.
createElement
(
'
li
'
);
var
item
=
document
.
createElement
(
'
li
'
);
var
nameMsg
=
name
+
"
:
"
+
msg
+
"
--
"
+
dateTime
;
var
nameMsg
=
name
+
"
:
"
+
msg
+
"
--
"
+
dateTime
;
item
.
textContent
=
nameMsg
;
item
.
textContent
=
nameMsg
;
...
...
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
sign in
to comment