npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@thejrsoft/subway-protocol

v1.4.1

Published

Shared WebSocket protocol definitions for JRSoft Subway - Enhanced with clientId fields for response tracking

Readme

JRSoft Subway WebSocket 协议

统一的 WebSocket 协议定义,供 Gateway、Backend 和 Edge 组件使用。

📋 概述

JRSoft Subway Protocol 是一个以 Gateway 为中心的实时通信协议,设计原则:

  • 简单性优先 - 基础功能使用最简消息格式
  • 渐进增强 - 通过可选字段支持高级功能
  • 统一标准 - 所有组件使用 v1.0 协议
  • 类型安全 - 完整的 TypeScript 支持

🏗️ 系统架构

┌─────────────┐     ┌─────────────┐     ┌──────────┐     ┌──────────┐
│   Backend   │────▶│   Gateway   │────▶│   Edge   │────▶│  Device  │
│  (18082)    │◀────│   (18081)   │◀────│ (Proxy)  │◀────│(Client)  │
└─────────────┘     └─────────────┘     └──────────┘     └──────────┘

📚 文档结构

01. 协议规范

02. 命令系统

03. 架构设计

04. 集成指南

05. 示例代码

  • 命令示例 - 各类命令的使用示例
  • 流程示例 - 完整的业务流程
  • 代码片段 - 常用代码模板

06. 参考文档

  • API 参考 - 完整的 API 文档
  • 术语表 - 专业术语解释
  • 常见问题 - FAQ

🚀 快速开始

安装

npm install jrsoft-subway-protocol

基本使用

import { 
  createSimpleCommand,
  validateMessage,
  GatewayClient 
} from 'jrsoft-subway-protocol';

// 创建命令
const command = createSimpleCommand({
  targetClientId: 'td-01',  // 直接指定设备ID
  commandCode: 'SET_BRIGHTNESS',
  deviceType: 'screen',
  deviceId: 1,
  operationType: 'write',
  parameters: { brightness: 80 }
});

// 验证消息
const valid = validateMessage(command);

// 发送命令
const client = new GatewayClient('ws://localhost:18081');
const response = await client.sendCommand(command);

强类型命令

import { createLedSwitchCommand } from 'jrsoft-subway-protocol';

// TypeScript 提供完整的类型检查和智能提示
const ledCommand = createLedSwitchCommand(
  'req-123',           // requestRef
  'td-01',             // targetClientId (设备ID)
  15,                  // deviceId (子硬件ID)
  'ON',                // switch: 'ON' | 'OFF'
  'write'              // operationType
);

🔑 核心特性

消息类型

  • register - 客户端注册
  • command - 命令请求
  • command_response - 命令响应
  • progress_update - 进度更新
  • program - 节目上传
  • heartbeat - 心跳保活
  • error - 错误消息

命令类型

  • Simple - 单次请求响应的简单命令
  • Batch - 批量控制多个子硬件
  • Complex - 支持多次响应的复杂命令

客户端类型

  • BACKEND - 业务后端服务
  • GATEWAY - 中心路由网关
  • EDGE - 设备代理节点
  • DEVICE - 终端设备

🛠️ 开发工具

类型定义

// 所有类型定义都已导出
import type { 
  CommandMessage,
  CommandResponseMessage,
  ProgressUpdateMessage 
} from 'jrsoft-subway-protocol';

验证工具

// 消息验证
validateMessage(message);

// 类型守卫
if (isCommandMessage(message)) {
  // TypeScript knows this is CommandMessage
}

工厂函数

// 创建各种消息
createRegisterMessage(clientId, clientType);
createHeartbeatMessage(clientId);
createErrorMessage(code, message);

📦 项目结构

jrsoft-subway-protocol/
├── src/                    # 源代码
│   ├── index.ts           # 主入口,协议定义
│   ├── command-types.ts   # 强类型命令定义
│   ├── command-factory.ts # 命令工厂函数
│   └── __tests__/         # 单元测试
├── docs/                   # 文档
│   ├── 01-protocol/       # 协议规范
│   ├── 02-commands/       # 命令系统
│   ├── 03-architecture/   # 架构设计
│   ├── 04-integration/    # 集成指南
│   ├── 05-examples/       # 示例代码
│   └── 06-reference/      # 参考文档
├── examples/               # 示例项目
└── package.json           # 项目配置

📊 版本信息

  • 当前版本: 1.0.6
  • 协议版本: 1.0
  • 最低 Node.js: 14.0.0
  • TypeScript: 4.5+

🌟 核心功能

连接管理

  • 客户端注册与注销(含 ACK 确认)
  • 心跳保活机制(含服务器状态)
  • Edge 代理设备注册
  • 会话管理和认证

命令系统

  • Simple 命令 - 快速点对点控制
  • Batch 命令 - 高效批量操作
  • Complex 命令 - 复杂交互流程
  • 强类型命令 - 基于 C# 模型的类型安全

进度报告

  • 统一的 progress_update 机制
  • 支持自定义阶段
  • 结构化日志(ReportMessage)
  • 设备操作追踪

程序管理

  • 大文件上传支持
  • 多阶段进度反馈
  • 断点续传能力
  • 完整性校验

🤝 贡献指南

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m 'Add amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 创建 Pull Request

📝 更新日志

查看 CHANGELOG.md 了解版本更新历史。

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。

🔗 相关项目

💬 联系方式