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

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