-
Hello. I'm using this example from the doc: https://channels.readthedocs.io/en/stable/topics/consumers.html#asynchttpconsumer I added only group registration and unregistration. So this is my consumer now: import json
from channels.generic.http import AsyncHttpConsumer
class LongPollConsumer(AsyncHttpConsumer):
async def handle(self, body):
await self.send_headers(headers=[
(b"Content-Type", b"application/json"),
])
await self.channel_layer.group_add('test', self.channel_name)
# Headers are only sent after the first body event.
# Set "more_body" to tell the interface server to not
# finish the response yet:
await self.send_body(b"", more_body=True)
async def chat_message(self, event):
# Send JSON and finish the response:
await self.send_body(json.dumps(event).encode("utf-8"))
async def disconnect(self):
await self.channel_layer.group_discard('test', self.channel_name) But Also I cant trigger channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)('test', {'type': 'chat.message, 'data': {'test': True}}) This code are placed in What is wrong? Why I got |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The finally block calls disconnection method and raise stop consumer after completing |
Beta Was this translation helpful? Give feedback.
The finally block calls disconnection method and raise stop consumer after completing
handler()
method (code ref). To work with long polling we should overridehttp_request
method by ourselves.