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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_rec_once(self, text_data):
  11. # print("WS~~ 1")
  12. msg_data = json.loads(text_data)
  13. # if 'message' in text_data_json:
  14. if "key" in msg_data:
  15. key = msg_data["key"]
  16. mthd = getattr(self,key,None)
  17. if mthd:
  18. await mthd(msg_data)
  19. else:
  20. p("METHOD " + key + " NOT FOUND!")
  21. print("",end="",flush=True)
  22. class ChatConsumer(
  23. Mixins.DevMixin,
  24. Mixins.LoneMixinA,
  25. Mixins.AclTaskMixin,
  26. Mixins.AconMixin,
  27. AsyncWebsocketConsumer,
  28. Mixins.LoneMixinB,
  29. Mixins.DevMroMixin,
  30. ):
  31. # def connect(self):
  32. async def ws_conn(self):
  33. p("!!!!!!!!!!!!!!!!!!!!?")
  34. print("",end="",flush=True)
  35. # time.sleep(1.5)
  36. # p("!!!!!!!!!!!!!!!!!!!!")
  37. # print("",end="",flush=True)
  38. self.accept()
  39. async def ws_disconn(self, close_code):
  40. pass
  41. async def ws_disconn_z(self, close_code):
  42. pass
  43. async def ws_rec(self, text_data):
  44. p("text_data:",text_data)
  45. print("",end="",flush=True)
  46. text_data_json = json.loads(text_data)
  47. message = text_data_json['message']
  48. self.send(text_data=json.dumps({
  49. 'message': message
  50. }))
  51. self.send(text_data=json.dumps({
  52. 'zzz': {"abc":123}
  53. }))
  54. # print(">>>>>>>>>>>>>")
  55. Mixins.ChatConsumer = ChatConsumer
  56. print("++?",end="\n",flush=True)
  57. # print("++2?",end="\n",flush=True)
  58. # print("++",end="\n",flush=True)