SAEA.MessageSocket 26.4.23.1

dotnet add package SAEA.MessageSocket --version 26.4.23.1
                    
NuGet\Install-Package SAEA.MessageSocket -Version 26.4.23.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SAEA.MessageSocket" Version="26.4.23.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SAEA.MessageSocket" Version="26.4.23.1" />
                    
Directory.Packages.props
<PackageReference Include="SAEA.MessageSocket" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SAEA.MessageSocket --version 26.4.23.1
                    
#r "nuget: SAEA.MessageSocket, 26.4.23.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SAEA.MessageSocket@26.4.23.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SAEA.MessageSocket&version=26.4.23.1
                    
Install as a Cake Addin
#tool nuget:?package=SAEA.MessageSocket&version=26.4.23.1
                    
Install as a Cake Tool

SAEA.MessageSocket - 高性能消息服务器 💬

NuGet version License

English Version | 中文版

基于 SAEA.Sockets 的高性能消息服务器,支持私信、频道、群组三种消息模式,采用 Protobuf 序列化传输。

快速导航 🧭

章节 内容
⚡ 30秒快速开始 最简单的上手示例
🎯 核心特性 框架的主要功能
📐 架构设计 消息模式与工作流程
💡 应用场景 何时选择 SAEA.MessageSocket
📊 性能对比 与其他方案对比
❓ 常见问题 FAQ 快速解答
🔧 核心类 主要类一览
📝 使用示例 详细代码示例

30秒快速开始 ⚡

最快上手方式,只需3步即可运行消息服务器:

Step 1: 安装 NuGet 包

dotnet add package SAEA.MessageSocket

Step 2: 创建消息服务器(仅需5行代码)

using SAEA.MessageSocket;

var server = new MessageServer(port: 39654);
server.OnDisconnected += (id) => Console.WriteLine($"用户断开: {id}");
server.Start();

Step 3: 创建消息客户端连接

var client = new MessageClient("127.0.0.1", 39654);
client.OnPrivateMessage += (msg) => Console.WriteLine($"私信: {msg.Content}");
client.OnChannelMessage += (msg) => Console.WriteLine($"频道: {msg.Content}");
client.OnGroupMessage += (msg) => Console.WriteLine($"群组: {msg.Content}");
client.Connect();
client.SendPrivateMsg("user_002", "Hello!");

就这么简单! 🎉 你已经实现了一个支持三种消息模式的高性能消息系统。


核心特性 🎯

特性 说明 优势
🚀 IOCP 高性能 基于 SAEA.Sockets 完成端口 支持万级并发连接
📦 Protobuf 序列化 高效二进制消息压缩 传输体积小,解析速度快
💬 三种消息模式 私信、频道、群组 满足各种即时通讯场景
📡 频道订阅 发布/订阅模式 动态订阅,灵活推送
👥 群组管理 创建、加入、离开、删除 完整的群组生命周期管理
💓 心跳保活 自动心跳机制 连接状态实时监控
批量发送优化 ClassificationBatcher 批量处理 高效消息分发

架构设计 📐

消息模式架构图

┌─────────────────────────────────────────────────────────────┐
│                  SAEA.MessageSocket 架构                      │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│    ┌─────────────────────────────────────────────────┐      │
│    │                 MessageServer                    │      │
│    │                 (消息服务器)                      │      │
│    └─────────────────────┬───────────────────────────┘      │
│                            │                                 │
│            ┌───────────────┼───────────────┐                 │
│            │               │               │                 │
│     ┌──────▼──────┐ ┌─────▼──────┐ ┌──────▼──────┐         │
│     │ PrivateMsg  │ │ ChannelMsg │ │  GroupMsg   │         │
│     │   (私信)    │ │  (频道)    │ │   (群组)    │         │
│     └──────┬──────┘ └─────┬──────┘ └──────┬──────┘         │
│            │               │               │                 │
│     ┌──────▼──────┐ ┌─────▼──────┐ ┌──────▼──────┐         │
│     │  点对点     │ │  发布订阅  │ │  群组管理   │         │
│     │  一对一     │ │  一对多    │ │   多对多    │         │
│     └─────────────┘ └────────────┘ └─────────────┘         │
│                                                             │
│    ┌─────────────────────────────────────────────────┐      │
│    │              Protobuf 序列化层                   │      │
│    │         ChatMessage / ChatMessageType           │      │
│    └─────────────────────────────────────────────────┘      │
│                            │                                 │
│    ┌───────────────────────▼───────────────────────┐        │
│    │              SAEA.Sockets (IOCP)               │        │
│    │              TCP 底层通信层                     │        │
│    └───────────────────────────────────────────────┘        │
│                                                             │
└─────────────────────────────────────────────────────────────┘

三种消息模式流程图

私信模式 (PrivateMessage):

  客户端A ──► SendPrivateMsg(userB, content) ──► 服务器路由
                                                        │
                                                        ▼
                                              查找 userB 连接
                                                        │
                                                        ▼
  客户端B ◄── OnPrivateMessage 事件 ◄─────────── 发送给 userB


频道模式 (ChannelMessage):

  发布者 ──► SendChannelMsg(channel, content) ──► 服务器
                                                       │
                                                       ▼
                                             查找频道订阅列表
                                                       │
                                           ┌───────────┼───────────┐
                                           ▼           ▼           ▼
                                       订阅者1     订阅者2     订阅者N
                                           │           │           │
                                           ▼           ▼           ▼
                                     OnChannel  OnChannel  OnChannel


群组模式 (GroupMessage):

  创建者 ──► SendCreateGroup(name) ──► 群组创建
      │                                       │
      │                                       ▼
      └──► SendAddMember(name) ──► 成员加入群组
                                              │
  成员 ──► SendGroupMessage(name, msg) ──► 群组广播
                                              │
                                  ┌───────────┼───────────┐
                                  ▼           ▼           ▼
                              成员1       成员2       成员N
                                  │           │           │
                                  ▼           ▼           ▼
                            OnGroup     OnGroup     OnGroup

应用场景 💡

✅ 适合使用 SAEA.MessageSocket 的场景

场景 描述 推荐模式
💬 即时通讯 私聊、群聊、聊天室 私信 + 群组
🎧 在线客服 一对一客服对话系统 私信模式
📰 消息推送 新闻、通知、公告推送 频道订阅
🤝 协作工具 团队实时协作、项目讨论 群组模式
🎬 直播互动 直播间弹幕、互动消息 频道 + 群组
🎮 游戏聊天 游戏内私聊、公会聊天 私信 + 群组

消息模式选择指南

需求场景 推荐模式 理由
一对一私聊 私信模式 点对点传输,效率最高
新闻/公告推送 频道模式 发布订阅,动态管理
团队讨论组 群组模式 支持管理权限,成员控制
直播弹幕 频道模式 高并发订阅,一对多推送

❌ 不适合的场景

场景 推荐替代方案
HTTP API 服务 使用 SAEA.Http 或 ASP.NET
浏览器客户端 使用 SAEA.WebSocket
MQTT 设备通信 使用 SAEA.MQTT
RPC 调用 使用 SAEA.RPC

性能对比 📊

与传统消息方案对比

指标 SAEA.MessageSocket 传统 WebSocket 轮询 HTTP
并发连接数 10,000+ ~5,000 ~1,000
消息延迟 ~1ms ~5ms ~100ms
传输体积 小 (Protobuf) 中 (JSON) 大 (HTTP头)
CPU 利用率 高 (IOCP)
内存占用 池化复用 频繁分配

消息模式性能对比

模式 并发能力 适用规模 典型延迟
私信模式 ⭐⭐⭐⭐⭐ 万级用户 ~1ms
频道模式 ⭐⭐⭐⭐⭐ 百万订阅 ~2ms
群组模式 ⭐⭐⭐⭐ 千人群组 ~5ms

Protobuf vs JSON 序列化

指标 Protobuf JSON
序列化速度 ⭐⭐⭐⭐⭐ ⭐⭐⭐
数据体积 小 (二进制) 大 (文本)
跨语言支持 广泛 广泛
可读性

💡 提示: Protobuf 在消息传输中比 JSON 体积小 3-10 倍,序列化速度快 5-20 倍。


常见问题 ❓

Q1: 三种消息模式有什么区别?

A:

模式 通信方式 典型场景
私信 一对一,点对点传输 私聊、客服对话
频道 一对多,发布/订阅 新闻推送、广播通知
群组 多对多,需创建和管理 团队讨论、群聊

Q2: 如何实现用户登录认证?

A: 在客户端连接后发送登录消息:

var client = new MessageClient("127.0.0.1", 39654);
client.Connect();

// 连接后发送登录认证
client.SendLogin("user_id", "token");

// 服务端可在 OnReceive 中处理登录逻辑

Q3: 频道和群组如何选择?

A:

  • 频道: 适用于单向推送场景(新闻、公告),订阅者只接收消息,无需管理成员
  • 群组: 适用于双向互动场景(讨论组、团队协作),支持成员管理、创建者权限控制

Q4: 如何处理离线消息?

A: SAEA.MessageSocket 专注于实时通信,离线消息需要配合存储方案:

// 服务端示例:存储离线消息
server.OnDisconnected += (id) => 
{
    // 用户离线,标记最后在线时间
    // 配合数据库存储离线消息
};

// 用户上线后拉取离线消息

Q5: 支持消息回调确认吗?

A: 支持,每种消息类型都有对应的 Answer 响应:

// 发送消息后会收到对应 Answer
// 如:PrivateMessage -> PrivateMessageAnswer
// 可通过消息 ID 关联请求和响应

Q6: 如何扩展自定义消息类型?

A: 在 ChatMessageType 枚举基础上扩展,或使用消息内容携带类型信息:

// 利用 Protobuf 的灵活性
// 在消息内容中嵌入自定义类型标识
client.SendPrivateMsg(userId, JsonSerializer.Serialize(new 
{ 
    type = "custom_type", 
    data = yourData 
}));

Q7: 单服务器能支持多少并发?

A: 默认配置支持 1000 个连接,可通过构造函数调整:

// bufferSize: 缓冲区大小
// count: 最大连接数
// timeOut: 超时时间(毫秒)
var server = new MessageSocket(1024, 10000, 30 * 60 * 1000);

实际性能取决于服务器硬件配置,IOCP 模式下万级并发轻松支持。


核心类 🔧

类名 说明
MessageServer 消息服务器,管理连接和消息路由
MessageClient 消息客户端,发送和接收各类消息
ChatMessage 聊天消息协议类(Protobuf 序列化)
ChatMessageType 消息类型枚举
PrivateMessage 私信消息模型
ChannelMessage 频道消息模型
GroupMessage 群组消息模型
ChannelList 频道列表管理
GroupList 群组列表管理
ClassificationBatcher 批量消息处理器

使用示例 📝

基础服务器配置

using SAEA.MessageSocket;

// 创建消息服务器
// bufferSize: 1024, count: 1000000, timeOut: 30分钟
var server = new MessageServer(1024, 1000 * 1000, 30 * 60 * 1000);

server.OnDisconnected += (id) => 
    Console.WriteLine($"用户断开连接: {id}");

server.Start();
Console.WriteLine("消息服务器已启动,端口: 39654");

完整客户端示例

using SAEA.MessageSocket;

var client = new MessageClient("127.0.0.1", 39654);

// 私信事件
client.OnPrivateMessage += (msg) => 
    Console.WriteLine($"[私信] 来自 {msg.Sender}: {msg.Content}");

// 频道消息事件
client.OnChannelMessage += (msg) => 
    Console.WriteLine($"[频道:{msg.Name}] {msg.Content}");

// 群组消息事件
client.OnGroupMessage += (msg) => 
    Console.WriteLine($"[群组:{msg.Name}] {msg.Content}");

// 断开连接事件
client.OnDisconnected += () => 
    Console.WriteLine("连接已断开");

// 连接服务器
client.Connect();

私信消息

// 发送私信给指定用户
client.SendPrivateMsg("user_002", "你好,这是一条私信!");

// OnPrivateMessage 事件会收到对方回复

频道消息(发布/订阅)

// 订阅频道
client.Subscribe("news_channel");
client.Subscribe("sports_channel");

// 发送频道消息(所有订阅该频道的用户都会收到)
client.SendChannelMsg("news_channel", "今日新闻头条...");

// 取消订阅
client.Unsubscribe("news_channel");

// 查看当前订阅的频道
var channels = client.GetSubscribedChannels();

群组消息

// 创建群组(创建者拥有管理权限)
client.SendCreateGroup("project_team");

// 其他用户加入群组
client.SendAddMember("project_team");

// 发送群组消息
client.SendGroupMessage("project_team", "项目进度更新...");

