Mercurial Hosting > chat
diff src/chat.js @ 12:9f45d32670ae
server push
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 31 Oct 2024 21:40:57 -0600 |
parents | 563a5358f2ee |
children | 0df3a63a895f |
line wrap: on
line diff
--- a/src/chat.js Thu Oct 31 19:17:53 2024 -0600 +++ b/src/chat.js Thu Oct 31 21:40:57 2024 -0600 @@ -1,6 +1,7 @@ 'use strict'; let currentChatId = null; +let eventSource; function selectChat(div) { let chatId = div.getAttribute('chat'); @@ -11,6 +12,19 @@ div.setAttribute('selected',''); ajax(`get_chat.js?chat=${chatId}`); currentChatId = chatId; + + if(eventSource) eventSource.close(); + eventSource = new EventSource(`${location.origin}/chat/${chatId}`); + eventSource.onmessage = function(event) { + eval(event.data); + }; +} + +function gotChat(html) { + document.querySelector('div[posts]').innerHTML = html; + fixDates(); + document.querySelector('div[input] textarea').focus(); + document.querySelector('div[input]').scrollIntoView({block: 'end'}); } function fixTextarea(event) { @@ -29,7 +43,6 @@ return; ajax(`add_post.js?chat=${currentChatId}&content=${encodeURIComponent(text)}`); textarea.value = ''; - console.log('addPost'); } function textareaKey(event) { @@ -50,3 +63,9 @@ function deleteChat() { ajax(`delete_chat.js?chat=${currentChatId}`); } + +function added(html) { + let input = document.querySelector('div[input]'); + input.insertAdjacentHTML('beforebegin',html); + input.scrollIntoView({block: 'end'}); +}