はじめに
このプラグインのインストールステップとフルマークダウンガイドまでのステップを含むセットアッププロンプトをコピーしてください。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-network-diagnostics`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/network-diagnostics/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
インストール
「インストール」のセクションCapgoのAI-Assisted Setupを使用してプラグインをインストールできます。AIツールにCapgoスキルを追加するには、以下のコマンドを使用してください。
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins使用するためのパラメータを入力してください。
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-network-diagnostics` plugin in my project.Manual Setupを使用する場合は、以下のコマンドを実行して、下記のプラットフォーム固有の手順に従ってプラグインをインストールしてください。
npm install @capgo/capacitor-network-diagnosticsnpx cap syncインポート
「インポート」のセクションimport { NetworkDiagnostics } from '@capgo/capacitor-network-diagnostics';完全な診断レポートを実行
「完全な診断レポートを実行」のセクションrunDiagnostics ユーザーが制限されたネットワーク上にいる場合のサポートレポートを収集する最速の方法です。
const report = await NetworkDiagnostics.runDiagnostics({ urls: [{ url: 'https://api.example.com/health', method: 'HEAD' }], ports: [{ host: 'api.example.com', port: 443 }], websockets: [{ url: 'wss://ws.example.com/socket' }], download: { url: 'https://speed.example.com/5mb.bin', maxBytes: 5 * 1024 * 1024, }, packetLoss: { mode: 'tcp', host: 'api.example.com', port: 443, count: 10, },});
console.log(report.status.connectionType);console.log(report.issues);現在のネットワーク状態を確認
Section titled “ネットワークの現在の状態を確認する”const status = await NetworkDiagnostics.getNetworkStatus();
console.log(status.connected);console.log(status.connectionType);console.log(status.internetReachable);console.log(status.captivePortal);プラットフォームによっては、正しいフラグが異なる場合があります。Androidは、有効なインターネットとキャプティブ ポータルの状態を報告できます。iOSは、パス状態、インターフェイスタイプ、コストの高いパス、制約されたパスを報告します。 Network.framework.
Test API の可達性を確認する
Section titled “Test API の可達性を確認する”const result = await NetworkDiagnostics.testUrl({ url: 'https://api.example.com/health', method: 'HEAD', timeoutMs: 5000, followRedirects: true,});
if (!result.reachable) { console.warn(result.errorCode, result.errorMessage);}実際のヘルスエンドポイントをバックエンドから使用します。ブラウザのみのチェックは、WebView、CORS、プロキシ、キャプティブ ポータルの動作によって隠される可能性があります。このプラグインは、ネイティブ ネットワーキングを使用するため、異なる結果が得られます。
TCP ポートをテストする
Section titled “TCP ポートをテストする”const port = await NetworkDiagnostics.testPort({ host: 'api.example.com', port: 443, timeoutMs: 3000,});
console.log(port.open, port.durationMs);Wi-Fi アクセス ポイントが標準以外のポート、MQTT、カスタム ゲートウェイ、またはプライベート バックエンドをブロックしながらでも通常のブラウジングを許可している場合に便利です。
WebSockets の接続性をテストする
セクション「WebSocket接続のテスト」const socket = await NetworkDiagnostics.testWebSocket({ url: 'wss://ws.example.com/socket', timeoutMs: 5000,});
console.log(socket.open, socket.statusCode);プロキシやキャプティブ ポータルがHTTPSページを許可するが、WebSocketアップグレード要求をブロックする場合に使用します。
ダウンロード速度を測定
セクション「ダウンロード速度の測定」const speed = await NetworkDiagnostics.testDownloadSpeed({ url: 'https://speed.example.com/5mb.bin', maxBytes: 5 * 1024 * 1024, timeoutMs: 30000,});
console.log(speed.mbps);自分の静的ファイルエンドポイントを使用して、結果がアプリが必要とするネットワークパスを反映するようにします。
パケットロスの推定
セクション「パケットロスの推定」const loss = await NetworkDiagnostics.testPacketLoss({ mode: 'tcp', host: 'api.example.com', port: 443, count: 10, timeoutMs: 3000, intervalMs: 250,});
console.log(loss.lossPercent);App StoreやPlay StoreのアプリでポータブルではないRaw ICMP Pingを使用する代わりに、TCPまたはHTTPプローブを繰り返してアプリケーションレベルのパケットロスを測定します。
Webフォールバック
「Web fallback」セクション開発用にWeb実装を使用します。ブラウザでは、RAW TCPソケットを開くことはできず、URLチェックはCORSによって制限される可能性があります。iOSまたはAndroidビルドを使用して、実際のサポート診断を実行してください。