// 离开群组
client.SendRemoveMember("project_team");

// 删除群组(仅创建者可操作)
client.SendRemoveGroup("project_team");

完整示例:多人聊天室

using SAEA.MessageSocket;

// 服务器端
var server = new MessageServer(1024, 1000 * 1000, 30 * 60 * 1000);
server.Start();

// 客户端1 - 创建群组
var client1 = new MessageClient("127.0.0.1", 39654);
client1.OnGroupMessage += (msg) => Console.WriteLine($"Client1: {msg.Content}");
client1.Connect();
client1.SendCreateGroup("chat_room");

// 客户端2 - 加入群组
var client2 = new MessageClient("127.0.0.1", 39654);
client2.OnGroupMessage += (msg) => Console.WriteLine($"Client2: {msg.Content}");
client2.Connect();
client2.SendAddMember("chat_room");

// 客户端3 - 加入群组
var client3 = new MessageClient("127.0.0.1", 39654);
client3.OnGroupMessage += (msg) => Console.WriteLine($"Client3: {msg.Content}");
client3.Connect();
client3.SendAddMember("chat_room");

// 发送群组消息(所有成员都会收到)
client1.SendGroupMessage("chat_room", "大家好!");
client2.SendGroupMessage("chat_room", "你好 client1!");

消息类型

public enum ChatMessageType
{
    Login = 1,                // 登录
    LoginAnswer = 2,          // 登录响应
    Subscribe = 3,            // 订阅频道
    SubscribeAnswer = 4,      // 订阅响应
    UnSubscribe = 5,          // 取消订阅
    UnSubscribeAnswer = 6,    // 取消订阅响应
    ChannelMessage = 7,       // 频道消息
    PrivateMessage = 8,       // 私信消息
    PrivateMessageAnswer = 9, // 私信响应
    CreateGroup = 10,         // 创建群组
    CreateGroupAnswer = 11,   // 创建群组响应
    AddMember = 12,           // 添加成员
    AddMemberAnswer = 13,     // 添加成员响应
    RemoveMember = 14,        // 移除成员
    RemoveMemberAnswer = 15,  // 移除成员响应
    RemoveGroup = 16,         // 删除群组
    RemoveGroupAnswer = 17,   // 删除群组响应
    GroupMessage = 18,        // 群组消息
    GroupMessageAnswer = 19   // 群组消息响应
}

消息模式对比

特性 私信模式 频道模式 群组模式
通信方式 一对一 一对多 多对多
需要创建
需要加入 订阅即可 需要加入
管理权限 创建者拥有
适用场景 私聊、客服 新闻推送、广播 团队协作、群聊

依赖项

包名 版本 说明
SAEA.Sockets 7.26.2.2 IOCP 通信框架
SAEA.Common 7.26.2.2 公共工具类(含 Protobuf)

更多资源


许可证

Apache License 2.0

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on SAEA.MessageSocket:

Repository Stars
yswenli/GFF
GFF is a imitation QQ communication project, based on high IOCP. GFF是模仿QQ通讯项目,通信基于SAEA.MessageSocket、SAEA.Http、SAEA.MVC实现
Version Downloads Last Updated
26.4.23.1 91 4/23/2026
26.4.22.1 96 4/21/2026
7.26.4.21 97 4/20/2026
7.26.4.20 94 4/20/2026
7.26.2.2 128 2/2/2026
7.26.1.27 117 1/27/2026
7.26.1.25 119 1/25/2026
7.25.2.19 265 2/19/2025
7.25.2.18 233 2/18/2025
7.25.2.13 222 2/13/2025
7.25.2.11 214 2/11/2025
7.24.8.4 224 8/4/2024
7.23.9.24 306 9/24/2023
7.0.3.19 419 3/19/2023
7.0.0.3 922 2/27/2022
7.0.0.2 525 12/5/2021
7.0.0.1 524 11/28/2021
6.2.6.7 603 9/19/2021
6.2.6.6 640 8/15/2021
6.2.6.5 537 8/10/2021
Loading failed

This is a message server and client component based on SAEA. Scokets. Messages are transmitted by Pb compression. Messages support private messages, channels, groups, etc.
这是一个基于SAEA.Scokets的高性能消息服务器、客户端组件,消息使用pb压缩传输,消息支持私信、频道、群组等方式
https://github.com/yswenli/SAEA