You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.6 KiB
JavaScript

const admin_chat_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
const admin_chat_domain = "osina.ositcom.com";
let chatWebSocket = null;
function openConversation(chatId) {
fetch(`/chat-rooms/${chatId}/`)
.then(response => response.text())
.then(html => {
const conversationContainer = document.getElementById('inner-conversation')
conversationContainer.innerHTML = html;
const guestSessionId = document.getElementById('sessionid').textContent.trim();
const userId = document.getElementById('userId').textContent.trim();
// Close the previous WebSocket connection if it exists
if (chatWebSocket) {
chatWebSocket.close();
}
chatWebSocket = new WebSocket(`${admin_chat_ws_scheme}://${admin_chat_domain}/ws/osichat/${guestSessionId}/`);
chatWebSocket.onopen = function () {
console.log('WebSocket connection to osichat established');
chatWebSocket.send(JSON.stringify({ 'event_type': 'load_chat', 'client_type': 'website_admin' }));
};
const sendMessageForm = document.querySelector('#sendMessage');
sendMessageForm.addEventListener('submit', function (event) {
event.preventDefault();
const message = event.target.elements.message.value;
const eventMessage = {
'event_type': 'send_message',
'message': message,
'user_id': userId
};
chatWebSocket.send(JSON.stringify(eventMessage));
event.target.reset();
});
chatWebSocket.onclose = function () {
console.log('WebSocket connection to osichat closed');
};
chatWebSocket.onerror = function (error) {
console.error('WebSocket error:', error);
};
appendTextAreaScript(admin_chat_domain, conversationContainer);
})
.catch(error => console.error('Error loading conversation details:', error));
}
function appendTextAreaScript(domain, conversationContainer) {
// Check if the script is already appended
if (!document.querySelector('script[src="https://' + domain + '/static/js/osichat-admin/textarea.js"]')) {
const textareaScript = document.createElement('script');
textareaScript.type = 'text/javascript';
textareaScript.src = `https://${domain}/static/js/osichat-admin/textarea.js`;
conversationContainer.appendChild(textareaScript);
}
}