new
parent
e29ab2c80c
commit
0997f656c1
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,3 +1,18 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
import requests
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
||||
def get_client_ip(request):
|
||||
client_ip = request.META.get('REMOTE_ADDR', '')
|
||||
|
||||
try:
|
||||
response = requests.get(f'http://ipinfo.io/{client_ip}/json')
|
||||
data = response.json()
|
||||
country = data.get('country', 'Unknown')
|
||||
except Exception as e:
|
||||
country = "Unknown"
|
||||
|
||||
return JsonResponse({'ip': client_ip, 'country': country})
|
Binary file not shown.
@ -1,52 +1,52 @@
|
||||
const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
||||
const my_domain = "osina.ositcom.com";
|
||||
const referrer = document.referrer;
|
||||
const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`;
|
||||
|
||||
const getClientIPAndCountry = (callback) => {
|
||||
fetch('https://ipinfo.io/json?token=YOUR_IPINFO_TOKEN')
|
||||
// Function to fetch client IP and country from the API
|
||||
function fetchClientData() {
|
||||
return fetch('https://osina.ositcom.com/get-client-ip/')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
callback({
|
||||
ip: data.ip,
|
||||
country: data.country
|
||||
});
|
||||
})
|
||||
.then(data => ({
|
||||
client_ip: data.ip,
|
||||
client_country: data.country
|
||||
}))
|
||||
.catch(error => {
|
||||
console.error('Error fetching IP and country:', error);
|
||||
callback({
|
||||
ip: null,
|
||||
country: 'unknown'
|
||||
});
|
||||
console.error('Error fetching client data:', error);
|
||||
return {
|
||||
client_ip: 'Unknown',
|
||||
client_country: 'Unknown'
|
||||
};
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
const establishWebSocketConnection = (client_ip, client_country) => {
|
||||
const visitorsSocket = new WebSocket(visitorsSocketUrl);
|
||||
function initializeWebSocket() {
|
||||
const referrer = document.referrer;
|
||||
|
||||
visitorsSocket.onopen = () => {
|
||||
console.log('WebSocket connection to visitors established');
|
||||
// Fetch client data and then initialize WebSocket
|
||||
fetchClientData().then(({ client_ip, client_country }) => {
|
||||
const visitorsSocket = new WebSocket(visitorsSocketUrl);
|
||||
|
||||
const event_message = {
|
||||
'event_type': 'new_visitor',
|
||||
'referrer': referrer,
|
||||
'url': window.location.href,
|
||||
'client_ip': client_ip,
|
||||
'client_country': client_country
|
||||
visitorsSocket.onopen = () => {
|
||||
console.log('WebSocket connection to visitors established');
|
||||
|
||||
const event_message = {
|
||||
'event_type': 'new_visitor',
|
||||
'referrer': referrer,
|
||||
'url': window.location.href,
|
||||
'client_ip': client_ip,
|
||||
'client_country': client_country
|
||||
};
|
||||
visitorsSocket.send(JSON.stringify(event_message));
|
||||
};
|
||||
|
||||
visitorsSocket.onclose = () => {
|
||||
console.log('WebSocket connection to visitors closed');
|
||||
};
|
||||
visitorsSocket.send(JSON.stringify(event_message));
|
||||
};
|
||||
|
||||
visitorsSocket.onclose = () => {
|
||||
console.log('WebSocket connection to visitors closed');
|
||||
};
|
||||
|
||||
visitorsSocket.onerror = (error) => {
|
||||
console.error('WebSocket error:', error);
|
||||
};
|
||||
};
|
||||
|
||||
// Get the client IP and country first, then establish the WebSocket connection
|
||||
getClientIPAndCountry((client_info) => {
|
||||
establishWebSocketConnection(client_info.ip, client_info.country);
|
||||
});
|
||||
|
||||
visitorsSocket.onerror = (error) => {
|
||||
console.error('WebSocket error:', error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
initializeWebSocket();
|
||||
|
Loading…
Reference in New Issue