コンテンツにスキップ
Tauri

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 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」の章を参照してください。また、プラグインのアクセス権限を設定するには「プライグン・アクセス権の使用」の章のステップ・バイ・ステップ・ガイドを参照してください。

src-tauri/capabilities/default.json
{
"$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-connect
  • allow-send

Permission Table

Identifier Description

websocket:allow-connect

Enables the connect command without any pre-configured scope.

websocket:deny-connect

Denies the connect command without any pre-configured scope.

websocket:allow-send

Enables the send command without any pre-configured scope.

websocket:deny-send

Denies the send command without any pre-configured scope.

【※ この日本語版は、「Nov 19, 2025 英語版」に基づいています】


© 2025 Tauri Contributors. CC-BY / MIT