You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

consumers.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # chat/consumers.py
  2. import json
  3. from channels.generic.websocket import WebsocketConsumer,AsyncWebsocketConsumer
  4. import wsps.tasks_loop
  5. import time
  6. p=print
  7. Mixins = wsps.tasks_loop.Mixins
  8. # class ChatConsumer(WebsocketConsumer,wsps.tasks_loop.AclTaskMixin,wsps.tasks_loop.AconMixin):
  9. class DispConsumer():
  10. async def ws_conn0(self):
  11. print("WS~~ DispConsumer")
  12. async def ws_rec_once(self, text_data):
  13. # print("WS~~ 12")
  14. # print("WS~~ DispConsumer")
  15. msg_data = json.loads(text_data)
  16. # if 'message' in text_data_json:
  17. if "key" in msg_data:
  18. key = "jm_" + msg_data["key"]
  19. # key = "j_" + msg_data["key"]
  20. mthd = getattr(self,key,None)
  21. if mthd:
  22. await mthd(msg_data)
  23. else:
  24. p("METHOD " + key + " NOT FOUND!")
  25. print("",end="",flush=True)
  26. class ChatConsumer():
  27. async def ws_conn0(self):
  28. print("WS~~ ChatConsumer")
  29. print("",end="",flush=True)
  30. self.accept()
  31. async def ws_disconn(self, close_code):
  32. pass
  33. async def ws_rec(self, text_data):
  34. return
  35. p("text_data:",text_data)
  36. print("",end="",flush=True)
  37. text_data_json = json.loads(text_data)
  38. message = text_data_json['message']
  39. await self.send(text_data=json.dumps({
  40. 'message': message
  41. }))
  42. await self.send(text_data=json.dumps({
  43. 'zzz': {"abc":123}
  44. }))zzz
  45. Mixins.ChatConsumer = ChatConsumer