Tauri如何引入tauri-plugin-websocket插件
在src-tauri/Cargo.toml中添加Cargo依赖
[dependencies]
tauri-plugin-websocket = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
添加npm包依赖
pnpm add https://github.com/tauri-apps/tauri-plugin-websocket
# or
npm add https://github.com/tauri-apps/tauri-plugin-websocket
# or
yarn add https://github.com/tauri-apps/tauri-plugin-websocket
在src-tauri/src/main.rs文件中初始化
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_websocket::init()) //添加这一行即可
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
在App.vue文件中测试使用
import WebSocket from "tauri-plugin-websocket-api";
const ws = await WebSocket.connect("ws://example.com");
await ws.send("Hello World");
await ws.disconnect();
ws.addListener((e) => {
console.log(e.data)
})