Typing indicators, online users

This commit is contained in:
Maxwell Jeffress 2024-11-24 16:07:45 +11:00
parent 78b885b22e
commit dae4bb380f
3 changed files with 118 additions and 0 deletions

View File

@ -206,3 +206,12 @@ input {
margin: 0; /* Remove default margins */ margin: 0; /* Remove default margins */
white-space: nowrap; /* Prevent button text from wrapping */ white-space: nowrap; /* Prevent button text from wrapping */
} }
.suttle {
text-align: left;
width: 100%;
padding-left: 10px;
margin-bottom: 5px;
font-size: 0.9em;
color: rgba(255, 255, 255, 0.7); /* Makes it slightly subtle */
}

View File

@ -32,7 +32,9 @@
</div> </div>
<div class="hidden" id="messaging"> <div class="hidden" id="messaging">
<div class="box"> <div class="box">
<div id="users" class="suttle"></div>
<div id="messagebox" class="box" style="height: 600px;"><div></div></div> <div id="messagebox" class="box" style="height: 600px;"><div></div></div>
<div id="typing" class="suttle"></div>
<div class="input-container"> <div class="input-container">
<input type="text" id="messageInput" placeholder="Send a message..."> <input type="text" id="messageInput" placeholder="Send a message...">
<button onclick="sendMessage()" class="bluebutton">Send</button> <button onclick="sendMessage()" class="bluebutton">Send</button>

View File

@ -14,6 +14,8 @@ if (width < 512) {
let ws; let ws;
let username; let username;
let password; let password;
let typingTimeout;
let typingPeople = new Array();
function resizeMessaging() { function resizeMessaging() {
const messagingDiv = document.getElementById('messaging'); const messagingDiv = document.getElementById('messaging');
@ -93,6 +95,7 @@ function connect() {
"content": `${username} joined the room!` "content": `${username} joined the room!`
} }
ws.send(JSON.stringify(connectMessage)); ws.send(JSON.stringify(connectMessage));
console.log(typingPeople);
ws.onmessage = (event) => { ws.onmessage = (event) => {
if (event.data === "ping") { if (event.data === "ping") {
ws.send("pong"); ws.send("pong");
@ -111,8 +114,39 @@ function connect() {
incorrectDetail = 1; incorrectDetail = 1;
location.reload(); location.reload();
} }
if (message.content == "banned") {
alert("kiddo you're banned lol what did you do to get banned lmaooo");
incorrectDetail = 1;
location.reload();
}
} }
} }
if (message.type == "typing" && message.content == "1") {
console.log(`${message.username} is typing`);
if (username !== message.username && !typingPeople.includes(message.username)) {
typingPeople.push(message.username);
console.log(typingPeople);
updatePeopleTyping();
}
return;
}
if (message.type == "typing" && message.content == "0") {
console.log(`${message.username} is no longer typing`)
if (username !== message.username && typingPeople.includes(message.username)) {
const index = typingPeople.indexOf(message.username);
typingPeople.splice(index, 1);
console.log(typingPeople);
updatePeopleTyping();
}
return;
}
if (message.type == "users" && message.username == "system") {
usersDiv = document.getElementById("users");
if (usersDiv) {
usersDiv.textContent = `Online users: ${message.content}`
}
return;
}
const messagesDiv = document.getElementById('messagebox'); const messagesDiv = document.getElementById('messagebox');
const messageElement = document.createElement('div'); const messageElement = document.createElement('div');
if (messageElement) { if (messageElement) {
@ -147,6 +181,28 @@ function sendMessage() {
if (processedMessage && ws && ws.readyState === WebSocket.OPEN) { if (processedMessage && ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(processedMessage)); ws.send(JSON.stringify(processedMessage));
messageInput.value = ''; messageInput.value = '';
if (typingTimeout) {
clearTimeout(typingTimeout);
}
const stoppedTypingMessage = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "0"
};
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(stoppedTypingMessage));
}
}
const processedMessage2 = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "0"
}
if (processedMessage && ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(processedMessage2));
} }
} }
@ -173,3 +229,54 @@ function register() {
window.open(`${getSignupUrl()}username:{${username}}token:{${md5(password)}}`); window.open(`${getSignupUrl()}username:{${username}}token:{${md5(password)}}`);
} }
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function startTypingIndicator() {
if (typingTimeout) {
clearTimeout(typingTimeout);
}
const typingMessage = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "1"
};
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(typingMessage));
}
typingTimeout = setTimeout(() => {
const stoppedTypingMessage = {
"type": "typing",
"username": username,
"token": md5(password),
"content": "0"
};
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(stoppedTypingMessage));
}
}, 5000);
}
function updatePeopleTyping() {
const typingDiv = document.getElementById('typing');
if (typingDiv) {
if (typingPeople.length === 0) {
typingDiv.textContent = '';
} else if (typingPeople.length === 1) {
typingDiv.textContent = `${typingPeople[0]} is typing...`;
} else if (typingPeople.length === 2) {
typingDiv.textContent = `${typingPeople[0]} and ${typingPeople[1]} are typing...`;
} else {
typingDiv.textContent = `${typingPeople.length} people are typing...`;
}
}
}
document.getElementById('messageInput').addEventListener('input', startTypingIndicator);