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.
53 lines
1.8 KiB
JavaScript
53 lines
1.8 KiB
JavaScript
const admin_chat_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
|
const admin_chat_domain = "192.168.1.106:8000";
|
|
let chatWebSocket = null;
|
|
|
|
function openConversation(chatId) {
|
|
fetch(`/conversations/${chatId}/`)
|
|
.then(response => response.text())
|
|
.then(html => {
|
|
document.getElementById('inner-conversation').innerHTML = html;
|
|
const guestSessionId = document.getElementById('sessionid').textContent;
|
|
// 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');
|
|
};
|
|
|
|
|
|
|
|
sendMessage.addEventListener('submit', function (event) {
|
|
event.preventDefault();
|
|
|
|
const message = event.target.elements.message.value;
|
|
const user_id = event.target.elements.user_id.value;
|
|
|
|
const eventMessage = {
|
|
'event_type': 'send_message',
|
|
'message': message,
|
|
'user_id': user_id
|
|
};
|
|
|
|
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);
|
|
};
|
|
})
|
|
.catch(error => console.error('Error loading conversation details:', error));
|
|
}
|