UNPKG

@foxglove/ws-protocol-examples

Version:

Foxglove WebSocket protocol examples

95 lines 3.56 kB
"use strict"; 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:publish-client"); debug_1.default.enable("foxglove:*"); let running = true; process.on("SIGINT", () => { running = false; }); async function main(url, args) { const address = url.startsWith("ws://") || url.startsWith("wss://") ? url : `ws://${url}`; 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("open", () => { (async () => { if (args.encoding === "json") { await sendJsonMessages(client); } else if (args.encoding === "ros1") { await sendRos1Messages(client); } else { await sendRos2Messages(client); } client.close(); })().catch((error) => { console.error(error); }); }); } async function sendJsonMessages(client) { const channelId = client.advertise({ topic: "/chatter", encoding: "json", schemaName: "std_msgs/String", }); while (running) { const data = `hello world ${Date.now()}`; const message = new Uint8Array(Buffer.from(JSON.stringify({ data }))); client.sendMessage(channelId, message); await delay(1000); } } async function sendRos1Messages(client) { const channelId = client.advertise({ topic: "/chatter", encoding: "ros1", schemaName: "std_msgs/String", }); const writer = new rosmsg_serialization_1.MessageWriter([{ definitions: [{ name: "data", type: "string" }] }]); while (running) { const data = `hello world ${Date.now()}`; const message = writer.writeMessage({ data }); client.sendMessage(channelId, message); await delay(1000); } } async function sendRos2Messages(client) { const channelId = client.advertise({ topic: "/chatter", encoding: "cdr", schemaName: "std_msgs/msg/String", }); const writer = new rosmsg2_serialization_1.MessageWriter([{ definitions: [{ name: "data", type: "string" }] }]); while (running) { const data = `hello world ${Date.now()}`; const message = writer.writeMessage({ data }); client.sendMessage(channelId, message); await delay(1000); } } // eslint-disable-next-line @typescript-eslint/promise-function-async function delay(durationMs) { return new Promise((resolve) => setTimeout(resolve, durationMs)); } exports.default = new commander_1.Command("publish-client") .description("connect to a server, advertise a channel, and publish to it") .addOption(new commander_1.Option("-e, --encoding <encoding>", "message encoding") .choices(["json", "ros1", "cdr"]) .default("json")) .argument("[url]", "ws(s)://host:port", "ws://localhost:8765") .action(main); //# sourceMappingURL=publish-client.js.map