98 lines
3.4 KiB
HTML
98 lines
3.4 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<link rel="icon" href="Dangrain.ico" type="image/x-icon">
|
|
<li><a href="Redditlike.HTML">Home</a></li>
|
|
<title>Chatroom</title>
|
|
|
|
<style>
|
|
#chatbox {
|
|
width: 1280px;
|
|
height: 720px;
|
|
border: 10px solid #ccc;
|
|
overflow-y: scroll;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="chatbox"></div>
|
|
<input type="text" id="username" placeholder="Your Name">
|
|
<input type="text" id="message" placeholder="Type your message">
|
|
<button id="send">Send</button>
|
|
|
|
<script>
|
|
const chatbox = document.getElementById('chatbox');
|
|
const usernameInput = document.getElementById('username');
|
|
const messageInput = document.getElementById('message');
|
|
const sendButton = document.getElementById('send');
|
|
|
|
let username = localStorage.getItem('username') || '';
|
|
usernameInput.value = username;
|
|
|
|
const socket = new WebSocket('wss://ws.postman-echo.com/raw');
|
|
|
|
socket.addEventListener('open', () => {
|
|
sendButton.disabled = false;
|
|
});
|
|
|
|
socket.addEventListener('message', event => {
|
|
const message = event.data;
|
|
appendMessageToChatbox(message);
|
|
});
|
|
|
|
messageInput.addEventListener('keydown', event => {
|
|
if (event.key === 'Enter') {
|
|
sendMessage();
|
|
}
|
|
});
|
|
|
|
sendButton.addEventListener('click', sendMessage);
|
|
|
|
function sendMessage() {
|
|
username = usernameInput.value;
|
|
localStorage.setItem('username', username);
|
|
const message = messageInput.value;
|
|
|
|
if (username.trim() === '' || message.trim() === '') return;
|
|
|
|
if (socket.readyState === WebSocket.OPEN) {
|
|
const data = {
|
|
username: username,
|
|
message: message
|
|
};
|
|
|
|
socket.send(JSON.stringify(data));
|
|
messageInput.value = '';
|
|
}
|
|
}
|
|
|
|
function appendMessageToChatbox(data) {
|
|
const messageElement = document.createElement('div');
|
|
const usernameElement = document.createElement('strong');
|
|
|
|
const messageData = JSON.parse(data);
|
|
|
|
usernameElement.textContent = messageData.username + ': ';
|
|
|
|
const messageWithLinks = convertURLsToLinks(messageData.message);
|
|
const messageContentElement = document.createElement('div');
|
|
messageContentElement.innerHTML = messageWithLinks;
|
|
|
|
messageElement.appendChild(usernameElement);
|
|
messageElement.appendChild(messageContentElement);
|
|
|
|
chatbox.appendChild(messageElement);
|
|
chatbox.scrollTop = chatbox.scrollHeight;
|
|
}
|
|
|
|
function convertURLsToLinks(text) {
|
|
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
|
return text.replace(urlRegex, (url) => {
|
|
return `<a href="${url}" target="_blank">${url}</a>`;
|
|
});
|
|
}
|
|
</script>
|
|
<h1>This worked so far, but It had to have It's websocket replaced every month, since Piesocket works that way.</h1>
|
|
<h1>I am going to fix this as soon as I replace my Raspberry Pi zero, since this one's WiFi antenna suddenly died</h1>
|
|
</body>
|
|
</html>
|