Created
September 25, 2022 16:46
-
-
Save percybolmer/49051b9644662385041c2eba10f8f139 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* routeEvent is a proxy function that routes | |
* events into their correct Handler | |
* based on the type field | |
* */ | |
function routeEvent(event) { | |
if (event.type === undefined) { | |
alert("no 'type' field in event"); | |
} | |
switch (event.type) { | |
case "new_message": | |
// Format payload | |
const messageEvent = Object.assign(new NewMessageEvent, event.payload); | |
appendChatMessage(messageEvent); | |
break; | |
default: | |
alert("unsupported message type"); | |
break; | |
} | |
} | |
/** | |
* appendChatMessage takes in new messages and adds them to the chat | |
* */ | |
function appendChatMessage(messageEvent) { | |
var date = new Date(messageEvent.sent); | |
// format message | |
const formattedMsg = `${date.toLocaleString()}: ${messageEvent.message}`; | |
// Append Message | |
textarea = document.getElementById("chatmessages"); | |
textarea.innerHTML = textarea.innerHTML + "\n" + formattedMsg; | |
textarea.scrollTop = textarea.scrollHeight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment