进程间通信
进程间通信 (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 使用一种称为 异步消息传递 的特定进程间通信样式,其中进程使用一些简单的数据表示来交换序列化的请求和响应。消息传递对于具有 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 Core 进程确定请求是恶意的,它只会丢弃请求并且永远不会执行相应的功能。
¥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 原语 - Events
和 Commands
- 更详细。
¥In the following, we explain Tauri’s two IPC primitives - Events
and Commands
- in more detail.
事件
¥Events
事件是一次性的单向 IPC 消息,最适合传达生命周期事件和状态变化。与 命令 不同,事件可以由前端和 Tauri Core 发出。
¥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.
命令
¥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.
Footnotes
Tauri 中文网 - 粤ICP备13048890号