01.Inverse3印刷
シミュレーションのWebSocketに接続し、Inverse3 報告するInverse3 、カーソルの位置、速度、および力をストリーミングします。
学習内容:
- WebSocket接続を開き、初期のフルステートメッセージを受信する
- ゼロフォースを送信する
set_cursor_forceキープアライブでセッションを維持する - Haply シミュレーションを認識できるように、セッションプロファイルを登録する
- 「初回メッセージのみ」のハンドシェイクパターン — 初回送信後にセッションの削除および設定を行う
- コンソールの出力を読みやすい速度に調整する
ワークフロー
- WebSocketを開く
ws://localhost:10001. このサービスは直ちに フルステート・フレーム 接続されたデバイスの一覧表示。 - 最初のフレームでは、最初のInverse3を選択してください
device_idそして、2つの部分からなるリクエストメッセージを作成します:session.configure.profile.name— シミュレーションを登録します Haply.- デバイスごとの
set_cursor_forceゼロベクトルを伴うコマンド。このサービスはこれをキープアライブとして利用しており、コマンドが到着し続ける限り、状態フレームを送信し続けます。
- そのメッセージを返信してください。 の皮をむく
sessionフィールド 次のティックまで — セッションプロファイルはワンショットのハンドシェイクであり、その後のティックではコマンドのみが送信されます。 - その後の各状態フレーム:カーソルを表示する
vec3(位置、速度、力)の各フィールドを約10 Hzにサンプリングレート制限し、ゼロ力キープアライブを再送信する。
パラメータ
| 名称 | デフォルト | 目的 |
|---|---|---|
URI | ws://localhost:10001 | シミュレーションチャンネルのWebSocket URL |
PRINT_EVERY_MS | 100 | コンソール出力スロットル |
| セッションプロファイル名 | co.haply.inverse.tutorials:print-inverse3 | Haply 上でこのシミュレーションを特定します |
状態フィールドを読み取る
差出人 data.inverse3[0].state:
cursor_position,cursor_velocity,current_cursor_force—vec3それぞれ
送信/受信
WebSocketのループ:ステートフレームを受信し、 コマンドフレーム. 最初のコマンドフレームには、セッションハンドシェイクとゼロフォースが含まれる set_cursor_force keepalive; それ以降の各フレームには ただ キープアライブ(セッション情報は削除されます)。
- Python
- C++ (nlohmann)
- C++ (Glaze)
単一の非同期ループ — recv() → build コマンド → send() → 繰り返す。
async with websockets.connect(URI) as websocket:
while True:
msg = await websocket.recv()
data = json.loads(msg)
if first_message:
first_message = False
device_id = data["inverse3"][0]["device_id"]
request_msg = {
"session": {"configure": {"profile": {
"name": "co.haply.inverse.tutorials:print-inverse3"}}},
"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_force":
{"vector": {"x": 0.0, "y": 0.0, "z": 0.0}}},
}]
}
await websocket.send(json.dumps(request_msg))
request_msg.pop("session", None) # one-shot handshake
libhvは、独自のI/Oスレッド上でWebSocketを処理します。フレームごとの処理は ws.onmessage. メインスレッドがENTERでブロックされてしまいます。
ws.onmessage = [&](const std::string &msg) {
const json data = json::parse(msg);
if (first_message) {
first_message = false;
device_id = data["inverse3"][0].at("device_id").get<std::string>();
request_msg = {
{"session", {{"configure", {{"profile",
{{"name", "co.haply.inverse.tutorials:print-inverse3"}}}}}}},
{"inverse3", json::array({
{{"device_id", device_id},
{"commands", {{"set_cursor_force",
{{"vector", {{"x", 0.0}, {"y", 0.0}, {"z", 0.0}}}}}}}},
})},
};
}
ws.send(request_msg.dump());
request_msg.erase("session"); // one-shot handshake
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
nlohmann バリアントと同じ libhv コールバックモデルを採用しており、変更点は本体のみです。Glaze はコンパイル時のリフレクションを使用します:JSON の構造を反映した構造体を宣言し、 glz::read / glz::write_json. std::optional<session_cmd> ワンショット・ハンドシェイクを保持します。この設定がオフの場合、GlazeはシリアライズされたJSONからこのフィールドを省略します。
// Struct models
struct vec3 { float x{}, y{}, z{}; };
struct inverse_state {
vec3 cursor_position{}, cursor_velocity{}, current_cursor_force{};
/* + body_orientation, angular_position, angular_velocity */
};
struct inverse_device { std::string device_id; inverse_state state; };
struct devices_message { std::vector<inverse_device> inverse3; };
struct set_cursor_force_cmd { vec3 vector; };
struct commands_message {
std::optional<session_cmd> session; // omitted from JSON when unset
std::vector<device_commands> inverse3;
};
// Send / receive
ws.onmessage = [&](const std::string &msg) {
devices_message data{};
if (glz::read<glz_settings>(data, msg)) return;
commands_message out_cmds{};
if (first_message) {
first_message = false;
out_cmds.session = session_cmd{ /* profile = print-inverse3 */ };
}
// ... populate out_cmds.inverse3 with zero-force keepalive ...
std::string out_json;
(void)glz::write_json(out_cmds, out_json);
ws.send(out_json);
};
ws.open("ws://localhost:10001");
while (std::cin.get() != '\n') {} // block main thread
関連: WebSocketプロトコル ・ 制御コマンド (set_cursor_force) ・ セッション ・ 型 (vec3)