consumers are now async

This commit is contained in:
Gonçalo Valério 2019-03-17 20:09:07 +00:00
parent b9c6f7ac00
commit 6371755c1f
1 changed files with 10 additions and 12 deletions

View File

@ -1,24 +1,22 @@
from channels.generic.websocket import WebsocketConsumer
from channels.generic.websocket import AsyncWebsocketConsumer
from asgiref.sync import async_to_sync
import json
class WebhookConsumer(WebsocketConsumer):
def connect(self):
class WebhookConsumer(AsyncWebsocketConsumer):
async def connect(self):
"""While the connection of open it is associated with a callback"""
self.callback = self.scope["url_route"]["kwargs"]["uuid"]
async_to_sync(self.channel_layer.group_add)(self.callback, self.channel_name)
self.accept()
await self.channel_layer.group_add(self.callback, self.channel_name)
await self.accept()
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(
self.callback, self.channel_name
)
async def disconnect(self, close_code):
await self.channel_layer.group_discard(self.callback, self.channel_name)
def receive(self, text_data):
async def receive(self, text_data):
# Discard all received data
pass
def new_request(self, event):
async def new_request(self, event):
"""Sends all the newly received data on the callback"""
self.send(text_data=json.dumps(event["data"]))
await self.send(text_data=json.dumps(event["data"]))