base.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. class AbstractSession:
  2. """
  3. 会话对象,由Backend创建
  4. 由Task在当前Session上下文中调用:
  5. get_current_session
  6. get_current_task_id
  7. send_task_command
  8. next_client_event
  9. on_task_exception
  10. register_callback
  11. 由Backend调用:
  12. send_client_event
  13. get_task_commands
  14. close
  15. Task和Backend都可调用:
  16. closed
  17. .. note::
  18. 后端Backend在相应on_session_close时关闭连接时,需要保证会话内的所有消息都传送到了客户端
  19. """
  20. @staticmethod
  21. def get_current_session() -> "AbstractSession":
  22. raise NotImplementedError
  23. @staticmethod
  24. def get_current_task_id():
  25. raise NotImplementedError
  26. def __init__(self, target, on_task_command=None, on_session_close=None, **kwargs):
  27. """
  28. :param target:
  29. :param on_task_command: Backend向ession注册的处理函数,当 Session 收到task发送的command时调用
  30. :param on_session_close: Backend向Session注册的处理函数,当 Session task执行结束时调用 *
  31. :param kwargs:
  32. """
  33. raise NotImplementedError
  34. def send_task_command(self, command):
  35. raise NotImplementedError
  36. def next_client_event(self) -> dict:
  37. raise NotImplementedError
  38. def send_client_event(self, event):
  39. raise NotImplementedError
  40. def get_task_commands(self) -> list:
  41. raise NotImplementedError
  42. def close(self):
  43. raise NotImplementedError
  44. def closed(self) -> bool:
  45. raise NotImplementedError
  46. def on_task_exception(self):
  47. raise NotImplementedError
  48. def register_callback(self, callback, **options):
  49. """ 向Session注册一个回调函数,返回回调id
  50. Session需要保证当收到前端发送的事件消息 ``{event: "callback",task_id: 回调id, data:...}`` 时,
  51. ``callback`` 回调函数被执行, 并传入事件消息中的 ``data`` 字段值作为参数
  52. """
  53. raise NotImplementedError