interact.py 782 B

12345678910111213141516171819202122232425262728293031
  1. import tornado.websocket
  2. import time, json
  3. from collections import defaultdict
  4. from .framework import Future, Msg, Global
  5. def _get_response(cmd, spec):
  6. msg = dict(command=cmd, spec=spec)
  7. Global.active_ws.write_message(json.dumps(msg))
  8. response_msg = yield from Future()
  9. return response_msg
  10. # 非阻塞协程工具库
  11. def text_input_coro(prompt):
  12. data = yield from _get_response("text_input", spec=dict(prompt=prompt))
  13. input_text = data['data']
  14. return input_text
  15. def ctrl_coro(ctrl_info):
  16. msg = dict(command="ctrl", spec=ctrl_info)
  17. Global.active_ws.write_message(json.dumps(msg))
  18. def text_print(text, *, ws=None):
  19. msg = dict(command="text_print", spec=dict(content=text))
  20. (ws or Global.active_ws).write_message(json.dumps(msg))