From 70b15ef04cdb3348e224801ab15bacb76bc5ebfb Mon Sep 17 00:00:00 2001 From: Arya Vohra Date: Mon, 1 Jun 2020 00:22:36 +0800 Subject: [PATCH] Added border colors for peer identification in chat and video, removing nicknames --- public/css/chat.css | 6 ++++++ public/js/chat.js | 47 ++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/public/css/chat.css b/public/css/chat.css index aef5584..cc49ab6 100644 --- a/public/css/chat.css +++ b/public/css/chat.css @@ -337,6 +337,9 @@ button:hover { justify-content: center; padding: 12px; max-width: 100%; + border-style: solid; + border-width: 2px; + border-color: var(--bloc-color); border-radius: 20px 20px 20px 5px; box-shadow: 6px 6px 12px #030506, -6px -6px 12px #23242a; } @@ -345,6 +348,9 @@ button:hover { #chat-zone .message-item.customer .message-bloc { background-color: rgb(47, 48, 52); color: #fff; + border-style: solid; + border-width: 2px; + border-color: var(--bloc-color); border-radius: 20px 20px 5px 20px; box-shadow: 6px 6px 12px #030506, -6px -6px 12px #22232a; } diff --git a/public/js/chat.js b/public/js/chat.js index 2233abb..1e6304c 100644 --- a/public/js/chat.js +++ b/public/js/chat.js @@ -30,9 +30,9 @@ const chatZone = $("#chat-zone"); var dataChannel = new Map(); var VideoChat = { - nickname: undefined, videoEnabled: true, audioEnabled: true, + borderColor: `hsl(${Math.floor(Math.random() * 360)}, 70%, 80%)`, connected: new Map(), localICECandidates: {}, socket: io(), @@ -97,17 +97,8 @@ var VideoChat = { }, }); - // Used to identify client to other peers in chats, captions etc. - VideoChat.nickname = prompt("Please enter a nickname", ""); - - while (VideoChat.nickname == null || VideoChat.nickname.trim() === "") { - VideoChat.nickname = prompt( - "Nickname cannot be empty. Please enter a nickname", - "" - ); - } - VideoChat.localVideo.srcObject = stream; + VideoChat.localVideo.style.border = `4px solid ${VideoChat.borderColor}`; // Now we're ready to join the chat room. VideoChat.socket.emit("join", roomHash); @@ -170,10 +161,6 @@ var VideoChat = { id: 0, }) ); - // Called when dataChannel is successfully opened - dataChannel.get(uuid).onopen = function (event) { - logIt("dataChannel opened"); - }; // Handle different dataChannel types dataChannel.get(uuid).onmessage = function (event) { const receivedData = event.data; @@ -186,8 +173,16 @@ var VideoChat = { recieveCaptions(cleanedMessage); } else if (dataType === "tog:") { toggleSendCaptions(); + } else if (dataType === "clr:") { + setStreamColor(uuid, cleanedMessage); } }; + // Called when dataChannel is successfully opened + dataChannel.get(uuid).onopen = function (event) { + logIt("dataChannel opened"); + // Sending our identifier color to the other peers + dataChannel.get(uuid).send("clr:" + VideoChat.borderColor); + }; // Set up callbacks for the connection generating iceCandidates or // receiving the remote media stream. Wrapping callback functions // to pass in the peer uuids. @@ -815,17 +810,16 @@ function recieveCaptions(captions) { // Text Chat // Add text message to chat screen on page -function addMessageToScreen(msg, isOwnMessage) { - // If nickname is undefined or null, user didn't input a nickname +function addMessageToScreen(msg, border, isOwnMessage) { if (isOwnMessage) { $(".chat-messages").append( - '
' + + `
` + msg + "
" ); } else { $(".chat-messages").append( - '
' + + `
` + msg + "
" ); @@ -843,9 +837,9 @@ chatInput.addEventListener("keypress", function (event) { // Make links clickable msg = msg.autoLink(); // Send message over data channel - sendToAllDataChannels("mes:" + VideoChat.nickname + ": " + msg); + sendToAllDataChannels("mes:" + VideoChat.borderColor + msg); // Add message to screen - addMessageToScreen(msg, true); + addMessageToScreen(msg, VideoChat.borderColor, true); // Auto scroll chat down chatZone.scrollTop(chatZone[0].scrollHeight); // Clear chat input @@ -855,8 +849,12 @@ chatInput.addEventListener("keypress", function (event) { // Called when a message is recieved over the dataChannel function handleRecieveMessage(msg) { + // Slice border color for message + const endIndex = msg.indexOf(')'); + const color = msg.slice(0, endIndex); + msg = msg.slice(endIndex+1, msg.length); // Add message to screen - addMessageToScreen(msg, false); + addMessageToScreen(msg, color, false); // Auto scroll chat down chatZone.scrollTop(chatZone[0].scrollHeight); // Show chat if hidden @@ -865,6 +863,11 @@ function handleRecieveMessage(msg) { } } +// Sets the border color of uuid's stream upon receiving color +function setStreamColor(uuid, color) { + document.querySelectorAll(`[uuid="${uuid}"]`)[0].style.border = `4px solid ${color}`; +} + // Show and hide chat function toggleChat() { var chatIcon = document.getElementById("chat-icon");