@foxglove/ws-protocol

中文文档
0.8.0发布于 2025/03/14MITTypeScriptESM + CJS构建来源已验证近 30 天下载 15万

Foxglove WebSocket 协议

中文使用文档
文档目录5 节
  1. 安装
  2. 实例
  3. 服务器模板
  4. 客户端模板
  5. 发展

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 标签

版本与发布记录

@foxglove/ws-protocol 共发布 28 个版本,首次发布于 2021/11/25;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.0latest2025/03/141.7 年前
0.7.52025/01/311.8 年前
0.7.42024/07/302 年前
0.7.32024/04/252 年前
0.7.22023/11/103 年前
0.7.12023/08/093 年前

关于 @foxglove/ws-protocol 的常见问题

@foxglove/ws-protocol 是什么?

Foxglove WebSocket 协议

如何安装 @foxglove/ws-protocol?

在项目目录运行 npm install @foxglove/ws-protocol,或使用 pnpm add / yarn add / bun add。

@foxglove/ws-protocol 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@foxglove/ws-protocol 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@foxglove/ws-protocol 可以商用吗?

@foxglove/ws-protocol 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@foxglove/ws-protocol 的下载量有多少?

根据 npm 官方下载统计,@foxglove/ws-protocol 在 2026/10/16 – 2026/11/14 这 30 天内被下载 150,156 次,日均约 5,005 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@foxglove/ws-protocol 还在维护吗?

最新版本 0.8.0 发布于 2025/03/14(1.7 年前);至今共发布 28 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/11/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。