UNPKG

@foxglove/ws-protocol-examples

Version:

Foxglove WebSocket protocol examples

50 lines 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); 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:param-client"); debug_1.default.enable("foxglove:*"); async function main(url) { const address = url.startsWith("ws://") || url.startsWith("wss://") ? url : `ws://${url}`; const initialParamRequest = "initialRequest"; 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("parameterValues", (event) => { if (event.id === initialParamRequest) { console.log(event.parameters); console.log(`Subscribing to ${event.parameters.length} parameters`); client.subscribeParameterUpdates(event.parameters.map((p) => p.name)); } else { console.log(`Received ${event.parameters.length} parameter updates`); console.log(event.parameters); } // Periodically change some parameter value to see subscriptions working setTimeout(() => { const toggledBoolParams = event.parameters .filter((p) => typeof p.value === "boolean") .map((p) => ({ ...p, value: !p.value })); client.setParameters(toggledBoolParams); }, 1000); }); client.on("serverInfo", (event) => { console.assert(event.capabilities.includes(ws_protocol_1.ServerCapability.parameters), `Capability ${ws_protocol_1.ServerCapability.parameters} is missing`); console.assert(event.capabilities.includes(ws_protocol_1.ServerCapability.parametersSubscribe), `Capability ${ws_protocol_1.ServerCapability.parametersSubscribe} is missing`); // Get all available parameters. client.getParameters([], initialParamRequest); }); } exports.default = new commander_1.Command("param-client") .description("connect to a server and subscribe to all available parameters") .argument("[url]", "ws(s)://host:port", "ws://localhost:8765") .action(main); //# sourceMappingURL=param-client.js.map