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

@lmkdbd/signaling

v3.0.9

Published

一个基于WebRTC和WebSocket的现代化信令服务器,支持DID(去中心化身份标识)认证和P2P连接管理。

Readme

Signaling Server

一个基于WebRTC和WebSocket的现代化信令服务器,支持DID(去中心化身份标识)认证和P2P连接管理。

🏗️ 项目架构

📁 目录结构

signaling_new/
├── client/              # 客户端实现
│   └── client.js        # 信令客户端核心逻辑
├── common/              # 跨平台兼容层
│   ├── browser.js       # 浏览器环境适配
│   ├── index.js         # 通用导出
│   └── react-native.js  # React Native环境适配
├── config/              # 配置文件
│   └── config.json      # 服务器配置
├── enum/                # 枚举和常量定义
│   ├── ConnectionStatus.js  # 连接状态枚举
│   ├── ErrorCode.js     # 错误码定义
│   ├── ProtocolType.js  # 协议类型枚举
│   └── index.js         # 统一导出
├── handler/             # HTTP/RPC请求处理层
│   ├── v1/              # API v1版本
│   │   ├── Peer.js      # Peer相关接口
│   │   ├── Register.js  # 注册相关接口
│   │   └── index.js     # v1版本统一导出
│   └── index.js         # Handler统一导出
├── pkg/                 # 核心功能包
│   ├── connections/     # 连接管理
│   │   └── index.js     # 连接管理器工厂
│   ├── did/             # DID身份认证
│   │   └── index.js     # 数字签名和验证
│   ├── singleton/       # 单例模式工具
│   │   └── index.js     # 单例工厂函数
│   └── utils/           # 工具函数
│       └── index.js     # 实用工具
├── service/             # 业务逻辑层
│   ├── PeerService.js   # Peer业务逻辑
│   ├── RegisterService.js # 注册业务逻辑
│   └── index.js         # Service统一导出和依赖注入
├── test/                # 测试文件
└── index.js             # 主入口文件

🎯 架构设计原则

1. 分层架构

  • Handler层: 纯路由,负责HTTP/RPC请求分发
  • Service层: 业务逻辑,使用Class实现,支持依赖注入
  • Package层: 核心功能模块,提供基础能力

2. 依赖注入

  • Service层通过constructor接收依赖
  • 使用单例模式管理连接和服务实例
  • 配置化的依赖关系管理

3. 跨平台支持

  • 统一的接口设计
  • 环境特定的实现适配
  • Browser、Node.js、React Native全平台支持

🚀 快速开始

安装依赖

npm install

配置

编辑 config/config.json:

{
    "challenge_token_expire_seconds": 300,
    "connection": {
        "max_connections": 1000,
        "info_max_bytes": 1000
    }
}

启动服务器

import { CreateServer } from '@lmkdbd/signaling';
import { ProtocolType } from '@lmkdbd/signaling/enum';

// WebSocket服务器
const wsServer = CreateServer({
    protocol: ProtocolType.WEBSOCKET,
    port: 8080
});

// DRTC服务器
const drtcServer = CreateServer({
    protocol: ProtocolType.DRTC,
    port: 8443,
    key: 'path/to/private.key',
    cert: 'path/to/certificate.crt'
});

客户端连接

import { ConnectToSignaling, generateDid } from '@lmkdbd/signaling';

// 生成DID身份
const didKey = await generateDid();

// 连接到信令服务器
const client = await ConnectToSignaling({
    did_key: didKey,
    signaling_url: 'ws://localhost:8080',
    register_data: {
        type: 'webrtc',
        host: didKey.id
    },
    handler: yourMessageHandler
});

// 发送消息到其他peer
await client.SendToPeer(targetDid, 'your/method', data);

📡 API文档

文档生成方式

apidoc -i handler/ -o apidoc/

会在apidoc目录下生成文档

注册相关接口

POST /v1/register/challenge

生成挑战令牌

请求参数:

{
    "data": "7b7d",  // hex编码的挑战数据
    "signature": "...", // 数字签名
    "did": "did:key:...", // DID标识符
    "timestamp": 1234567890
}

响应:

{
    "code": 0,
    "data": {
        "token": "LN5eA69cddTyFQy4AeBTzm6G1w2XYwUj",
        "expires": 1739435311410
    }
}

POST /v1/register/register

完成注册

请求参数:

{
    "data": "7b22746f6b656e...", // 包含token和注册信息的hex数据
    "signature": "...",
    "did": "did:key:...",
    "timestamp": 1234567890
}

Peer相关接口

POST /v1/peer/send

转发消息到指定peer

请求参数:

{
    "peer_did": "did:key:...",
    "method": "your/method/name",
    "data": { /* 任意数据 */ }
}

POST /v1/peer/get_peer

获取peer信息

请求参数:

{
    "peer_did": "did:key:..."
}

🔧 核心特性

1. DID身份认证

  • 基于Ed25519的数字签名
  • 去中心化身份验证
  • 挑战-响应认证机制

2. 连接管理

  • 统一的连接管理器
  • 支持WeakMap和Map两种存储方式
  • 连接数量限制和状态管理

3. 多协议支持

  • WebSocket通信
  • DRTC (Direct RTC) 点对点连接
  • 自动协议检测和路由

4. 错误处理

  • 统一的错误码系统
  • 详细的错误消息
  • 分类的错误类型

5. 跨平台兼容

  • Browser环境支持
  • Node.js环境支持
  • React Native环境支持

🧪 测试


# 运行测试
fibjs test/drtc_test.js
fibjs test/websocket.js

📝 许可证

ISC License

👥 贡献

欢迎提交Issue和Pull Request!

🔗 相关链接