Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. class LongPollConsumer(AsyncHttpConsumer):
  2. async def handle(self, body):
  3. self.room_name = self.scope['url_route']['kwargs']['uuid']
  4. self.room_group_name = 'upvote_'
  5. # await self.send_response(200, b"Hello", headers=[
  6. # (b"Content-Type", b"application/json"),
  7. # ])
  8. await self.send_headers(headers=[
  9. (b"Content-Type", b"application/json"),
  10. ])
  11. print('consumer room_name', self.room_group_name)
  12. async_to_sync(self.channel_layer.group_add)(
  13. self.room_group_name,
  14. self.channel_name
  15. )
  16.  
  17. # Headers are only sent after the first body event.
  18. # Set "more_body" to tell the interface server to not
  19. # finish the response yet:
  20. await self.send_body(b"i", more_body=True)
  21.  
  22.  
  23. async def upvote_message(self, event):
  24. # Send JSON and finish the response:
  25. # async_to_sync(self.channel_layer.group_discard)(
  26. # self.room_group_name,
  27. # self.channel_name
  28. # )
  29. print('inside....upvote_message')
  30. await self.send_body(json.dumps(event).encode("utf-8"))
  31.  
  32. room_group_name = 'upvote_'
  33. channel_layer = get_channel_layer()
  34. async_to_sync(channel_layer.group_send)(
  35. room_group_name, {
  36. "type": "upvote.message",
  37. "message": {
  38. 'actionType': 'WS_NEW_EVENT_RECEIVED',
  39. 'data': {}
  40. }
  41. }
  42. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement