emile 10 months ago
parent 1cc3dc2c4a
commit ef7d33caa4

@ -3,50 +3,51 @@ const my_domain = "osina.ositcom.com";
const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`; const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`;
// Function to fetch client IP and country from the API // Function to fetch client IP and country from the API
function fetchClientData() { document.addEventListener('DOMContentLoaded', () => {
return fetch('https://osina.ositcom.com/get-client-ip/') function fetchClientData() {
.then(response => response.json()) return fetch('https://osina.ositcom.com/get-client-ip/')
.then(data => ({ .then(response => response.json())
client_ip: data.ip, .then(data => ({
client_country: data.country client_ip: data.ip,
})) client_country: data.country
.catch(error => { }))
console.error('Error fetching client data:', error); .catch(error => {
return { console.error('Error fetching client data:', error);
client_ip: 'Unknown', return {
client_country: 'Unknown' client_ip: 'Unknown',
}; client_country: 'Unknown'
}); };
} });
}
function initializeWebSocket() {
const referrer = document.referrer; function initializeWebSocket() {
const referrer = document.referrer;
// Fetch client data and then initialize WebSocket
fetchClientData().then(({ client_ip, client_country }) => { fetchClientData().then(({ client_ip, client_country }) => {
const visitorsSocket = new WebSocket(visitorsSocketUrl); const visitorsSocket = new WebSocket(visitorsSocketUrl);
visitorsSocket.onopen = () => { visitorsSocket.onopen = () => {
console.log('WebSocket connection to visitors established'); console.log('WebSocket connection to visitors established');
const event_message = { const event_message = {
'event_type': 'new_visitor', 'event_type': 'new_visitor',
'referrer': referrer, 'referrer': referrer,
'url': window.location.href, 'url': window.location.href,
'client_ip': client_ip, 'client_ip': client_ip,
'client_country': client_country 'client_country': client_country
};
visitorsSocket.send(JSON.stringify(event_message));
}; };
visitorsSocket.send(JSON.stringify(event_message));
};
visitorsSocket.onclose = () => { visitorsSocket.onclose = () => {
console.log('WebSocket connection to visitors closed'); console.log('WebSocket connection to visitors closed');
}; };
visitorsSocket.onerror = (error) => { visitorsSocket.onerror = (error) => {
console.error('WebSocket error:', error); console.error('WebSocket error:', error);
}; };
}); });
} }
initializeWebSocket(); initializeWebSocket();
});

Loading…
Cancel
Save