diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index bf1e6ead..e9b399ab 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc index d5c596bc..93158a61 100644 Binary files a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc and b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/routing.cpython-310.pyc b/osinaweb/osichat/__pycache__/routing.cpython-310.pyc index a81e08e1..50165609 100644 Binary files a/osinaweb/osichat/__pycache__/routing.cpython-310.pyc and b/osinaweb/osichat/__pycache__/routing.cpython-310.pyc differ diff --git a/osinaweb/osichat/consumers.py b/osinaweb/osichat/consumers.py index 3f121e10..f59178b1 100644 --- a/osinaweb/osichat/consumers.py +++ b/osinaweb/osichat/consumers.py @@ -9,13 +9,12 @@ import requests class OsitcomVisitor(WebsocketConsumer): def connect(self): - async_to_sync(self.channel_layer.group_add)( 'ositcom_visitors', self.channel_name ) self.accept() - self.visitor = None - + print('i am here') + def disconnect(self, close_code): if self.visitor: self.visitor.left_date = datetime.now() @@ -26,6 +25,7 @@ class OsitcomVisitor(WebsocketConsumer): def receive(self, text_data): text_data_json = json.loads(text_data) event_type = text_data_json.get('event_type') + print('333') if event_type == 'new_visitor': event = { 'type': 'new_visitor_handler', @@ -41,7 +41,8 @@ class OsitcomVisitor(WebsocketConsumer): ) def new_visitor_handler(self, event): - visitor = Visitor.objects.create( + print('hiiiiii') + self.visior = Visitor.objects.create( session_id = event['session_id'], ip_address = event['client_ip'], referrer = event['referrer'], @@ -49,7 +50,6 @@ class OsitcomVisitor(WebsocketConsumer): url = event['url'], visit_date = datetime.now(), ) - self.visitor = visitor @@ -57,13 +57,7 @@ class OsitcomVisitor(WebsocketConsumer): class OsitcomChatRoom(WebsocketConsumer): def connect(self): - cookie_header = self.scope.get('headers', {}) - http_cookie = dict(cookie_header).get(b'cookie', b'').decode('utf-8') - for cookie in http_cookie.split('; '): - key, value = cookie.split('=') - if key == 'sessionid': - self.session_id = value - break + self.session_id = self.scope['url_route']['kwargs']['session_id'] async_to_sync(self.channel_layer.group_add)( self.session_id, self.channel_name ) diff --git a/osinaweb/osichat/routing.py b/osinaweb/osichat/routing.py index 22ef7412..5439cef6 100644 --- a/osinaweb/osichat/routing.py +++ b/osinaweb/osichat/routing.py @@ -3,6 +3,6 @@ from .consumers import * websocket_urlpatterns = [ path("ws/osichat/visitors/", OsitcomVisitor.as_asgi()), - path("ws/osichat/", OsitcomChatRoom.as_asgi()), + path("ws/osichat//", OsitcomChatRoom.as_asgi()), ] \ No newline at end of file diff --git a/osinaweb/static/js/osichat/conversation.js b/osinaweb/static/js/osichat/conversation.js index 1508ba06..08ae3b92 100644 --- a/osinaweb/static/js/osichat/conversation.js +++ b/osinaweb/static/js/osichat/conversation.js @@ -1,88 +1,104 @@ - const chat_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; const domain = "192.168.1.106:8000"; -const osichatSocketUrl = `${chat_ws_scheme}://${domain}/ws/osichat/`; -const osichatSocket = new WebSocket(osichatSocketUrl); - - -osichatSocket.onopen = () => { - console.log('WebSocket connection to osichat established'); -}; - -osichatSocket.onmessage = function (e) { - const data = JSON.parse(e.data); - if (data.event_type === 'load_chat') { - const chatDiv = document.getElementById('osichat'); - const html = data.html; - chatDiv.innerHTML = html; - - // Append the toggle tag - const script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = `http://${domain}/static/js/osichat/chat-toggle.js`; - chatDiv.appendChild(script); - - - const startChatContainer = document.getElementById('startChat'); - // If this container exists then the template returned was start-conversation.html, no conversation yet. - if (startChatContainer) { - startChatContainer.addEventListener('submit', function (event) { - event.preventDefault(); - - const guestName = event.target.elements.guest_name.value; - const guestMobileNumber = event.target.elements.guest_mobile_number.value; - - const eventMessage = { - 'event_type': 'start_conversation', - 'guest_name': guestName, - 'guest_mobile_number': guestMobileNumber, - }; - - osichatSocket.send(JSON.stringify(eventMessage)); - - event.target.reset(); - }); - } - - const sendMessageContainer = document.getElementById('sendMessage'); - if (sendMessageContainer) { - sendMessageContainer.addEventListener('submit', function (event) { - event.preventDefault(); - console.log('i am here') - - const message = event.target.elements.message.value; - - const eventMessage = { - 'event_type': 'send_message', - 'message': message, - - }; - - osichatSocket.send(JSON.stringify(eventMessage)); - - event.target.reset(); - }); - } - - } else if (data.event_type === 'start_conversation') { - const chatDiv = document.getElementById('startChatContainer'); - const html = data.html; - chatDiv.innerHTML = html; - - } else if (data.event_type === 'send_message') { - const messagesDiv = document.getElementById('messages'); - const html = data.html; - messagesDiv.insertAdjacentHTML('beforeend', html); - +console.log('hi') +// Function to fetch session ID + + +async function fetchSessionID() { + try { + const response = await fetch('http://192.168.1.106:3000/get-client-session/'); + const data = await response.json(); + return data.session_id; + } catch (error) { + console.error('Error fetching session ID:', error); + return 'Unknown'; } -}; +} + +// Function to initialize WebSocket connection +async function initializeChatWebSocket() { + const session_id = await fetchSessionID(); + const osichatSocketUrl = `${chat_ws_scheme}://${domain}/ws/osichat/${session_id}/`; + const osichatSocket = new WebSocket(osichatSocketUrl); + + osichatSocket.onopen = () => { + console.log('WebSocket connection to osichat established'); + }; + + osichatSocket.onmessage = function (e) { + const data = JSON.parse(e.data); + if (data.event_type === 'load_chat') { + const chatDiv = document.getElementById('osichat'); + const html = data.html; + chatDiv.innerHTML = html; + + // Append the toggle tag + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = `http://${domain}/static/js/osichat/chat-toggle.js`; + chatDiv.appendChild(script); + + const startChatContainer = document.getElementById('startChat'); + // If this container exists then the template returned was start-conversation.html, no conversation yet. + if (startChatContainer) { + startChatContainer.addEventListener('submit', function (event) { + event.preventDefault(); + + const guestName = event.target.elements.guest_name.value; + const guestMobileNumber = event.target.elements.guest_mobile_number.value; + + const eventMessage = { + 'event_type': 'start_conversation', + 'guest_name': guestName, + 'guest_mobile_number': guestMobileNumber, + }; + + osichatSocket.send(JSON.stringify(eventMessage)); + + event.target.reset(); + }); + } + + const sendMessageContainer = document.getElementById('sendMessage'); + if (sendMessageContainer) { + sendMessageContainer.addEventListener('submit', function (event) { + event.preventDefault(); + + const message = event.target.elements.message.value; + + const eventMessage = { + 'event_type': 'send_message', + 'message': message, + }; + + osichatSocket.send(JSON.stringify(eventMessage)); + + event.target.reset(); + }); + } + + } else if (data.event_type === 'start_conversation') { + const chatDiv = document.getElementById('startChatContainer'); + const html = data.html; + chatDiv.innerHTML = html; + + } else if (data.event_type === 'send_message') { + const messagesDiv = document.getElementById('messages'); + const html = data.html; + messagesDiv.insertAdjacentHTML('beforeend', html); + } + }; -osichatSocket.onclose = () => { - console.log('WebSocket connection to osichat closed'); -}; + osichatSocket.onclose = () => { + console.log('WebSocket connection to osichat closed'); + }; -osichatSocket.onerror = (error) => { - console.log('WebSocket error:', error); -}; + osichatSocket.onerror = (error) => { + console.log('WebSocket error:', error); + }; +} +document.addEventListener('DOMContentLoaded', () => { + initializeChatWebSocket(); +}); diff --git a/osinaweb/static/js/osichat/visitors.js b/osinaweb/static/js/osichat/visitors.js index 594f8d7c..0f57261b 100644 --- a/osinaweb/static/js/osichat/visitors.js +++ b/osinaweb/static/js/osichat/visitors.js @@ -1,63 +1,66 @@ const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; -const my_domain = "osina.ositcom.com"; -const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`; - -document.addEventListener('DOMContentLoaded', () => { - function fetchClientData() { - return fetch('https://osina.ositcom.com/get-client-ip/') - .then(response => response.json()) - .then(data => ({ - client_ip: data.ip, - client_country: data.country - })) - .catch(error => { - console.error('Error fetching client data:', error); - return { - client_ip: 'Unknown', - client_country: 'Unknown' - }; - }); - } +const my_domain = "192.168.1.106:8000"; + +async function fetchClientData() { + try { + const response = await fetch('https://osina.ositcom.com/get-client-ip/'); + const data = await response.json(); + return { + client_ip: data.ip, + client_country: data.country + }; + } catch (error) { + console.error('Error fetching client data:', error); + return { + client_ip: 'Unknown', + client_country: 'Unknown' + }; + } +} - function fetchSessionID() { - return fetch('https://ositcom.com/get-client-session/') - .then(response => response.json()) - .then(data => data.session_id) - .catch(error => { - console.error('Error fetching session ID:', error); - return 'Unknown'; - }); +async function fetchSessionID() { + try { + const response = await fetch('http://192.168.1.106:3000/get-client-session/'); + const data = await response.json(); + return data.session_id; + } catch (error) { + console.error('Error fetching session ID:', error); + return 'Unknown'; } +} - function initializeWebSocket() { - const referrer = document.referrer; +async function initializeVisitorsWebSocket() { + console.log('Initializing WebSocket...'); - Promise.all([fetchClientData(), fetchSessionID()]).then(([clientData, session_id]) => { - const visitorsSocket = new WebSocket(visitorsSocketUrl); + const referrer = document.referrer; + const clientData = await fetchClientData(); + const session_id = await fetchSessionID(); + const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`; + const visitorsSocket = new WebSocket(visitorsSocketUrl); - visitorsSocket.onopen = () => { - console.log('WebSocket connection to visitors established'); + visitorsSocket.onopen = () => { + console.log('WebSocket connection to visitors established'); - const event_message = { - 'event_type': 'new_visitor', - 'referrer': referrer, - 'url': window.location.href, - 'client_ip': clientData.client_ip, - 'client_country': clientData.client_country, - 'session_id': session_id - }; - visitorsSocket.send(JSON.stringify(event_message)); - }; + const event_message = { + 'event_type': 'new_visitor', + 'referrer': referrer, + 'url': window.location.href, + 'client_ip': clientData.client_ip, + 'client_country': clientData.client_country, + 'session_id': session_id + }; + visitorsSocket.send(JSON.stringify(event_message)); + }; - visitorsSocket.onclose = () => { - console.log('WebSocket connection to visitors closed'); - }; + visitorsSocket.onclose = () => { + console.log('WebSocket connection to visitors closed'); + }; - visitorsSocket.onerror = (error) => { - console.error('WebSocket error:', error); - }; - }); + visitorsSocket.onerror = (error) => { + console.error('WebSocket error:', error); + }; } - initializeWebSocket(); -}); + document.addEventListener('DOMContentLoaded', async () => { + initializeVisitorsWebSocket(); + }); \ No newline at end of file