30 lines
979 B
Python
30 lines
979 B
Python
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):
|
|
json_data = json.loads(text_data)
|
|
# Echo
|
|
self.send(text_data=text_data)
|
|
# Run task
|
|
if json_data['task'] == 'calculate':
|
|
calculate_min_distance(self.room_group_name)
|
|
|
|
def channel_message(self, event):
|
|
message = event['message']
|
|
|
|
# Send message to WebSocket
|
|
self.send(text_data=str(message))
|