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 🙏

© 2026 – Pkg Stats / Ryan Hefner

my-websocket-server

v1.0.1

Published

一个基于 Node.js 的轻量级 WebSocket 服务器实现,无需第三方依赖,完全从零实现 WebSocket 协议。

Readme

My WebSocket Server

一个基于 Node.js 的轻量级 WebSocket 服务器实现,无需第三方依赖,完全从零实现 WebSocket 协议。

✨ 特性

  • 🚀 零依赖: 完全基于 Node.js 内置模块实现
  • 📦 轻量级: 代码简洁,易于理解和扩展
  • 🔧 自定义实现: 手动实现 WebSocket 协议帧解析和构造
  • 💡 教育友好: 代码结构清晰,适合学习 WebSocket 协议原理
  • 🎯 CLI 支持: 提供命令行工具,支持参数配置

📦 安装

# 安装依赖(无第三方依赖)
npm install

# 全局安装命令行工具
npm install -g .

🚀 快速开始

启动服务器

# 使用默认端口 8080
my-websocket-server

# 指定端口
my-websocket-server -p 9090

# 查看帮助
my-websocket-server --help

客户端连接

打开项目中的 src/index.html 文件,或者使用以下代码连接:

const ws = new WebSocket("ws://localhost:8080");

ws.onopen = () => {
  console.log("WebSocket连接已建立");
  ws.send("Hello, WebSocket!");
};

ws.onmessage = (event) => {
  console.log("收到消息:", event.data);
};

ws.onclose = () => {
  console.log("WebSocket连接已关闭");
};

ws.onerror = (error) => {
  console.error("WebSocket错误:", error);
};

📖 API 文档

WebSocketServer 类

构造函数

const WebSocketServer = require("./src/ws");
const server = new WebSocketServer(options);

参数:

  • options (Object)
    • port (Number): 服务器监听端口,默认 8080

事件

'message'

当收到客户端消息时触发。

server.on("message", (message, socket) => {
  console.log("收到消息:", message.toString());
});

参数:

  • message (Buffer): 收到的消息内容
  • socket (Socket): 客户端连接套接字
'close'

当客户端连接关闭时触发。

server.on("close", (socket) => {
  console.log("客户端断开连接");
});

方法

send(message, socket)

向指定客户端发送消息。

server.send("Hello Client!", socket);

参数:

  • message (String): 要发送的消息
  • socket (Socket): 目标客户端套接字

🏗️ 技术实现

WebSocket 协议实现

本项目完全手动实现了 WebSocket 协议的核心功能:

  1. 握手阶段: 处理 HTTP Upgrade 请求,计算 Sec-WebSocket-Accept
  2. 帧解析: 解析 WebSocket 数据帧格式
  3. 帧构造: 构造符合协议的响应帧
  4. 掩码处理: 处理客户端发送的掩码数据

核心模块

src/ws.js

WebSocket 服务器核心实现,包含:

  • WebSocket 协议握手处理
  • 数据帧解析和构造
  • 客户端连接管理
  • 消息收发处理

src/index.js

命令行工具入口,提供:

  • 命令行参数解析
  • 服务器启动和配置
  • 帮助信息显示

src/index.html

测试客户端页面,用于:

  • WebSocket 连接测试
  • 消息收发验证
  • 调试和演示

🔧 开发调试

启动开发服务器

node src/index.js

测试连接

  1. 启动服务器
  2. 在浏览器中打开 src/index.html
  3. 查看控制台输出验证连接

命令行选项

# 显示帮助信息
my-websocket-server --help

# 显示版本信息
my-websocket-server --version

# 指定端口启动
my-websocket-server -p 3000

📋 WebSocket 帧格式

本实现支持的 WebSocket 帧格式:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len |    Extended payload length    |
|I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
|N|V|V|V|       |S|             |   (if payload len==126/127)   |
| |1|2|3|       |K|             |                               |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
|     Extended payload length continued, if payload len == 127  |
+ - - - - - - - - - - - - - - - +-------------------------------+
|                               |Masking-key, if MASK set to 1  |
+-------------------------------+-------------------------------+
| Masking-key (continued)       |          Payload Data         |
+-------------------------------- - - - - - - - - - - - - - - - +
:                     Payload Data continued ...                :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                     Payload Data continued ...                |
+---------------------------------------------------------------+

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📄 许可证

ISC

👨‍💻 作者

lw


💡 提示: 这是一个用于学习 WebSocket 协议的项目。在生产环境中,建议使用成熟的 WebSocket 库如 wssocket.io