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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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~~ 12")
  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. DispConsumer,
  24. Mixins.DevMixin,
  25. Mixins.LoneMixinA,
  26. Mixins.AclTaskMixin,
  27. Mixins.AconMixin,
  28. Mixins.LoneMixinB,
  29. Mixins.DevMroMixin,
  30. AsyncWebsocketConsumer,
  31. ):
  32. # def connect(self):
  33. async def ws_conn0(self):
  34. print("ChatConsumer:ws_conn0")
  35. # p("!!!!!!!!!!!!!!!!!!!!?")
  36. print("",end="",flush=True)
  37. # time.sleep(1.5)
  38. # p("!!!!!!!!!!!!!!!!!!!!")
  39. # print("",end="",flush=True)
  40. self.accept()
  41. await self.send(text_data=json.dumps({
  42. 'wsconn': {"abc.":123}
  43. }))
  44. async def ws_disconn(self, close_code):
  45. pass
  46. async def ws_disconn_z(self, close_code):
  47. pass
  48. async def ws_rec(self, text_data):
  49. return
  50. p("text_data:",text_data)
  51. print("",end="",flush=True)
  52. text_data_json = json.loads(text_data)
  53. message = text_data_json['message']
  54. await self.send(text_data=json.dumps({
  55. 'message': message
  56. }))
  57. await self.send(text_data=json.dumps({
  58. 'zzz': {"abc":123}
  59. }))
  60. # print(">>>>>>>>>>>>>")
  61. Mixins.ChatConsumer = ChatConsumer
  62. print("++?",end="\n",flush=True)
  63. # print("++2?",end="\n",flush=True)
  64. # print("++",end="\n",flush=True)