123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import asyncio
- import queue
- import threading
- import websockets
- import os
- import sys
- import time
- import json
- from pprint import pprint
- from InquirerPy import prompt
- from InquirerPy.base.control import Choice
- from mock_messages import mock_message_json
- # 服务器端口
- port = 8765
- # Gavin
- user_code = "4991656C2F524F57A910358B8CF8838F"
- def get_option_by_index(index):
- return {"index": index, "content": mock_message_json[index]}
- # 2022-12-23
- # ChatMsgNotification|1| 聊天通知
- # TokenReplacedNotification|2| 账号被替换登出通知
- # DisconnectNotification| 3| 与服务器断开连接通知
- # ConnectionNotification| 4| 与服务器已连接通知
- # ExamRecordsFinishedNotification| 5 | 完成检查通知
- # RejectApplyConsultationNotification| 6 | 拒绝预约申请的通知
- # CancelInvitingInLiveConsultationNotification| 7 | 取消会诊过程中邀请其他成员的通知
- # InviteInLiveConsultationNotification| 8 | 会诊过程中邀请其他成员的通知
- # InviteInLiveConsultationNotification| 9 | 会诊开始前提醒的通知
- # PasswordExpiredWarningNotification| 10 | 用户密码过期预警通知
- # InviteLiveConsultationNotification| 11 | 开始会诊的通知
- # AcceptLiveConsultationNotification| 12 | 接受会诊的通知
- # RejectLiveConsultationNotification| 13 | 拒绝会诊的通知
- # InviteLiveConsultationToDeviceNotification| 14 | 开始会诊通知 to设备端
- # CancelLiveConsultationNotification| 15 | 取消会诊通知
- # CloseLiveConsultationNotification| 16 | 关闭会诊通知
- # JoinLiveConsultationNotification| 17 | 进入会诊通知
- # NetworkErrConsultationNotification| 18 | 网络质量不佳会诊通知
- # LeaveConsultationNotification| 19 | 离开会诊通知
- # JoinInLiveConsultationNotification| 20 | 会诊中加入房间
- # RejectLiveConsultationNotification| 21 | 拒绝会诊的通知
- # ApplyConsultationNotification| 22 | 会诊申请通知
- # ApprovalApplyConsultationNotification| 23 | 批准申请会诊通知
- # InviteeConsultationNotification| 24 | 会诊受邀请人通知
- # InviteeApproveApplyConsultationNotification| 25 | 会诊受邀请参与人同意通知
- # InviteeRejectApplyConsultationNotification| 26 | 会诊受邀请参与人拒绝通知
- # MuteLiveConsultationNotification| 27 | 开启关闭静音
- # SwitchLiveConsultationVideoNotification| 28 | 开启关闭视频
- # HeartRateJoinConsultationNotification| 29 | 会诊心跳,进入房间
- # HeartRateLeaveConsultationNotification| 30 | 会诊心跳,离开房间
- options = [
- {'name': '❌「0」断开连接', 'value': '0'},
- {'name': '❓「1」聊天通知', 'value': get_option_by_index(1)},
- {'name': '❓「2」账号被替换登出通知', 'value': get_option_by_index(2)},
- {'name': '❓「3」与服务器断开连接通知', 'value': get_option_by_index(3)},
- {'name': '❓「4」与服务器已连接通知', 'value': get_option_by_index(4)},
- {'name': '❓「5」完成检查通知', 'value': get_option_by_index(5)},
- {'name': '✅「6」拒绝预约申请的通知 📅', 'value': get_option_by_index(6)},
- {'name': '✅「7」取消会诊过程中邀请其他成员的通知', 'value': get_option_by_index(7)},
- {'name': '✅「8」会诊过程中邀请其他成员的通知', 'value': get_option_by_index(8)},
- {'name': '✅「9」会诊开始前提醒的通知 📅', 'value': get_option_by_index(9)},
- {'name': '❓「10」用户密码过期预警通知', 'value': get_option_by_index(10)},
- {'name': '✅「11」开始会诊的通知', 'value': get_option_by_index(11)},
- {'name': '❓「12」接受会诊的通知', 'value': get_option_by_index(12)},
- {'name': '❓「13」拒绝会诊的通知', 'value': get_option_by_index(13)},
- {'name': '❓「14」开始会诊通知 to 设备端', 'value': get_option_by_index(14)},
- {'name': '❓「15」取消会诊通知', 'value': get_option_by_index(15)},
- {'name': '❓「16」关闭会诊通知', 'value': get_option_by_index(16)},
- {'name': '❓「17」进入会诊通知', 'value': get_option_by_index(17)},
- {'name': '❓「18」网络质量不佳会诊通知', 'value': get_option_by_index(18)},
- {'name': '❓「19」离开会诊通知', 'value': get_option_by_index(19)},
- {'name': '❓「20」会诊中加入房间', 'value': get_option_by_index(20)},
- {'name': '❓「21」拒绝会诊的通知', 'value': get_option_by_index(21)},
- {'name': '✅「22」会诊申请通知 📅', 'value': get_option_by_index(22)},
- {'name': '✅「23」批准申请会诊通知 📅', 'value': get_option_by_index(23)},
- {'name': '✅「24」会诊受邀请人通知 📅', 'value': get_option_by_index(24)},
- {'name': '✅「25」会诊受邀请参与人同意通知 📅', 'value': get_option_by_index(25)},
- {'name': '✅「26」会诊受邀请参与人拒绝通知 📅', 'value': get_option_by_index(26)},
- {'name': '❓「27」开启关闭静音', 'value': get_option_by_index(27)},
- {'name': '❓「28」开启关闭视频', 'value': get_option_by_index(28)},
- {'name': '❓「29」会诊心跳 进入房间', 'value': get_option_by_index(29)},
- {'name': '❓「30」会诊心跳 离开房间', 'value': get_option_by_index(30)},
- ]
- def get_questions(index):
- questions = [
- {
- 'type': 'list',
- 'name': 'message',
- 'message': '请选择要发送的消息:',
- 'choices': [Choice(**option) for option in options],
- 'default': options[index]['value']
- },
- ]
- return questions
- is_ws_connected = False
- # 创建消息队列
- msg_queue = asyncio.Queue()
- # 创建通知队列
- flag_queue = queue.Queue()
- async def send_messages(websocket):
- # 不停遍历队列,如果有消息就发送
- print('客户端已经接入,地址:', websocket.remote_address)
- flag_queue.put('connected')
- while True:
- await asyncio.sleep(0.1)
- if msg_queue.empty():
- continue
- else:
- message = await msg_queue.get()
- await websocket.send(message)
- async def echo(websocket, path):
- await send_messages(websocket)
- async def ws_server():
- async with websockets.serve(echo, "localhost", 8765):
- print('WS 服务已经建立,地址 ws://127.0.0.1:'+str(port)+',等待客户端接入...')
- await asyncio.Future() # run forever
- def create_ws_server():
- # 启动线程
- asyncio.run(ws_server())
- def create_message(msg_queue):
- # 创建消息
- json_message = ""
- message_name = ""
- default_index = 0
- while json_message != "0":
- os.system('cls') # 清空控制台
- if json_message != "":
- print('----------------------------------------')
- print('已经发送的消息:'+options[default_index]['name']+'\n')
- message_content = json.loads(json_message)
- pprint(message_content)
- print('\n----------------------------------------\n')
- answers = prompt(get_questions(default_index))
- if answers['message'] == "0":
- os.system('cls') # 清空控制台
- break
- json_message = answers['message']['content']
- default_index = answers['message']['index']
- msg_queue.put_nowait(json_message) # 将消息添加到消息队列中
- os._exit(0)
- def wait_connect_start():
- # 等待连接
- while True:
- time.sleep(1)
- if flag_queue.empty():
- continue
- else:
- break
- create_message(msg_queue)
- def main():
- thread = threading.Thread(target=create_ws_server)
- thread.start()
- wait_connect_start()
- main()
|