:github_url: hide .. _class_JSONRPC: JSONRPC ======= **继承:** :ref:`Object` 用于处理看起来像 JSONRPC 文档的字典的辅助类。 .. rst-class:: classref-introduction-group 描述 ---- `JSON-RPC `__ 是一项标准,它将方法调用包装在一个 :ref:`JSON` 对象中。该对象有一个特定的结构,并标识出哪个方法被调用,该函数的参数,并携带一个 ID 来跟踪响应。这个类在 :ref:`Dictionary` 之上实现了该标准;你必须用其他函数在 :ref:`Dictionary` 和 :ref:`JSON` 之间进行转换。 .. rst-class:: classref-reftable-group 方法 ---- .. table:: :widths: auto +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Dictionary` | :ref:`make_notification`\ (\ method\: :ref:`String`, params\: :ref:`Variant`\ ) | +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Dictionary` | :ref:`make_request`\ (\ method\: :ref:`String`, params\: :ref:`Variant`, id\: :ref:`Variant`\ ) | +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Dictionary` | :ref:`make_response`\ (\ result\: :ref:`Variant`, id\: :ref:`Variant`\ ) | +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Dictionary` | :ref:`make_response_error`\ (\ code\: :ref:`int`, message\: :ref:`String`, id\: :ref:`Variant` = null\ ) |const| | +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`Variant` | :ref:`process_action`\ (\ action\: :ref:`Variant`, recurse\: :ref:`bool` = false\ ) | +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`process_string`\ (\ action\: :ref:`String`\ ) | +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |void| | :ref:`set_method`\ (\ name\: :ref:`String`, callback\: :ref:`Callable`\ ) | +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 枚举 ---- .. _enum_JSONRPC_ErrorCode: .. rst-class:: classref-enumeration enum **ErrorCode**: :ref:`🔗` .. _class_JSONRPC_constant_PARSE_ERROR: .. rst-class:: classref-enumeration-constant :ref:`ErrorCode` **PARSE_ERROR** = ``-32700`` 无法解析请求,因为不符合 JSON 标准(\ :ref:`JSON.parse()` 失败)。 .. _class_JSONRPC_constant_INVALID_REQUEST: .. rst-class:: classref-enumeration-constant :ref:`ErrorCode` **INVALID_REQUEST** = ``-32600`` 请求进行方法调用,但是请求格式无效。 .. _class_JSONRPC_constant_METHOD_NOT_FOUND: .. rst-class:: classref-enumeration-constant :ref:`ErrorCode` **METHOD_NOT_FOUND** = ``-32601`` 请求进行方法调用,但是 JSONRPC 子类中不存在该名称的函数。 .. _class_JSONRPC_constant_INVALID_PARAMS: .. rst-class:: classref-enumeration-constant :ref:`ErrorCode` **INVALID_PARAMS** = ``-32602`` 请求进行方法调用,但是给定的方法参数无效。内置 JSONRPC 未使用。 .. _class_JSONRPC_constant_INTERNAL_ERROR: .. rst-class:: classref-enumeration-constant :ref:`ErrorCode` **INTERNAL_ERROR** = ``-32603`` 处理请求时发生内部错误。内置 JSONRPC 未使用。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 方法说明 -------- .. _class_JSONRPC_method_make_notification: .. rst-class:: classref-method :ref:`Dictionary` **make_notification**\ (\ method\: :ref:`String`, params\: :ref:`Variant`\ ) :ref:`🔗` 返回 JSON-RPC 通知形式的字典。通知是一次性的信息,不需要有响应。 - ``method``\ :被调用的方法的名称。 - ``params``\ :传递给该被调用的方法的参数的数组或字典。 .. rst-class:: classref-item-separator ---- .. _class_JSONRPC_method_make_request: .. rst-class:: classref-method :ref:`Dictionary` **make_request**\ (\ method\: :ref:`String`, params\: :ref:`Variant`, id\: :ref:`Variant`\ ) :ref:`🔗` 以 JSON-RPC 请求的形式返回字典。请求被发送到服务器并期望得到响应。ID 字段用于服务器指定它正在响应的确切请求。 - ``method``\ :被调用的方法的名称。 - ``params``\ :传递给该被调用的方法的参数的数组或字典。 - ``id``\ :唯一标识该请求。服务器应发送具有相同 ID 的响应。 .. rst-class:: classref-item-separator ---- .. _class_JSONRPC_method_make_response: .. rst-class:: classref-method :ref:`Dictionary` **make_response**\ (\ result\: :ref:`Variant`, id\: :ref:`Variant`\ ) :ref:`🔗` 当服务器接收并处理了请求时,它应该发送响应。如果不想要响应,则需要发送通知。 - ``result``\ :被调用的函数的返回值。 - ``id``\ :该响应针对的请求的 ID。 .. rst-class:: classref-item-separator ---- .. _class_JSONRPC_method_make_response_error: .. rst-class:: classref-method :ref:`Dictionary` **make_response_error**\ (\ code\: :ref:`int`, message\: :ref:`String`, id\: :ref:`Variant` = null\ ) |const| :ref:`🔗` 创建响应,指示先前的回复以某种方式失败。 - ``code``\ :这是哪种错误对应的错误代码。请参阅 :ref:`ErrorCode` 常量。 - ``message``\ :关于该错误的自定义消息。 - ``id``\ :该错误作为响应对应的请求。 .. rst-class:: classref-item-separator ---- .. _class_JSONRPC_method_process_action: .. rst-class:: classref-method :ref:`Variant` **process_action**\ (\ action\: :ref:`Variant`, recurse\: :ref:`bool` = false\ ) :ref:`🔗` 给定采用 JSON-RPC 请求形式的字典:解压请求并运行它。通过查看名为“method”的字段,并在 JSONRPC 对象中查找等效命名的函数来解析方法。如果找到,则调用该方法。 要添加新的受支持方法,请扩展 JSONRPC 类并在你的子类上调用 :ref:`process_action()`\ 。 \ ``action``\ :要运行的动作,作为 JSON-RPC 请求或通知形式的字典。 .. rst-class:: classref-item-separator ---- .. _class_JSONRPC_method_process_string: .. rst-class:: classref-method :ref:`String` **process_string**\ (\ action\: :ref:`String`\ ) :ref:`🔗` .. container:: contribute There is currently no description for this method. Please help us by `contributing one `__! .. rst-class:: classref-item-separator ---- .. _class_JSONRPC_method_set_method: .. rst-class:: classref-method |void| **set_method**\ (\ name\: :ref:`String`, callback\: :ref:`Callable`\ ) :ref:`🔗` Registers a callback for the given method name. - ``name``: The name that clients can use to access the callback. - ``callback``: The callback which will handle the specified method. .. |virtual| replace:: :abbr:`virtual (本方法通常需要用户覆盖才能生效。)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (本方法无副作用,不会修改该实例的任何成员变量。)` .. |vararg| replace:: :abbr:`vararg (本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。)` .. |constructor| replace:: :abbr:`constructor (本方法用于构造某个类型。)` .. |static| replace:: :abbr:`static (调用本方法无需实例,可直接使用类名进行调用。)` .. |operator| replace:: :abbr:`operator (本方法描述的是使用本类型作为左操作数的有效运算符。)` .. |bitfield| replace:: :abbr:`BitField (这个值是由下列位标志构成位掩码的整数。)` .. |void| replace:: :abbr:`void (无返回值。)`