Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

consumers.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. self.groups_set = set()
  36. print("ChatConsumer:ws_conn0")
  37. # p("!!!!!!!!!!!!!!!!!!!!?")
  38. print("",end="",flush=True)
  39. # time.sleep(1.5)
  40. # p("!!!!!!!!!!!!!!!!!!!!")
  41. # print("",end="",flush=True)
  42. self.accept()
  43. await self.send(text_data=json.dumps({
  44. 'wsconn': {"abc.":123}
  45. }))
  46. async def ws_disconn(self, close_code):
  47. pass
  48. async def ws_disconn_z(self, close_code):
  49. pass
  50. async def ws_rec(self, text_data):
  51. return
  52. p("text_data:",text_data)
  53. print("",end="",flush=True)
  54. text_data_json = json.loads(text_data)
  55. message = text_data_json['message']
  56. await self.send(text_data=json.dumps({
  57. 'message': message
  58. }))
  59. await self.send(text_data=json.dumps({
  60. 'zzz': {"abc":123}
  61. }))
  62. # print(">>>>>>>>>>>>>")
  63. Mixins.ChatConsumer = ChatConsumer
  64. print("++?",end="\n",flush=True)
  65. # print("++2?",end="\n",flush=True)
  66. # print("++",end="\n",flush=True)