From 482d01dc030dc76aca289a256d80e4dc87b4260c Mon Sep 17 00:00:00 2001 From: JISHNU PRAMOD <39820988+jishnupramod@users.noreply.github.com> Date: Sat, 4 Sep 2021 21:09:14 +0530 Subject: [PATCH] update main.js - add "user left" notification Similar to the notification message when a user joins the chat, when the user leaves the chat either by exiting the window or by refreshing the page, the "user left!" notification is displayed on the chat page. --- src/main/resources/static/js/main.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/resources/static/js/main.js b/src/main/resources/static/js/main.js index 503892a..3f84f6b 100644 --- a/src/main/resources/static/js/main.js +++ b/src/main/resources/static/js/main.js @@ -119,3 +119,14 @@ function getAvatarColor(messageSender) { usernameForm.addEventListener('submit', connect, true) messageForm.addEventListener('submit', send, true) + +// Notification message when user leaves the chat +window.onbeforeunload = sendLeaveMessage +function sendLeaveMessage() { + var chatMessage = { + sender: username, + type: 'LEAVE' + }; + stompClient.send("/app/chat.send", {}, JSON.stringify(chatMessage)); + return null +}