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.shortcuts import render
|
||||||
from django.http import JsonResponse
|
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 visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
|
||||||
const my_domain = "osina.ositcom.com";
|
const my_domain = "osina.ositcom.com";
|
||||||
const referrer = document.referrer;
|
|
||||||
const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`;
|
const visitorsSocketUrl = `${visitors_ws_scheme}://${my_domain}/ws/osichat/visitors/`;
|
||||||
|
|
||||||
const getClientIPAndCountry = (callback) => {
|
// Function to fetch client IP and country from the API
|
||||||
fetch('https://ipinfo.io/json?token=YOUR_IPINFO_TOKEN')
|
function fetchClientData() {
|
||||||
|
return fetch('https://osina.ositcom.com/get-client-ip/')
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => ({
|
||||||
callback({
|
client_ip: data.ip,
|
||||||
ip: data.ip,
|
client_country: data.country
|
||||||
country: data.country
|
}))
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('Error fetching IP and country:', error);
|
console.error('Error fetching client data:', error);
|
||||||
callback({
|
return {
|
||||||
ip: null,
|
client_ip: 'Unknown',
|
||||||
country: 'unknown'
|
client_country: 'Unknown'
|
||||||
});
|
};
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
const establishWebSocketConnection = (client_ip, client_country) => {
|
function initializeWebSocket() {
|
||||||
const visitorsSocket = new WebSocket(visitorsSocketUrl);
|
const referrer = document.referrer;
|
||||||
|
|
||||||
visitorsSocket.onopen = () => {
|
// Fetch client data and then initialize WebSocket
|
||||||
console.log('WebSocket connection to visitors established');
|
fetchClientData().then(({ client_ip, client_country }) => {
|
||||||
|
const visitorsSocket = new WebSocket(visitorsSocketUrl);
|
||||||
const event_message = {
|
|
||||||
'event_type': 'new_visitor',
|
visitorsSocket.onopen = () => {
|
||||||
'referrer': referrer,
|
console.log('WebSocket connection to visitors established');
|
||||||
'url': window.location.href,
|
|
||||||
'client_ip': client_ip,
|
const event_message = {
|
||||||
'client_country': client_country
|
'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.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);
|
||||||
};
|
};
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Get the client IP and country first, then establish the WebSocket connection
|
initializeWebSocket();
|
||||||
getClientIPAndCountry((client_info) => {
|
|
||||||
establishWebSocketConnection(client_info.ip, client_info.country);
|
|
||||||
});
|
|
||||||
|
Loading…
Reference in New Issue