Compare commits

..

2 Commits

Author SHA1 Message Date
Angelus Vela 2b49232e6d New Chat Web 4 weeks ago
Angelus Vela 5b623c8a9c index.html 4 weeks ago

@ -1,8 +1,40 @@
<!DOCTYPE HTML>
<html> <html>
<head>
<title>Chat App</title>
</head>
<body> <body>
<h1> Hello Angelus from index.html file </h1> <h1> A new Chat App </h1>
<ul id='messages'></ul>
<form id='form'>
<input id='input'autocomplete='off'/><button type='submit'>Send</button>
</form>
<p>This is some text </p> <p>This is some text </p>
<script src='/socket.io/socket.io.js'></script> <script src='/socket.io/socket.io.js'></script>
<script>var socket = io();</script> <script>
const socket = io();
const form = document.getElementById('form');
const input = document.getElementById('input');
const messages = document.getElementById('messages');
// send message
form.addEventListener('submit', function(e) {
e.preventDefault();
if(input.value) {
socket.emit('chat message',input.value);
input.value = '';
}
});
//receive message
socket.on('chat message', function(msg){
const item = document.createElement('li');
item.textContent = msg;
messages.appendChild(item);
});
</script>
</body> </body>
</html> </html>

@ -12,6 +12,15 @@ app.get('/', (req, res) => {
io.on('connection',(socket) => { io.on('connection',(socket) => {
console.log ('A new user is connected'); console.log ('A new user is connected');
//disconect
socket.on('disconnect',() => {
console.log("A user has been disconnect");
});
// more functions
socket.on('chat message', (msg) => {
console.log(msg);
io.emit('chat message', msg);
});
}); });
server.listen(3333, () => { server.listen(3333, () => {

@ -1,10 +1,12 @@
{ {
"name": "nodejs-first-example", "name": "A chat application",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "A simple chat application using express and socket.io",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
@ -12,5 +14,8 @@
"dependencies": { "dependencies": {
"express": "^4.22.3", "express": "^4.22.3",
"socket.io": "^4.8.4" "socket.io": "^4.8.4"
},
"devDependencies": {
"nodemon": "^3.1.14"
} }
} }

Loading…
Cancel
Save