您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

consumers.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. r'''
  41. await self.send(text_data=json.dumps({
  42. 'wsconn': {"abc.":12}
  43. }))
  44. '''
  45. # print("",end="",flush=True)
  46. self.accept()
  47. r'''
  48. await self.send(text_data=json.dumps({
  49. 'wsconn': {"abc.":123}
  50. }))
  51. '''
  52. async def ws_disconn(self, close_code):
  53. pass
  54. async def ws_disconn_z(self, close_code):
  55. pass
  56. async def ws_rec(self, text_data):
  57. return
  58. p("text_data:",text_data)
  59. print("",end="",flush=True)
  60. text_data_json = json.loads(text_data)
  61. message = text_data_json['message']
  62. await self.send(text_data=json.dumps({
  63. 'message': message
  64. }))
  65. await self.send(text_data=json.dumps({
  66. 'zzz': {"abc":123}
  67. }))
  68. # print(">>>>>>>>>>>>>")
  69. Mixins.ChatConsumer = ChatConsumer
  70. print("++?",end="\n",flush=True)
  71. # print("++2?",end="\n",flush=True)
  72. # print("++",end="\n",flush=True)