-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
26 lines (22 loc) · 863 Bytes
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const messageInput = document.getElementById('message-input');
const sendButton = document.getElementById('send-button');
const chatMessages = document.querySelector('.chat-messages');
sendButton.addEventListener('click', () => {
const message = messageInput.value.trim();
if (message !== '') {
addMessage('user', message);
messageInput.value = '';
}
});
function addMessage(type, message) {
const messageElement = document.createElement('div');
messageElement.classList.add('chat-message', type);
messageElement.textContent = message;
chatMessages.appendChild(messageElement);
chatMessages.scrollTop = chatMessages.scrollHeight; // Scroll to bottom
}
// server.js
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);