Skip to content
Tauri 中文网

进程间通信

进程间通信 (IPC) 允许隔离进程安全地进行通信,是构建更复杂应用的关键。

🌐 Inter-Process Communication (IPC) allows isolated processes to communicate securely and is key to building more complex applications.

在以下指南中了解有关特定 IPC 模式的更多信息:

🌐 Learn more about the specific IPC patterns in the following guides:

Tauri 使用一种特定风格的进程间通信(IPC),称为[异步消息传递],其中进程交换使用简单数据表示序列化的_请求_和_响应_。消息传递对于有 web 开发经验的人来说应该很熟悉,因为这种模式用于互联网的客户端-服务器通信。

🌐 Tauri uses a particular style of Inter-Process Communication called Asynchronous Message Passing, where processes exchange requests and responses serialized using some simple data representation. Message Passing should sound familiar to anyone with web development experience, as this paradigm is used for client-server communication on the internet.

消息传递比共享内存或直接函数访问更安全,因为接收方可以根据自己的判断拒绝或丢弃请求。例如,如果 Tauri 核心进程认为某个请求是恶意的,它只需丢弃该请求,而从不执行相应的函数。

🌐 Message passing is a safer technique than shared memory or direct function access because the recipient is free to reject or discard requests as it sees fit. For example, if the Tauri Core process determines a request to be malicious, it simply discards the requests and never executes the corresponding function.

在下文中,我们将更详细地解释 Tauri 的两个 IPC 原语——EventsCommands

🌐 In the following, we explain Tauri’s two IPC primitives - Events and Commands - in more detail.

🌐 Events

事件是一次性触发、一种单向的 IPC 消息,最适合用于传递生命周期事件和状态变化。与命令不同,事件可以由前端_和_ Tauri 核心发出。

🌐 Events are fire-and-forget, one-way IPC messages that are best suited to communicate lifecycle events and state changes. Unlike Commands, Events can be emitted by both the Frontend and the Tauri Core.

Diagram
在核心和 Webview 之间发送的事件。

🌐 Commands

Tauri 还在 IPC 消息之上提供了类似 [外部函数接口] 的抽象1。主要的 API,invoke,类似于浏览器的 fetch API,并允许前端调用 Rust 函数、传递参数并接收数据。

🌐 Tauri also provides a foreign function interface-like abstraction on top of IPC messages1. The primary API, invoke, is similar to the browser’s fetch API and allows the Frontend to invoke Rust functions, pass arguments, and receive data.

因为这个机制在底层使用类似 JSON-RPC 的协议来序列化请求和响应,所以所有参数和返回数据都必须可以序列化为 JSON。

🌐 Because this mechanism uses a JSON-RPC like protocol under the hood to serialize requests and responses, all arguments and return data must be serializable to JSON.

Diagram
命令调用中涉及的IPC消息。
  1. Because Commands still use message passing under the hood, they do not share the same security pitfalls as real FFI interfaces do. 2


Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站