WebSocket クイックスタート
以下の接続先を使い、クライアント名を自分のアプリ名に置き換えます。
ws://127.0.0.1:42070/?client=YourAppclient クエリパラメーターは必須です。空白や特殊文字を含む名前は URL エンコードしてください。
エンベロープを送る
Section titled “エンベロープを送る”SendNotification コマンドのテキストメッセージを送ります。jsonData は JSON をエンコードした文字列であり、入れ子のオブジェクトではありません。
{ "sender": "YourApp", "target": "AstralOverlay", "command": "SendNotification", "jsonData": "{\"title\":\"Hello from YourApp\",\"content\":\"Your first notification.\",\"sourceApp\":\"YourApp\"}"}現在の実装は command と jsonData を読み取ります。sender や target で通知を振り分けません。他のコマンドは無視します。送信成功は配信確認ではありません。 target は省略可能で、処理には使われません。例では説明用のラベルとして AstralOverlay を指定していますが、省略しても構いません。
Python の例
Section titled “Python の例”クライアントライブラリをインストールします。
python -m pip install websocket-clientimport jsonfrom websocket import create_connection
payload = { "title": "Build complete", "content": "Your project is ready.", "sourceApp": "BuildHelper", "timeout": 5.0,}message = { "sender": "BuildHelper", "target": "AstralOverlay", "command": "SendNotification", "jsonData": json.dumps(payload),}with create_connection("ws://127.0.0.1:42070/?client=BuildHelper") as connection: connection.send(json.dumps(message))新しい .NET コンソールアプリの場合:
using System;using System.Net.WebSockets;using System.Text;using System.Text.Json;using System.Threading;
using var socket = new ClientWebSocket();using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));await socket.ConnectAsync( new Uri("ws://127.0.0.1:42070/?client=BuildHelper"), timeout.Token);
var payload = new { title = "Build complete", content = "Your project is ready.", sourceApp = "BuildHelper", timeout = 5.0};var envelope = new { sender = "BuildHelper", target = "AstralOverlay", command = "SendNotification", jsonData = JsonSerializer.Serialize(payload)};var bytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(envelope));await socket.SendAsync( new ArraySegment<byte>(bytes), WebSocketMessageType.Text, true, timeout.Token);この例は終了時に切断します。長時間動く連携では接続を再利用でき、接続エラー後は再接続する必要があります。
ペイロードのリファレンスと互換性の制限を参照してください。
