base.py 1.7 KB

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