emile 10 months ago
parent ada7c209e2
commit 32ea7a408f

Binary file not shown.

@ -1,7 +1,7 @@
from django.contrib import admin
from .models import *
# Register your models here.
admin.site.register(Visitor)
admin.site.register(ChatRoom)
admin.site.register(ChatRoomGuest)
admin.site.register(ChatMember)

@ -6,6 +6,49 @@ from asgiref.sync import async_to_sync
from django.shortcuts import get_object_or_404
class OsitcomVisitor(WebsocketConsumer):
def connect(self):
self.accept()
async_to_sync(self.channel_layer.group_add)(
'ositcom_visitors', self.channel_name
)
self.visitor = None
def disconnect(self, close_code):
if self.visitor:
self.visitor.left_date = datetime.now()
self.visitor.save()
async_to_sync(self.channel_layer.group_discard)(
'ositcom_visitors', self.channel_name
)
def receive(self, text_data):
text_data_json = json.loads(text_data)
event_type = text_data_json.get('event_type')
if event_type == 'new_visitor':
event = {
'type': 'new_visitor_handler',
'session_id': text_data_json.get('session_id'),
'ip_address': text_data_json.get('ip_address'),
'country': text_data_json.get('country'),
'url': text_data_json.get('url'),
}
async_to_sync(self.channel_layer.group_send)(
'ositcom_visitors', event
)
def new_visitor_handler(self, event):
visitor = Visitor.objects.create(
session_id = event['session_id'],
ip_address = event['ip_address'],
country = event['country'],
url = event['url'],
visit_date = datetime.now(),
)
self.visitor = visitor
class OsitcomChatRoom(WebsocketConsumer):
def connect(self):
@ -105,7 +148,6 @@ class OsitcomChatRoom(WebsocketConsumer):
context = {
'chat_message': chat_message,
}
print('lokf')
html = render_to_string("partials/message.html", context=context)
self.send(text_data=json.dumps({
'event_type': 'send_message',

@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2024-07-21 17:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('osichat', '0010_visitor_left_date_visitor_visit_date'),
]
operations = [
migrations.AddField(
model_name='visitor',
name='country',
field=models.CharField(max_length=15, null=True),
),
]

@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2024-07-21 17:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('osichat', '0011_visitor_country'),
]
operations = [
migrations.AlterField(
model_name='visitor',
name='reference',
field=models.URLField(blank=True, null=True),
),
]

@ -5,8 +5,9 @@ from osinacore.models import *
class Visitor(models.Model):
session_id = models.CharField(max_length=300)
ip_address = models.CharField(max_length=300)
country = models.CharField(max_length=15, null=True)
url = models.URLField()
reference = models.URLField()
reference = models.URLField(null=True, blank=True)
visit_date = models.DateTimeField(null=True)
left_date = models.DateTimeField(null=True)

@ -2,6 +2,7 @@ from django.urls import path
from .consumers import *
websocket_urlpatterns = [
path("ws/osichat/visitors/", OsitcomVisitor.as_asgi()),
path("ws/osichat/<str:session_id>/", OsitcomChatRoom.as_asgi()),
]

@ -2,7 +2,6 @@
const chat_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
const domain = "192.168.1.111:8000";
const sessionId = document.getElementById('session_id').textContent.trim();
console.log(sessionId);
const osichatSocketUrl = `${chat_ws_scheme}://${domain}/ws/osichat/${sessionId}/`;
const osichatSocket = new WebSocket(osichatSocketUrl);

@ -0,0 +1,31 @@
const visitors_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
const my_domain = "192.168.1.111:8000";
const session_id = document.getElementById('session_id').textContent.trim();
const client_ip = document.getElementById('client_ip').textContent.trim();
const client_country = document.getElementById('client_country').textContent.trim();
const current_url = document.getElementById('current_url').textContent.trim();
const visitorsSocketUrl = `${chat_ws_scheme}://${domain}/ws/osichat/visitors/`;
const visitorsSocket = new WebSocket(visitorsSocketUrl);
visitorsSocket.onopen = () => {
console.log('WebSocket connection to visitors established');
const event_message = {
'event_type': 'new_visitor',
'session_id': session_id,
'ip_address': client_ip,
'country': client_country,
'url': current_url
};
visitorsSocket.send(JSON.stringify(event_message));
};
visitorsSocket.onclose = () => {
console.log('WebSocket connection to visitors closed');
};
visitorsSocket.onerror = (error) => {
console.log('WebSocket error:', error);
};
Loading…
Cancel
Save