document.addEventListener('DOMContentLoaded', function (event) { console.log('chat init'); let location = window.location; let wsStart = 'ws://'; if (location.protocol === 'https') { wsStart = 'wss://'; } var currentUrl = location.href; var urlParts = currentUrl.split('/'); var otherUserId = urlParts[urlParts.length - 2]; console.log(otherUserId); const urlChat = wsStart + location.host + `/ws/chat/` + otherUserId + '/'; const requestUser = JSON.parse( document.getElementById('request-user').textContent ); const chatSocket = new WebSocket(urlChat); chatSocket.onopen = async function (e) { console.log('open', e); }; // when data is received chatSocket.onmessage = async function (e) { console.log('message', e); const data = JSON.parse(e.data); const chat = document.getElementById('chat-messages'); const dateOptions = { hour: 'numeric', minute: 'numeric', hour12: true }; const datetime = new Date(data.datetime).toLocaleString('en', dateOptions); const isMe = data.sender === requestUser; const source = isMe ? 'chat-message-right' : ' chat-message-left'; const name = isMe ? 'Me' : data.fullName; chat.innerHTML += `