新增:文档链接样式

This commit is contained in:
若汝棋茗
2024-10-19 15:58:17 +08:00
parent 01b09fd43b
commit 15246f51a5
7 changed files with 285 additions and 8 deletions

23
handbook/blog/authors.yml Normal file
View File

@@ -0,0 +1,23 @@
rrqm:
name: 若汝棋茗
title: Software Engineer Ⅱ
url: https://github.com/rrqm
image_url: https://github.com/rrqm.png
page: true
socials:
github: rrqm
newsletter: QQ:505554090
# slorber:
# name: Sébastien Lorber
# title: Docusaurus maintainer
# url: https://sebastienlorber.com
# image_url: https://github.com/slorber.png
# page:
# # customize the url of the author page at /blog/authors/<permalink>
# permalink: '/all-sebastien-lorber-articles'
# socials:
# x: sebastienlorber
# linkedin: sebastienlorber
# github: slorber
# newsletter: https://thisweekinreact.com

9
handbook/blog/tags.yml Normal file
View File

@@ -0,0 +1,9 @@
TcpClient:
label: TcpClient
permalink: /TcpClient
description: TcpClient
TcpService:
label: TcpService
permalink: /TcpService
description: TcpService

View File

@@ -0,0 +1,135 @@
---
slug: tcplimiting
title: Tcp实现接收数据限流
authors: [rrqm]
tags: [TcpClient, TcpService]
---
import CardLink from '@site/src/components/CardLink.js';
## 一、引言
使用[TouchSocket](https://gitee.com/dotnetchina/TouchSocket)创建服务器后,想实现一个限流功能,应该如何实现呢?
## 二、技术细节
## 三、实践应用
## 四、代码实现
## 五、性能分析
## 六、总结
## 七、参考资料
## 八、本文示例Demo
<CardLink link="https://gitee.com/RRQM_Home/TouchSocket/tree/master/examples/BlogsDemos/ThrottlingConsoleApp" isPro="true"/>
***
## 二、程序集源码
#### 2.1 源码位置
- [Gitee](https://gitee.com/RRQM_Home)
- [Github](https://github.com/RRQM)
#### 2.2 说明文档
[文档首页](https://touchsocket.net/)
## 三、安装
Nuget安装`TouchSocket`即可,具体步骤详看链接博客。
[VS、Unity安装和使用Nuget包](https://blog.csdn.net/qq_40374647/article/details/121464929)
## 四、创建插件
```csharp
public class MyThrottlingPlugin : PluginBase, ITcpConnectedPlugin, ITcpReceivingPlugin
{
private readonly int m_max;
[DependencyInject(10)]
public MyThrottlingPlugin(int max)
{
this.m_max = max;
this.Order = int.MaxValue;//提升优先级
}
Task ITcpConnectedPlugin<ITcpClientBase>.OnTcpConnected(ITcpClientBase client, ConnectedEventArgs e)
{
client.InitFlowGate(this.m_max);//初始化流量计数器。
return e.InvokeNext();
}
async Task ITcpReceivingPlugin<ITcpClientBase>.OnTcpReceiving(ITcpClientBase client, ByteBlockEventArgs e)
{
await client.GetFlowGate().AddCheckWaitAsync(e.ByteBlock.Len);
await e.InvokeNext();
}
}
```
## 五、创建扩展类
```csharp
/// <summary>
/// 一个流量计数器扩展。
/// </summary>
internal static class DependencyExtensions
{
public static readonly DependencyProperty<FlowGate> FlowGateProperty =
DependencyProperty<FlowGate>.Register("FlowGate", null);
public static void InitFlowGate(this IDependencyObject dependencyObject, int max)
{
dependencyObject.SetValue(FlowGateProperty, new FlowGate() { Maximum = max });
}
public static FlowGate GetFlowGate(this IDependencyObject dependencyObject)
{
return dependencyObject.GetValue(FlowGateProperty);
}
}
```
## 六、启动服务器
```csharp
/// <summary>
/// 限制单个客户端的访问流量
/// 博客连接<see href="https://blog.csdn.net/qq_40374647/article/details/125496769"/>
/// </summary>
/// <param name="args"></param>
private static void Main(string[] args)
{
var service = new TcpService();
service.Received = (client, byteBlock, requestInfo) =>
{
//从客户端收到信息
var mes = Encoding.UTF8.GetString(byteBlock.Buffer, 0, byteBlock.Len);
client.Logger.Info($"已从{client.Id}接收到信息:{mes}");
};
service.Setup(new TouchSocketConfig()//载入配置
.SetListenIPHosts(new IPHost[] { new IPHost("127.0.0.1:7789"), new IPHost(7790) })//同时监听两个地址
.ConfigureContainer(a =>
{
a.AddConsoleLogger();
})
.ConfigurePlugins(a =>
{
a.Add<MyThrottlingPlugin>();
}))
.Start();//启动
service.Logger.Info("服务器已启动");
Console.ReadLine();
}
```
## 七、效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c8f957d58769b7cc5770f59d0494f558.gif)
本文[示例demo](https://gitee.com/RRQM_Home/TouchSocket/tree/master/examples/BlogsDemos/ThrottlingConsoleApp)
实际上,该插件也能用于**客户端**。同时,也能限制**发送**流量限制。

View File

@@ -55,13 +55,20 @@ const config = {
// }
},
},
// blog: {
// showReadingTime: true,
// // Please change this to your repo.
// // Remove this to remove the "edit this page" links.
// editUrl:
// 'https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/',
// },
blog: {
showReadingTime: true,
feedOptions: {
type: ['rss', 'atom'],
xslt: true,
},
onInlineTags: 'warn',
onInlineAuthors: 'warn',
onUntruncatedBlogPosts: 'warn',
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
editUrl:
'https://gitee.com/rrqm_home/touchsocket/tree/master/handbook/',
},
theme: {
customCss: './src/css/custom.css',
},
@@ -107,7 +114,7 @@ const config = {
{
label: "博客",
position: "left",
to: "docs/current/blog"
to: "/blog"
},
{
label: "视频",

View File

@@ -0,0 +1,36 @@
// src/CardLink.js
import React from 'react';
import '../css/CardLink.css';
import CodeDemo from '../pages/codedemo.svg';
// 辅助函数从URL中提取最后一个路径段
function getLastPathSegment(url) {
const urlParts = new URL(url).pathname.split('/').filter(Boolean);
if (urlParts.length > 0) {
return urlParts[urlParts.length - 1];
} else {
return url; // 如果没有路径段返回整个URL
}
}
const CardLink = ({ title, link, isPro }) => {
const resolvedTitle = title || getLastPathSegment(link);
return (
<div className="card-link">
<a href={link}>
<div className="row card-content">
<CodeDemo height="30" width="30" />
<h3>{resolvedTitle}</h3>
</div>
{isPro && (
<div className="pro-badge">
<a href="/docs/current/enterprise">Pro</a>
</div>
)}
</a>
</div>
);
};
export default CardLink;

View File

@@ -0,0 +1,66 @@
/* CardLink.css */
.card-link {
display: block;
width: 100%;
/* 设置宽度为100%,占据父容器全宽 */
position: relative;
text-align: left;
/* 左对齐文本 */
padding: 20px;
margin: 10px;
border: 1px solid #ccc;
border-radius: 5px;
text-decoration: none;
color: #333;
transition: background-color 0.3s ease;
}
.card-link:hover {
background-color: #f0f0f0;
}
.card-content {
display: flex;
/* 使用Flex布局 */
align-items: center;
/* 垂直居中对齐 */
}
.card-content img {
margin-right: 10px;
/* 图片与文本之间的间距 */
}
.card-content h3 {
margin: 5px;
margin-left: 10px;
/* 设置左边距 */
/* 标题与描述之间的间距 */
flex: 1;
/* 让标题占据剩余空间 */
}
.card-content p {
flex: 1;
/* 让描述占据剩余空间 */
}
.pro-badge {
position: absolute;
top: 5px;
right: 5px;
z-index: 1;
}
.pro-badge a {
padding: 5px 10px;
border: none;
border-radius: 3px;
background-color: #007bff;
color: white;
cursor: pointer;
}
.pro-badge a:hover {
background-color: #0056b3;
}

View File

@@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1729315128202" class="icon" viewBox="0 0 1031 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6120" xmlns:xlink="http://www.w3.org/1999/xlink" width="201.3671875" height="200"><path d="M946.9 765.9H84.7C38.1 765.9 0 727.8 0 681.2V127.4c0-46.6 38.1-84.7 84.7-84.7h862.2c46.6 0 84.7 38.1 84.7 84.7v553.8c0 46.6-38.1 84.7-84.7 84.7z" fill="#E3E3E3" p-id="6121"></path><path d="M946.9 991.5H84.7C38.1 991.5 0 953.4 0 906.8V268.4h1031.5v638.5c0.1 46.5-38 84.6-84.6 84.6z" fill="#475762" p-id="6122"></path><path d="M163.304 234.063a22.17 22.17 0 1 0 44.34 0 22.17 22.17 0 1 0-44.34 0zM226.647 234.063a22.17 22.17 0 1 0 44.34 0 22.17 22.17 0 1 0-44.34 0zM289.99 234.063a22.17 22.17 0 1 0 44.34 0 22.17 22.17 0 1 0-44.34 0zM233.754 409.879H200.46c-10.042 0-18.23-8.189-18.23-18.23 0-10.043 8.188-18.231 18.23-18.231h33.294c10.042 0 18.23 8.188 18.23 18.23s-8.188 18.23-18.23 18.23z" fill="#CACACA" p-id="6123"></path><path d="M309.7 379.8H239c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h70.7c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m22.6 89.2H298c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h34.3c13 0 23.6 10.6 23.6 23.6 0 13-10.7 23.6-23.6 23.6z m52.3 89.2h-32.8c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h32.8c13 0 23.6 10.6 23.6 23.6 0 13-10.7 23.6-23.6 23.6z" fill="#F26397" p-id="6124"></path><path d="M597.359 547.688H480.097c-10.042 0-18.23-8.188-18.23-18.23s8.188-18.23 18.23-18.23h117.262c10.042 0 18.23 8.188 18.23 18.23-0.077 10.042-8.265 18.23-18.23 18.23z" fill="#6ACEB1" p-id="6125"></path><path d="M785.2 558.2h-63.6c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h63.6c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m139.5 82H777c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h147.7c13 0 23.6 10.6 23.6 23.6 0 12.9-10.7 23.6-23.6 23.6zM775 736.6h-70.7c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6H775c13 0 23.6 10.6 23.6 23.6 0 13-10.7 23.6-23.6 23.6z m-81.8 89.2h-70.7c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h70.7c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m231.5-267.6h-63.6c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h63.6c13 0 23.6 10.6 23.6 23.6 0 13-10.7 23.6-23.6 23.6zM917 736.6h-20.5c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6H917c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z" fill="#5F7786" p-id="6126"></path><path d="M575.3 469H423.5c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h151.8c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m47.2 171.2H470.7c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h151.8c13 0 23.6 10.6 23.6 23.6-0.1 12.9-10.7 23.6-23.6 23.6z m0 96.4H470.7c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h151.8c13 0 23.6 10.6 23.6 23.6-0.1 13-10.7 23.6-23.6 23.6z m-114.9 89.2h-36.9c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h36.9c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z" fill="#6ACEB1" p-id="6127"></path><path d="M418.685 616.593h-26.496c-10.042 0-18.23-8.188-18.23-18.23s8.188-18.23 18.23-18.23h26.496c10.042 0 18.23 8.188 18.23 18.23s-8.188 18.23-18.23 18.23z m0 68.905h-26.496c-10.042 0-18.23-8.188-18.23-18.23s8.188-18.23 18.23-18.23h26.496c10.042 0 18.23 8.188 18.23 18.23s-8.188 18.23-18.23 18.23z m-39.937 68.905H346.69c-10.042 0-18.23-8.188-18.23-18.23 0-10.043 8.188-18.23 18.23-18.23h32.058c10.042 0 18.23 8.187 18.23 18.23-0.077 10.042-8.265 18.23-18.23 18.23z m-26.96 64.965h-54.614c-10.042 0-18.23-8.188-18.23-18.23s8.188-18.23 18.23-18.23h54.614c10.043 0 18.23 8.188 18.23 18.23s-8.187 18.23-18.23 18.23z" fill="#F26397" p-id="6128"></path><path d="M151.8 469h-43.1c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h43.1c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m0 89.2h-43.1c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h43.1c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m0 89.2h-43.1c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h43.1c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m0 89.2h-43.1c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h43.1c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m0 89.2h-43.1c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h43.1c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z m0 89.2h-43.1c-13 0-23.6-10.6-23.6-23.6 0-13 10.6-23.6 23.6-23.6h43.1c13 0 23.6 10.6 23.6 23.6 0 13-10.6 23.6-23.6 23.6z" fill="#CACACA" p-id="6129"></path></svg>

After

Width:  |  Height:  |  Size: 4.3 KiB