2024-11-05 11:46:00 +01:00
|
|
|
import json
|
|
|
|
from channels.generic.websocket import WebsocketConsumer
|
|
|
|
from asgiref.sync import async_to_sync
|
|
|
|
from waiting_room.tasks import calculate_min_distance
|
|
|
|
|
|
|
|
class MyConsumer(WebsocketConsumer):
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self.room_group_name = self.scope["url_route"]["kwargs"]["room_name"]
|
|
|
|
async_to_sync(self.channel_layer.group_add)(self.room_group_name, self.channel_name)
|
|
|
|
self.accept()
|
|
|
|
|
|
|
|
def disconnect(self, close_code):
|
|
|
|
async_to_sync(self.channel_layer.group_discard)(self.room_group_name, self.channel_name)
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def receive(self, text_data):
|
2024-11-05 16:52:47 +01:00
|
|
|
json_data = json.loads(text_data)
|
2024-11-05 11:46:00 +01:00
|
|
|
# Echo
|
|
|
|
self.send(text_data=text_data)
|
2024-11-05 16:52:47 +01:00
|
|
|
# Run task
|
|
|
|
if json_data['task'] == 'calculate':
|
|
|
|
calculate_min_distance(self.room_group_name)
|
2024-11-05 11:46:00 +01:00
|
|
|
|
|
|
|
def channel_message(self, event):
|
|
|
|
message = event['message']
|
|
|
|
|
|
|
|
# Send message to WebSocket
|
|
|
|
self.send(text_data=str(message))
|