Websocket( ウェブソケット)
Plugin 説明内容の英語表記部分について Plugin の各章は、原文データからページ内容の一部が自動生成されているため、英語表記のままの部分があります。
JavaScript で Rust クライアントを使用して WebSocket 接続を開きます。
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
はじめに、「websocket」プラグインをインストールしてください。
自分のプロジェクトのパッケージ・マネージャーを使用して依存関係を追加します:
npm run tauri add websocketyarn run tauri add websocketpnpm tauri add websocketdeno task tauri add websocketbun tauri add websocketcargo tauri add websocket-
src-tauriフォルダで次のコマンドを実行して、このプラグインをCargo.toml内のプロジェクトの依存関係に追加します:cargo add tauri-plugin-websocket -
追加したプラグインを初期化するために
lib.rsを修正します: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");} -
お好みの JavaScript パッケージ・マネージャーを使用して、「JavaScript Guest」バインディングをインストールします:
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
「Websocket」プラグインは JavaScript で利用できます。
import WebSocket from '@tauri-apps/plugin-websocket';// `"withGlobalTauri": true` を使用する場合は、// const WebSocket = window.__TAURI__.websocket; を使用できます
const ws = await WebSocket.connect('ws://127.0.0.1:8080');
const removeListener = ws.addListener((msg) => { console.log('Received Message:', msg);});
await ws.send('Hello World!');
// オプションでリスナーを削除しますremoveListener();
await ws.disconnect();デフォルトでは、潜在的に危険なプラグイン・コマンドとそのスコープ(有効範囲)はすべてブロックされており、アクセスできません。これらを有効にするには、capabilities 設定でアクセス権限を変更する必要があります。
詳細については「セキュリティ・レベル Capabilities」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。
{ "$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
This default permission set includes the following:
allow-connectallow-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. |
【※ この日本語版は、「Nov 19, 2025 英語版」に基づいています】
© 2025 Tauri Contributors. CC-BY / MIT