Compare commits

..

2 Commits
main ... events

3
.gitignore vendored

@ -0,0 +1,3 @@
node_modules/
package-lock.json
*.*.swp

@ -1,14 +1,14 @@
<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
<title>Chat App</title>
<title>Socket.io Chat</title>
</head>
<body>
<h1>A new Chat App</h1>
<h1> My Chat App</h1>
<ul id='messages'></ul>
<form id='form'>
<input id='input' autocomplete='off'/><button type='submit'>Send</button>
<input id='input' autocomplete='off'/> <button type='submit'>Send</button>
</form>
<script src='/socket.io/socket.io.js'></script>
@ -18,16 +18,17 @@
const input = document.getElementById('input');
const messages = document.getElementById('messages');
// send message
// Send messages
form.addEventListener('submit', function(e) {
e.preventDefault();
if(input.value) {
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});
// receive message
// Receive messages
socket.on('chat message', function(msg) {
const item = document.createElement('li');
item.textContent = msg;

@ -13,13 +13,12 @@ app.get('/', (req, res) => {
io.on('connection', (socket) => {
console.log('A new user is connected');
// disconnect
socket.on('disconnect', () => {
console.log("A user has been disconnect");
socket.on('disconnect', ()=> {
console.log('User disconnected');
});
// more functions
socket.on('chat message', (msg) => {
console.log(msg);
console.log('Received:', msg);
io.emit('chat message', msg);
});
});

@ -1,7 +1,7 @@
{
"name": "A chat application",
"name": "nodejs-first-example",
"version": "1.0.0",
"description": "A simpel chat application using express and socket.io",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",

Loading…
Cancel
Save