Haply Inverse SDK
Le Haply Inverse SDK 是一种面向触觉设备的 WebSocket + HTTP 语言无关接口Haply —— 即 Inverse3、Inverse3x、 Minverse、VerseGrip和Wireless VerseGrip。它作为本地服务运行,负责设备发现、串行通信、安全监控以及状态广播,因此您的应用程序只需通过套接字使用 JSON 进行通信即可。
其功能包括:
- 设备发现与管理——通过REST HTTP API自动发现并配置已连接的Haply 设备。
- 实时状态广播——通过 WebSockets 将设备状态传输至触觉控制频率(数千赫兹)。
- 命令处理——以极高的精度执行力和位命令,从而实现精准的触觉反馈。
- 后台运行——作为本地服务运行,并在无需用户干预的情况下保持设备处于就绪状态。
使用Haply 集线器进行安装
最简单的入门方法是使用 Haply Hub——这是一款桌面应用程序,可用于安装、运行、配置、测试和监控您的设备Haply 。它能保持固件最新,集成了 Inverse Service,并附带演示程序,让您在编写任何代码之前就能验证硬件是否正常。

Haply Hub
下载最新版本的Haply Hub
下载并安装 Hub,连接您的设备,Hub 将引导您完成整个固件更新过程。安装完成后,每当打开 Hub时,Inverse Service都会在后台自动运行。
您还可以将 Inverse Service 的特定版本直接安装为系统服务(Windows)或守护进程(Linux/macOS),无需通过 Hub 进行安装。请参阅“运行服务”部分,获取安装程序链接和操作说明。
快速示例
连接到该服务,读取第一个Inverse3 的游标位置,并发送一条零强制维持消息,以便该服务继续广播状态帧:
- Python
- JavaScript(Node)
- C++ (nlohmann)
- C++(优化版)
- Rust
import asyncio, json, websockets
async def main():
# Connect to the Haply Inverse service WebSocket
async with websockets.connect("ws://localhost:10001") as ws:
# Read the first state frame to discover the device id
first_state = json.loads(await ws.recv())
device_id = first_state["inverse3"][0]["device_id"]
# Build a zero-force keepalive command targeting that device
keepalive = {"inverse3": [{
"device_id": device_id,
"commands": {"set_cursor_force": {"vector": {"x": 0, "y": 0, "z": 0}}}
}]}
# Realtime loop: one send per tick, then read the resulting state
while True:
await ws.send(json.dumps(keepalive))
state = json.loads(await ws.recv())
pos = state["inverse3"][0]["state"]["cursor_position"]
print(f"pos: {pos}")
asyncio.run(main())
import WebSocket from 'ws'
// Connect to the Haply Inverse service WebSocket
const ws = new WebSocket('ws://localhost:10001')
let keepalive
ws.on('message', (msg) => {
const state = JSON.parse(msg)
if (!keepalive) {
// Read the first state frame to discover the device id
const deviceId = state.inverse3[0].device_id
// Build a zero-force keepalive command targeting that device
keepalive = JSON.stringify({
inverse3: [
{
device_id: deviceId,
commands: { set_cursor_force: { vector: { x: 0, y: 0, z: 0 } } },
},
],
})
ws.send(keepalive)
return
}
// Realtime loop: one send per tick, then read the resulting state
ws.send(keepalive)
console.log('pos:', state.inverse3[0].state.cursor_position)
})
#include <external/libhv.h>
#include <nlohmann/json.hpp>
int main() {
hv::WebSocketClient ws;
nlohmann::json keepalive;
ws.onmessage = [&](const std::string& msg) {
auto state = nlohmann::json::parse(msg);
if (keepalive.is_null()) {
// Read the first state frame to discover the device id
auto device_id = state["inverse3"][0]["device_id"];
// Build a zero-force keepalive command targeting that device
keepalive = {{"inverse3", nlohmann::json::array({{
{"device_id", device_id},
{"commands", {{"set_cursor_force",
{{"vector", {{"x", 0}, {"y", 0}, {"z", 0}}}}}}}
}})}};
ws.send(keepalive.dump());
return;
}
// Realtime loop: one send per tick, then read the resulting state
ws.send(keepalive.dump());
auto pos = state["inverse3"][0]["state"]["cursor_position"];
std::cout << "pos: " << pos << "\n";
};
// Connect to the Haply Inverse service WebSocket
ws.open("ws://localhost:10001");
std::cin.get();
}
利用Glaze(全球最快的 JSON 库之一)进行编译时反射。在触觉频率(几 kHz 量级)下,用于解析 JSON 的每一微秒,都是从控制循环中“偷走”的一微秒,因此这一点至关重要。 仅声明您需要读写的最基本结构;其余部分将被忽略。
#include <external/libhv.h>
#include <glaze/glaze.hpp>
struct vec3 { float x{}, y{}, z{}; };
struct inverse_state { vec3 cursor_position; };
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 device_cmd { std::string device_id;
struct { std::optional<set_cursor_force_cmd> set_cursor_force; } commands; };
struct commands_message { std::vector<device_cmd> inverse3; };
int main() {
hv::WebSocketClient ws;
commands_message keepalive;
ws.onmessage = [&](const std::string& msg) {
devices_message state{};
if (glz::read_json(state, msg)) return;
if (keepalive.inverse3.empty()) {
// Read the first state frame to discover the device id
auto device_id = state.inverse3[0].device_id;
// Build a zero-force keepalive command targeting that device
keepalive.inverse3.push_back({device_id});
keepalive.inverse3[0].commands.set_cursor_force = set_cursor_force_cmd{};
std::string out; (void)glz::write_json(keepalive, out);
ws.send(out);
return;
}
// Realtime loop: one send per tick, then read the resulting state
std::string out; (void)glz::write_json(keepalive, out);
ws.send(out);
printf("pos: %f %f %f\n", state.inverse3[0].state.cursor_position.x,
state.inverse3[0].state.cursor_position.y,
state.inverse3[0].state.cursor_position.z);
};
// Connect to the Haply Inverse service WebSocket
ws.open("ws://localhost:10001");
std::cin.get();
}
使用……的示例说明 tokio-tungstenite + serde_json.
use futures_util::{SinkExt, StreamExt};
use serde_json::json;
use tokio_tungstenite::{connect_async, tungstenite::Message};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Connect to the Haply Inverse service WebSocket
let (mut ws, _) = connect_async("ws://localhost:10001").await?;
// Read the first state frame to discover the device id
let first = match ws.next().await {
Some(Ok(Message::Text(m))) => m,
_ => anyhow::bail!("no initial state frame"),
};
let first_state: serde_json::Value = serde_json::from_str(&first)?;
let device_id = first_state["inverse3"][0]["device_id"].as_str().unwrap();
// Build a zero-force keepalive command targeting that device
let keepalive = json!({
"inverse3": [{
"device_id": device_id,
"commands": { "set_cursor_force": { "vector": { "x": 0, "y": 0, "z": 0 } } }
}]
}).to_string();
// Realtime loop: one send per tick, then read the resulting state
loop {
ws.send(Message::Text(keepalive.clone())).await?;
let msg = match ws.next().await {
Some(Ok(Message::Text(m))) => m,
_ => break,
};
let state: serde_json::Value = serde_json::from_str(&msg)?;
println!("pos: {}", state["inverse3"][0]["state"]["cursor_position"]);
}
Ok(())
}
请谨慎调整力值。突然将力值调高可能会损坏设备或导致设备出现意外行为。
请参阅“JSON 规范”页面,了解消息封装、端口和内容类型规则的相关信息。
更多示例
如需有关 Python、C++(nlohmann)和 C++(Glaze)的完整教程——涵盖力反馈、位置控制、多设备配置、装配和基准点配置以及事件流——请访问“教程”页面。