-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
167 lines (157 loc) · 5 KB
/
index.html
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Whatsapp Chat</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<script src="/socket.io/socket.io.js"></script>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f8f9fa;
}
.chat-container {
width: 90%;
max-width: 600px;
background: #333;
color: white;
border-radius: 10px;
overflow: hidden;
display: flex;
flex-direction: column;
height: 90vh;
}
.chat-box {
flex-grow: 1;
overflow-y: auto;
padding: 10px;
background: #222;
}
.message {
padding: 10px;
margin: 5px;
border-radius: 5px;
max-width: 75%;
}
.message.right {
align-self: flex-end;
background: #0d6efd;
color: white;
}
.message.left {
align-self: flex-start;
background: #f8f9fa;
color: black;
}
.chat-input {
display: flex;
padding: 10px;
background: rgba(0, 0, 0, 0.15);
}
.chat-input input {
flex-grow: 1;
margin-right: 10px;
border-radius: 20px;
border: none;
padding: 10px;
}
.chat-input button {
border-radius: 5px;
}
</style>
</head>
<body>
<div class="chat-container">
<div id="chatBox" class="chat-box"></div>
<div class="chat-input">
<input
id="input"
type="text"
class="form-control"
placeholder="Type a message..."
/>
<input type="file" id="file" class="form-control-file" />
<button id="sendBtn" class="btn btn-primary">Send</button>
</div>
</div>
<script>
let socket = io();
let name = `user${Math.floor(Math.random() * 1000)}`;
const chatBox = document.getElementById("chatBox");
const input = document.getElementById("input");
const fileInput = document.getElementById("file");
const sendBtn = document.getElementById("sendBtn");
input.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
event.preventDefault(); // Prevents line break in input field
sendBtn.click(); // Trigger send button click event
}
});
// Auto-scroll function
function scrollToBottom() {
chatBox.scrollTop = chatBox.scrollHeight;
}
// Convert file to Base64
function convertFileToBase64(file, callback) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => callback(reader.result);
reader.onerror = (error) => console.log("Error: ", error);
}
// Add a new message to the chat
function addMessage(msg, type) {
let messageDiv = document.createElement("div");
messageDiv.className = `message ${type}`;
messageDiv.innerHTML = `<strong>${msg.name}</strong>: ${msg.message}`;
// If the message contains a file
if (msg.fileData && msg.fileName) {
let fileLink = document.createElement("a");
fileLink.href = msg.fileData;
fileLink.download = msg.fileName;
fileLink.innerHTML = `<br><button class='btn btn-sm btn-light mt-1'>Download ${msg.fileName}</button>`;
messageDiv.appendChild(fileLink);
}
chatBox.appendChild(messageDiv);
scrollToBottom();
}
// Send message on button click
sendBtn.addEventListener("click", () => {
if (input.value.trim() || fileInput.files.length > 0) {
let msg = {
name: name,
message: input.value,
fileData: null, // Will be set if a file is uploaded
fileName: null,
};
// If a file is selected, convert it to Base64 and send
if (fileInput.files.length > 0) {
let file = fileInput.files[0];
convertFileToBase64(file, (base64Data) => {
msg.fileData = base64Data;
msg.fileName = file.name;
socket.emit("chat-message", msg);
addMessage(msg, "right");
});
} else {
socket.emit("chat-message", msg);
addMessage(msg, "right");
}
input.value = "";
fileInput.value = "";
}
});
// Listen for messages from other users
socket.on("chat", (msg) => {
addMessage(msg, "left");
});
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>