chookchat/client-web/index.js

176 lines
5.6 KiB
JavaScript
Raw Normal View History

2024-11-23 18:10:56 +11:00
alert("Chookchat is currently in early development, expect bugs! Please don't try breaking the public server, do that with your own test server (read more in the Git repo). Thanks for trying Chookchat!")
const width = $(document).width();
if (width < 512) {
console.log("enlarging");
const loginDiv = document.getElementById('login');
if (loginDiv) {
console.log(loginDiv.style.width = `${width}px`);
console.log("enlarged?");
}
}
2024-11-05 20:35:10 +11:00
let ws;
let username;
let password;
2024-11-23 18:10:56 +11:00
function resizeMessaging() {
const messagingDiv = document.getElementById('messaging');
if (messagingDiv) {
messagingDiv.style.width = `${window.innerWidth - 40}px`; // -40 for body margins
messagingDiv.style.height = `${window.innerHeight - 40}px`; // -40 for body margins
}
}
// Call it initially
resizeMessaging();
// Add resize listener to handle window resizing
window.addEventListener('resize', resizeMessaging);
2024-11-05 20:35:10 +11:00
function showConfig() {
const serverconfig = document.getElementById('serverconfig')
if (serverconfig) {
serverconfig.style.display = 'block';
}
}
function md5(string) {
return CryptoJS.MD5(string).toString();
}
function getUrl() {
const serverUrl = document.getElementById('serverUrl').value.trim();
const serverPort = document.getElementById('serverPort').value;
const useWss = document.getElementById('securityStatus').checked;
const protocol = useWss ? 'wss' : 'ws';
const cleanUrl = serverUrl.replace(/^(https?:\/\/|wss?:\/\/)/, '');
return `${protocol}://${cleanUrl}:${serverPort}/api/websocket`;
}
function getSignupUrl() {
const serverUrl = document.getElementById('serverUrl').value.trim();
const serverPort = document.getElementById('serverPort').value;
const useWss = document.getElementById('securityStatus').checked;
const protocol = useWss ? 'https' : 'http';
const cleanUrl = serverUrl.replace(/^(https?:\/\/|wss?:\/\/)/, '');
return `${protocol}://${cleanUrl}:${serverPort}/api/createaccount/`;
}
function connect() {
username = document.getElementById('username').value;
password = document.getElementById('password').value;
if (!username || !password) {
alert('Please enter a username and password');
return;
}
const wsUrl = getUrl();
if (ws) {
ws.close();
}
ws = new WebSocket(wsUrl);
2024-11-23 18:10:56 +11:00
var incorrectDetail = 0;
2024-11-05 20:35:10 +11:00
ws.onopen = () => {
2024-11-24 14:13:05 +11:00
if (typeof Notification !== "undefined") {
Notification.requestPermission();
}
2024-11-05 20:35:10 +11:00
console.log('Connected!');
document.getElementById('login').style.display = 'none';
document.getElementById('messaging').style.display = 'block';
2024-11-23 18:10:56 +11:00
const connectMessage = {
"type": "connect",
"username": username,
"token": md5(password),
"content": `${username} joined the room!`
}
ws.send(JSON.stringify(connectMessage));
2024-11-05 20:35:10 +11:00
ws.onmessage = (event) => {
if (event.data === "ping") {
ws.send("pong");
return;
}
2024-11-23 18:10:56 +11:00
const message = JSON.parse(event.data);
if (message.type == "error") {
if (message.username == "system") {
if (message.content == "invalid-token") {
alert("Your password is incorrect! Please try putting in your password right.");
incorrectDetail = 1;
location.reload();
}
if (message.content == "unknown-account") {
alert("That username isn't on the server. Maybe try registering?");
incorrectDetail = 1;
location.reload();
}
}
}
2024-11-05 20:35:10 +11:00
const messagesDiv = document.getElementById('messagebox');
const messageElement = document.createElement('div');
if (messageElement) {
if (messagesDiv) {
messagesDiv.appendChild(messageElement);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
messageElement.className = 'message';
2024-11-23 18:10:56 +11:00
messageElement.textContent = `${message.username}: ${message.content}` ;
2024-11-05 20:35:10 +11:00
}
}
if (document.hidden) {
2024-11-23 18:10:56 +11:00
const notifiction = new Notification("Chookchat", {body: messageElement.textContent});
2024-11-05 20:35:10 +11:00
}
};
}
2024-11-23 18:10:56 +11:00
ws.onclose = () => {
alert("Chookchat has disconnected :/ Refresh the page to try again");
}
2024-11-05 20:35:10 +11:00
}
function sendMessage() {
const messageInput = document.getElementById('messageInput');
const message = messageInput.value.trim();
2024-11-23 18:10:56 +11:00
const processedMessage = {
"type": "message",
"username": username,
"token": md5(password),
"content": message
}
if (processedMessage && ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(processedMessage));
2024-11-05 20:35:10 +11:00
messageInput.value = '';
}
}
document.getElementById('messageInput').addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
sendMessage();
}
});
document.getElementById('password').addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
connect();
}
});
function register() {
username = document.getElementById('username').value;
password = document.getElementById('password').value;
if (!username || !password) {
alert('Please enter a username and password');
return;
}
window.open(`${getSignupUrl()}username:{${username}}token:{${md5(password)}}`);
}