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.
64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
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'
|
|
};
|
|
});
|
|
}
|
|
|
|
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';
|
|
});
|
|
}
|
|
|
|
function initializeWebSocket() {
|
|
const referrer = document.referrer;
|
|
|
|
Promise.all([fetchClientData(), fetchSessionID()]).then(([clientData, session_id]) => {
|
|
const visitorsSocket = new WebSocket(visitorsSocketUrl);
|
|
|
|
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));
|
|
};
|
|
|
|
visitorsSocket.onclose = () => {
|
|
console.log('WebSocket connection to visitors closed');
|
|
};
|
|
|
|
visitorsSocket.onerror = (error) => {
|
|
console.error('WebSocket error:', error);
|
|
};
|
|
});
|
|
}
|
|
|
|
initializeWebSocket();
|
|
});
|