@foxglove/ws-protocol-examples
Version:
Foxglove WebSocket protocol examples
79 lines • 3.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const rosmsg_serialization_1 = require("@foxglove/rosmsg-serialization");
const rosmsg2_serialization_1 = require("@foxglove/rosmsg2-serialization");
const ws_protocol_1 = require("@foxglove/ws-protocol");
const commander_1 = require("commander");
const debug_1 = tslib_1.__importDefault(require("debug"));
const ws_1 = tslib_1.__importDefault(require("ws"));
const log = (0, debug_1.default)("foxglove:service-client");
debug_1.default.enable("foxglove:*");
const SUPPORTED_MSG_ENCODINGS = ["json", "ros1", "cdr"];
async function main(url) {
const address = url.startsWith("ws://") || url.startsWith("wss://") ? url : `ws://${url}`;
let fallbackMsgEncoding;
log(`Client connecting to ${address}`);
const client = new ws_protocol_1.FoxgloveClient({
ws: new ws_1.default(address, [ws_protocol_1.FoxgloveClient.SUPPORTED_SUBPROTOCOL]),
});
client.on("error", (error) => {
log("Error", error);
throw error;
});
client.on("serverInfo", (serverInfo) => {
const supportedEncodings = serverInfo.supportedEncodings ?? [];
fallbackMsgEncoding = supportedEncodings.find((encoding) => SUPPORTED_MSG_ENCODINGS.includes(encoding));
});
client.on("advertiseServices", (services) => {
const service = services.find((s) => /std_srvs(\/srv)?\/SetBool/.test(s.type));
if (!service) {
return;
}
const msgEncoding = service.request?.encoding ?? fallbackMsgEncoding;
if (msgEncoding == undefined) {
const supportedEndingsStr = SUPPORTED_MSG_ENCODINGS.join(", ");
throw new Error(`Unable to call service ${service.name}: No supported message encoding found. Supported encodings: [${supportedEndingsStr}]`);
}
let requestData = new Uint8Array();
if (msgEncoding === "json") {
requestData = new Uint8Array(Buffer.from(JSON.stringify({ data: true })));
}
else if (msgEncoding === "ros1") {
const writer = new rosmsg_serialization_1.MessageWriter([{ definitions: [{ name: "data", type: "bool" }] }]);
requestData = writer.writeMessage({ data: true });
}
else {
const writer = new rosmsg2_serialization_1.MessageWriter([{ definitions: [{ name: "data", type: "bool" }] }]);
requestData = writer.writeMessage({ data: true });
}
client.sendServiceCallRequest({
serviceId: service.id,
callId: 123,
encoding: msgEncoding,
data: requestData,
});
});
client.on("serviceCallResponse", (response) => {
if (response.encoding === "json") {
const responseData = new TextDecoder().decode(response.data);
console.log(JSON.parse(responseData));
}
else if (response.encoding === "ros1") {
console.log(response);
}
else if (response.encoding === "cdr") {
console.log(response);
}
client.close();
});
client.on("serviceCallFailure", (event) => {
console.error(`Failed to call service ${event.serviceId}: ${event.message}`);
client.close();
});
}
exports.default = new commander_1.Command("service-client")
.description("connect to a server and call the first advertised SetBool service")
.argument("[url]", "ws(s)://host:port", "ws://localhost:8765")
.action(main);
//# sourceMappingURL=service-client.js.map