Websocket
使用 JavaScript 中的 Rust 客户端打开 WebSocket 连接。
¥Open a WebSocket connection using a Rust client in JavaScript.
支持的平台
¥Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
设置
¥Setup
安装 websocket 插件即可开始使用。
¥Install the websocket plugin to get started.
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
npm run tauri add websocket
yarn run tauri add websocket
pnpm tauri add websocket
deno task tauri add websocket
bun tauri add websocket
cargo tauri add websocket
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-websocket -
Modify
lib.rs
to initialize the plugin:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_websocket::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-websocketyarn add @tauri-apps/plugin-websocketpnpm add @tauri-apps/plugin-websocketdeno add npm:@tauri-apps/plugin-websocketbun add @tauri-apps/plugin-websocket
使用
¥Usage
websocket 插件在 JavaScript 中可用。
¥The websocket plugin is available in JavaScript.
import WebSocket from '@tauri-apps/plugin-websocket';// when using `"withGlobalTauri": true`, you may use// const WebSocket = window.__TAURI__.websocket;
const ws = await WebSocket.connect('ws://127.0.0.1:8080');
ws.addListener((msg) => { console.log('Received Message:', msg);});
await ws.send('Hello World!');
await ws.disconnect();
权限
¥Permissions
默认情况下,所有潜在危险的插件命令和范围都会被阻止,无法访问。你必须修改 capabilities
配置中的权限才能启用这些权限。
¥By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities
configuration to enable these.
有关更详细的说明,请参阅 功能概述。
¥See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": ["websocket:default"]}
Default Permission
Allows connecting and sending data to a WebSocket server
allow-connect
allow-send
Permission Table
Identifier | Description |
---|---|
|
Enables the connect command without any pre-configured scope. |
|
Denies the connect command without any pre-configured scope. |
|
Enables the send command without any pre-configured scope. |
|
Denies the send command without any pre-configured scope. |
Tauri v2.4 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站