web_socket_server.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import asyncio
  2. import queue
  3. import threading
  4. import websockets
  5. import os
  6. import sys
  7. import time
  8. import json
  9. from pprint import pprint
  10. from InquirerPy import prompt
  11. from InquirerPy.base.control import Choice
  12. from mock_messages import mock_message_json
  13. # 服务器端口
  14. port = 8765
  15. # Gavin
  16. user_code = "4991656C2F524F57A910358B8CF8838F"
  17. def get_option_by_index(index):
  18. return {"index": index, "content": mock_message_json[index]}
  19. # 2022-12-23
  20. # ChatMsgNotification|1| 聊天通知
  21. # TokenReplacedNotification|2| 账号被替换登出通知
  22. # DisconnectNotification| 3| 与服务器断开连接通知
  23. # ConnectionNotification| 4| 与服务器已连接通知
  24. # ExamRecordsFinishedNotification| 5 | 完成检查通知
  25. # RejectApplyConsultationNotification| 6 | 拒绝预约申请的通知
  26. # CancelInvitingInLiveConsultationNotification| 7 | 取消会诊过程中邀请其他成员的通知
  27. # InviteInLiveConsultationNotification| 8 | 会诊过程中邀请其他成员的通知
  28. # InviteInLiveConsultationNotification| 9 | 会诊开始前提醒的通知
  29. # PasswordExpiredWarningNotification| 10 | 用户密码过期预警通知
  30. # InviteLiveConsultationNotification| 11 | 开始会诊的通知
  31. # AcceptLiveConsultationNotification| 12 | 接受会诊的通知
  32. # RejectLiveConsultationNotification| 13 | 拒绝会诊的通知
  33. # InviteLiveConsultationToDeviceNotification| 14 | 开始会诊通知 to设备端
  34. # CancelLiveConsultationNotification| 15 | 取消会诊通知
  35. # CloseLiveConsultationNotification| 16 | 关闭会诊通知
  36. # JoinLiveConsultationNotification| 17 | 进入会诊通知
  37. # NetworkErrConsultationNotification| 18 | 网络质量不佳会诊通知
  38. # LeaveConsultationNotification| 19 | 离开会诊通知
  39. # JoinInLiveConsultationNotification| 20 | 会诊中加入房间
  40. # RejectLiveConsultationNotification| 21 | 拒绝会诊的通知
  41. # ApplyConsultationNotification| 22 | 会诊申请通知
  42. # ApprovalApplyConsultationNotification| 23 | 批准申请会诊通知
  43. # InviteeConsultationNotification| 24 | 会诊受邀请人通知
  44. # InviteeApproveApplyConsultationNotification| 25 | 会诊受邀请参与人同意通知
  45. # InviteeRejectApplyConsultationNotification| 26 | 会诊受邀请参与人拒绝通知
  46. # MuteLiveConsultationNotification| 27 | 开启关闭静音
  47. # SwitchLiveConsultationVideoNotification| 28 | 开启关闭视频
  48. # HeartRateJoinConsultationNotification| 29 | 会诊心跳,进入房间
  49. # HeartRateLeaveConsultationNotification| 30 | 会诊心跳,离开房间
  50. options = [
  51. {'name': '❌「0」断开连接', 'value': '0'},
  52. {'name': '❓「1」聊天通知', 'value': get_option_by_index(1)},
  53. {'name': '❓「2」账号被替换登出通知', 'value': get_option_by_index(2)},
  54. {'name': '❓「3」与服务器断开连接通知', 'value': get_option_by_index(3)},
  55. {'name': '❓「4」与服务器已连接通知', 'value': get_option_by_index(4)},
  56. {'name': '❓「5」完成检查通知', 'value': get_option_by_index(5)},
  57. {'name': '✅「6」拒绝预约申请的通知 📅', 'value': get_option_by_index(6)},
  58. {'name': '✅「7」取消会诊过程中邀请其他成员的通知', 'value': get_option_by_index(7)},
  59. {'name': '✅「8」会诊过程中邀请其他成员的通知', 'value': get_option_by_index(8)},
  60. {'name': '✅「9」会诊开始前提醒的通知 📅', 'value': get_option_by_index(9)},
  61. {'name': '❓「10」用户密码过期预警通知', 'value': get_option_by_index(10)},
  62. {'name': '✅「11」开始会诊的通知', 'value': get_option_by_index(11)},
  63. {'name': '❓「12」接受会诊的通知', 'value': get_option_by_index(12)},
  64. {'name': '❓「13」拒绝会诊的通知', 'value': get_option_by_index(13)},
  65. {'name': '❓「14」开始会诊通知 to 设备端', 'value': get_option_by_index(14)},
  66. {'name': '❓「15」取消会诊通知', 'value': get_option_by_index(15)},
  67. {'name': '❓「16」关闭会诊通知', 'value': get_option_by_index(16)},
  68. {'name': '❓「17」进入会诊通知', 'value': get_option_by_index(17)},
  69. {'name': '❓「18」网络质量不佳会诊通知', 'value': get_option_by_index(18)},
  70. {'name': '❓「19」离开会诊通知', 'value': get_option_by_index(19)},
  71. {'name': '❓「20」会诊中加入房间', 'value': get_option_by_index(20)},
  72. {'name': '❓「21」拒绝会诊的通知', 'value': get_option_by_index(21)},
  73. {'name': '✅「22」会诊申请通知 📅', 'value': get_option_by_index(22)},
  74. {'name': '✅「23」批准申请会诊通知 📅', 'value': get_option_by_index(23)},
  75. {'name': '✅「24」会诊受邀请人通知 📅', 'value': get_option_by_index(24)},
  76. {'name': '✅「25」会诊受邀请参与人同意通知 📅', 'value': get_option_by_index(25)},
  77. {'name': '✅「26」会诊受邀请参与人拒绝通知 📅', 'value': get_option_by_index(26)},
  78. {'name': '❓「27」开启关闭静音', 'value': get_option_by_index(27)},
  79. {'name': '❓「28」开启关闭视频', 'value': get_option_by_index(28)},
  80. {'name': '❓「29」会诊心跳 进入房间', 'value': get_option_by_index(29)},
  81. {'name': '❓「30」会诊心跳 离开房间', 'value': get_option_by_index(30)},
  82. ]
  83. def get_questions(index):
  84. questions = [
  85. {
  86. 'type': 'list',
  87. 'name': 'message',
  88. 'message': '请选择要发送的消息:',
  89. 'choices': [Choice(**option) for option in options],
  90. 'default': options[index]['value']
  91. },
  92. ]
  93. return questions
  94. is_ws_connected = False
  95. # 创建消息队列
  96. msg_queue = asyncio.Queue()
  97. # 创建通知队列
  98. flag_queue = queue.Queue()
  99. async def send_messages(websocket):
  100. # 不停遍历队列,如果有消息就发送
  101. print('客户端已经接入,地址:', websocket.remote_address)
  102. flag_queue.put('connected')
  103. while True:
  104. await asyncio.sleep(0.1)
  105. if msg_queue.empty():
  106. continue
  107. else:
  108. message = await msg_queue.get()
  109. await websocket.send(message)
  110. async def echo(websocket, path):
  111. await send_messages(websocket)
  112. async def ws_server():
  113. async with websockets.serve(echo, "localhost", 8765):
  114. print('WS 服务已经建立,地址 ws://127.0.0.1:'+str(port)+',等待客户端接入...')
  115. await asyncio.Future() # run forever
  116. def create_ws_server():
  117. # 启动线程
  118. asyncio.run(ws_server())
  119. def create_message(msg_queue):
  120. # 创建消息
  121. json_message = ""
  122. message_name = ""
  123. default_index = 0
  124. while json_message != "0":
  125. os.system('cls') # 清空控制台
  126. if json_message != "":
  127. print('----------------------------------------')
  128. print('已经发送的消息:'+options[default_index]['name']+'\n')
  129. message_content = json.loads(json_message)
  130. pprint(message_content)
  131. print('\n----------------------------------------\n')
  132. answers = prompt(get_questions(default_index))
  133. if answers['message'] == "0":
  134. os.system('cls') # 清空控制台
  135. break
  136. json_message = answers['message']['content']
  137. default_index = answers['message']['index']
  138. msg_queue.put_nowait(json_message) # 将消息添加到消息队列中
  139. os._exit(0)
  140. def wait_connect_start():
  141. # 等待连接
  142. while True:
  143. time.sleep(1)
  144. if flag_queue.empty():
  145. continue
  146. else:
  147. break
  148. create_message(msg_queue)
  149. def main():
  150. thread = threading.Thread(target=create_ws_server)
  151. thread.start()
  152. wait_connect_start()
  153. main()