Foxglove WebSocket 服务器和客户端
此软件包提供了服务器执行 Foxglove WebSocket 协议。本协议允许 狐狸集团 输入任意“活”流数据.
安装
$ npm install @foxglove/ws-protocol
此软件包不需要特定的WebSocket服务器或客户端执行,所以您需要安装自己的软件包. 对于Node.js,你可以使用 负 软件包 :
$ npm install ws
实例
运行这些 示例脚本,在 TypeScript 中执行,以启动.
服务器模板
以下模板发布关于一个单一主题的信息 example_msg,使用 JSON 编码消息数据和 杰森·斯克马 来描述信件布局.
const { FoxgloveServer } = require("@foxglove/ws-protocol");
const { WebSocketServer } = require("ws");
function delay(durationSec) {
return new Promise((resolve) => setTimeout(resolve, durationSec * 1000));
}
async function main() {
const server = new FoxgloveServer({ name: "example-server" });
const ws = new WebSocketServer({
port: 8765,
handleProtocols: (protocols) => server.handleProtocols(protocols),
});
ws.on("listening", () => {
console.log("server listening on %s", ws.address());
});
ws.on("connection", (conn, req) => {
const name = `${req.socket.remoteAddress}:${req.socket.remotePort}`;
console.log("connection from %s via %s", name, req.url);
server.handleConnection(conn, name);
});
server.on("subscribe", (chanId) => {
console.log("first client subscribed to %d", chanId);
});
server.on("unsubscribe", (chanId) => {
console.log("last client unsubscribed from %d", chanId);
});
server.on("error", (err) => {
console.error("server error: %o", err);
});
const ch1 = server.addChannel({
topic: "example_msg",
encoding: "json",
schemaName: "ExampleMsg",
schema: JSON.stringify({
type: "object",
properties: {
msg: { type: "string" },
count: { type: "number" },
},
}),
});
const textEncoder = new TextEncoder();
let i = 0;
while (true) {
await delay(0.2);
server.sendMessage(
ch1,
BigInt(Date.now()) * 1_000_000n,
textEncoder.encode(JSON.stringify({ msg: "Hello!", count: ++i })),
);
}
}
main().catch(console.error);
将模板代码复制到一个文件并运行它(例如. node server.js) (中文(简体) ). 然后,对文件进行必要的调整,使这个简单的服务器自定义到您想要的规格.
客户端模板
下面的模板订阅所有频道使用此程序的信息 json 编码 见 @foxglove/ws-protocol-examples 例如,客户端使用 protobuf 编码.
const { FoxgloveClient } = require("@foxglove/ws-protocol");
const { WebSocket } = require("ws");
async function main() {
const client = new FoxgloveClient({
ws: new WebSocket(`ws://localhost:8765`, [FoxgloveClient.SUPPORTED_SUBPROTOCOL]),
});
const deserializers = new Map();
client.on("advertise", (channels) => {
for (const channel of channels) {
if (channel.encoding !== "json") {
console.warn(`Unsupported encoding ${channel.encoding}`);
continue;
}
const subId = client.subscribe(channel.id);
const textDecoder = new TextDecoder();
deserializers.set(subId, (data) => JSON.parse(textDecoder.decode(data)));
}
});
client.on("message", ({ subscriptionId, timestamp, data }) => {
console.log({
subscriptionId,
timestamp,
data: deserializers.get(subscriptionId)(data),
});
});
}
main().catch(console.error);
将模板代码复制成文件(例如. client.js开始一个 Foxglove Websocket 服务器。在单独的终端窗口中,运行客户端代码(例如:. node client.js) (中文(简体) ).
如果您的服务器和客户端运行正确, 您应该看到以下输出 :
$ node client.js
{
subscriptionId: 0,
timestamp: 1638999307183000000n,
data: { msg: 'Hello!', count: 2849 }
}
{
subscriptionId: 0,
timestamp: 1638999307384000000n,
data: { msg: 'Hello!', count: 2850 }
}
...
对文件作必要的调整来自定义这个简单的客户端.
发展
这个软件包生活在一个使用 纱线工作空间,所以大多数命令(除 yarn install) 应为前缀 yarn workspace @foxglove/ws-protocol ...。 。 。 .
yarn install- 安装发展依赖yarn workspace @foxglove/ws-protocol version --patch(或--minor或者说--major) — 增加版本编号并创建适当的 git 标签