forked from OWASP-BLT/BLT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57403a9
commit 05e94d5
Showing
12 changed files
with
194 additions
and
195 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1373,4 +1373,3 @@ class Room(models.Model): | |
|
||
def __str__(self): | ||
return self.name | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{% extends "base.html" %} | ||
{% block title %}{{ room.name }}{% endblock title %} | ||
{% block content %} | ||
{% include "includes/sidenav.html" %} | ||
<div class="max-w-4xl mx-auto py-8"> | ||
<h1 class="text-3xl font-bold text-gray-900">{{ room.name }}</h1> | ||
<div id="log-history" class="mt-4 p-4 bg-gray-100 rounded-lg h-96 overflow-y-auto"> | ||
<!-- Messages will be appended here --> | ||
</div> | ||
<input id="message-field" type="text" placeholder="Type a message..." | ||
class="border rounded-lg px-4 py-2 w-full" /> | ||
<button id="send-button" | ||
class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg mt-2"> | ||
Send | ||
</button> | ||
</div> | ||
<script> | ||
const roomId = {{ room.id }}; | ||
const username = "{{ request.user.username|escapejs }}"; // Add user's name | ||
let socket; | ||
|
||
const protocol = window.location.protocol === "https:" ? "wss://" : "ws://"; | ||
const wsUrl = `${protocol}${window.location.host}/ws/discussion-rooms/chat/${roomId}/`; | ||
|
||
const sendButton = document.getElementById("send-button"); | ||
const messageInput = document.getElementById('message-field'); | ||
const chatLog = document.getElementById('log-history'); | ||
|
||
// Connect to WebSocket server | ||
function connectWebSocket() { | ||
return new Promise((resolve, reject) => { | ||
socket = new WebSocket(wsUrl); | ||
|
||
socket.onopen = () => { | ||
resolve(); // Resolve the promise when the connection is established | ||
}; | ||
|
||
socket.onmessage = (event) => { | ||
const data = JSON.parse(event.data); | ||
handleSocketMessage(data); | ||
}; | ||
|
||
socket.onerror = (error) => { | ||
reject(error); // Reject the promise if there's an error | ||
}; | ||
|
||
socket.onclose = () => { | ||
console.error('Chat socket closed unexpectedly'); | ||
}; | ||
}); | ||
} | ||
|
||
// Handle WebSocket messages | ||
function handleSocketMessage(data) { | ||
const newMessage = document.createElement('div'); | ||
newMessage.innerHTML = `<strong>${data.username}:</strong> ${data.message}`; | ||
newMessage.classList.add('py-1', 'px-2', 'bg-white', 'rounded-lg', 'mb-2', 'shadow-sm'); | ||
chatLog.appendChild(newMessage); | ||
chatLog.scrollTop = chatLog.scrollHeight; // Auto-scroll to the bottom | ||
} | ||
|
||
// Send message through WebSocket | ||
function sendMessage() { | ||
const message = messageInput.value; | ||
if (message.trim() !== "") { | ||
const data = { | ||
'type': 'message', | ||
'message': message, | ||
'username': username | ||
}; | ||
socket.send(JSON.stringify(data)); | ||
// handleSocketMessage(data); // Display the message for the sender | ||
messageInput.value = ''; | ||
} | ||
} | ||
|
||
// Event listener for send button | ||
sendButton.addEventListener('click', sendMessage); | ||
|
||
// Connect to WebSocket when the page loads | ||
connectWebSocket().catch(error => { | ||
console.error('WebSocket connection failed:', error); | ||
}); | ||
</script> | ||
{% endblock content %} |
Oops, something went wrong.