From ab244c440ecf1c4507a5670d1e2ea86c27668fde Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=8B=A5=E6=B1=9D=E6=A3=8B=E8=8C=97?= <505554090@qq.com>
Date: Fri, 16 May 2025 10:48:34 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=EF=BC=9A=E6=96=B0=E5=A2=9Ewe?=
=?UTF-8?q?bsocket=E8=AF=BE=E7=A8=8B=E9=93=BE=E6=8E=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
handbook/docs/webapi.mdx | 6 ++++++
handbook/docs/websocketclient.mdx | 14 ++++++++++++++
handbook/docs/websocketdescription.mdx | 4 ++++
handbook/docs/websocketheartbeat.mdx | 4 ++++
handbook/docs/websocketservice.mdx | 15 +++++++++++++++
handbook/docs/wscommandlineplugin.mdx | 4 ++++
6 files changed, 47 insertions(+)
diff --git a/handbook/docs/webapi.mdx b/handbook/docs/webapi.mdx
index ed5ecd013..4ab4edd6d 100644
--- a/handbook/docs/webapi.mdx
+++ b/handbook/docs/webapi.mdx
@@ -79,6 +79,9 @@ Console.WriteLine($"使用:http://127.0.0.1:7789/ApiServer/Sum?a=10&b=20");
```
+
+
+
## 五、参数规则
@@ -267,6 +270,9 @@ public int SumCallContext(IWebApiCallContext callContext, int a, int b)
:::
+
+
+
### 7.1 获取当前客户端信息
一般来说,调用上下文的`Caller`就是实际通信的客户端,也就是`HttpSessionClient`。
diff --git a/handbook/docs/websocketclient.mdx b/handbook/docs/websocketclient.mdx
index 91fa32db9..64ef63438 100644
--- a/handbook/docs/websocketclient.mdx
+++ b/handbook/docs/websocketclient.mdx
@@ -5,6 +5,7 @@ title: 创建WebSocket客户端
import Tag from "@site/src/components/Tag.js";
import CardLink from "@site/src/components/CardLink.js";
+import BilibiliCard from '@site/src/components/BilibiliCard.js';
### 定义
@@ -182,6 +183,8 @@ client.Logger.Info("通过ws://127.0.0.1:7789/postws连接成功");
客户端定义了一些发送方法,方便开发者快速发送数据。
+
+
### 5.1 发送文本类消息
```csharp showLineNumbers
@@ -368,11 +371,15 @@ using (var client = GetClient())
:::
+
+
### 6.4 接收中继数据
`WebSocket`协议本身是支持超大数据包的,但是这些包不会一次性接收,而是分多次接收的,同时会通过`Opcode`来表明其为中继数据。
+
+
下面将演示接收文本数据。
【方法1】
@@ -623,6 +630,9 @@ await client.PingAsync();
await client.PongAsync();
```
+
+
+
:::tip 建议
`WebSocket`是双向通讯,所以支持客户端和服务器双向操作`Ping`和`Pong`报文。但是一般来说都是客户端执行`Ping`,服务器回应`Pong`。
@@ -640,6 +650,8 @@ await client.PongAsync();
})
```
+
+
## 八、关闭连接
在使用`WebSocket`时,如果想主动关闭连接,可以使用`CloseAsync`方法,同时可以携带一个关闭原因。
@@ -656,6 +668,8 @@ await webSocket.CloseAsync("关闭");
await webSocket.CloseAsync(WebSocketCloseStatus.EndpointUnavailable,"关闭");//状态码为1001,意为:服务端不可用。
```
+
+
## 九、本文示例Demo
diff --git a/handbook/docs/websocketdescription.mdx b/handbook/docs/websocketdescription.mdx
index 05ac514db..c5a2e72ab 100644
--- a/handbook/docs/websocketdescription.mdx
+++ b/handbook/docs/websocketdescription.mdx
@@ -3,6 +3,8 @@ id: websocketdescription
title: 产品及架构介绍
---
+import BilibiliCard from '@site/src/components/BilibiliCard.js';
+
### 定义
命名空间:TouchSocket.Http.WebSockets
@@ -12,6 +14,8 @@ title: 产品及架构介绍
- WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范。WebSocket API也被W3C定为标准。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。
+
+
## 产品特点
- 简单易用。
diff --git a/handbook/docs/websocketheartbeat.mdx b/handbook/docs/websocketheartbeat.mdx
index 4c8c09885..a3d7f3b9c 100644
--- a/handbook/docs/websocketheartbeat.mdx
+++ b/handbook/docs/websocketheartbeat.mdx
@@ -3,6 +3,8 @@ id: websocketheartbeat
title: Websocket心跳设置
---
+import BilibiliCard from '@site/src/components/BilibiliCard.js';
+
### 定义
命名空间:TouchSocket.Http.WebSockets
@@ -12,6 +14,8 @@ title: Websocket心跳设置
Websocket拥有自己的心跳数据格式,和响应心跳格式,专业名词是Ping和Pong。所以依靠这两个,可以直接实现心跳。
+
+
## 二、心跳配置
#### 2.1 服务器配置
diff --git a/handbook/docs/websocketservice.mdx b/handbook/docs/websocketservice.mdx
index 1495f84ae..bad4fd3b2 100644
--- a/handbook/docs/websocketservice.mdx
+++ b/handbook/docs/websocketservice.mdx
@@ -4,6 +4,7 @@ title: 创建WebSocket服务器
---
import CardLink from "@site/src/components/CardLink.js";
+import BilibiliCard from '@site/src/components/BilibiliCard.js';
### 定义
@@ -58,6 +59,8 @@ await service.StartAsync();
service.Logger.Info("服务器已启动");
```
+
+
### 4.2 验证连接
可以对连接的`Url`、`Query`、`Header`等参数进行验证,然后决定是否执行`WebSocket`连接。
@@ -161,6 +164,8 @@ await service.StartAsync();
service.Logger.Info("服务器已启动");
```
+
+
【接收连接】
```csharp {16} showLineNumbers
@@ -278,6 +283,8 @@ config.SetServiceSslOption(new ServiceSslOption() //Ssl配置,当为null的时
WebSocket服务器接收消息,目前有两种方式。第一种就是通过订阅`IWebSocketReceivedPlugin`插件完全异步的接收消息。第二种就是调用`WebSocket`,然后调用`ReadAsync`方法异步阻塞式读取。
+
+
### 5.1 简单接收消息
【定义插件】
@@ -424,6 +431,8 @@ class MyReadWebSocketPlugin : PluginBase, IWebSocketHandshakedPlugin
}
```
+
+
【使用】
```csharp {16}
@@ -469,6 +478,8 @@ private static HttpService CreateHttpService()
`WebSocket`协议本身是支持超大数据包的,但是这些包不会一次性接收,而是分多次接收的,同时会通过`Opcode`来表明其为中继数据。
+
+
下面将演示接收文本数据。
【方法1】
@@ -865,6 +876,8 @@ await webSocket.PingAsync();
await webSocket.PongAsync();
```
+
+
### 6.8 发送大数据
发送大数据时,需要分包发送,可以使用`SendAsync`的重载方法,设置`FIN`标志。
@@ -900,6 +913,8 @@ await webSocket.CloseAsync("关闭");
await webSocket.CloseAsync(WebSocketCloseStatus.EndpointUnavailable,"关闭");//状态码为1001,意为:服务端不可用。
```
+
+
## 八、本文示例Demo
\ No newline at end of file
diff --git a/handbook/docs/wscommandlineplugin.mdx b/handbook/docs/wscommandlineplugin.mdx
index c43639bb8..bdb9095e7 100644
--- a/handbook/docs/wscommandlineplugin.mdx
+++ b/handbook/docs/wscommandlineplugin.mdx
@@ -3,6 +3,8 @@ id: wscommandlineplugin
title: 快捷事务命令行
---
+import BilibiliCard from '@site/src/components/BilibiliCard.js';
+
### 定义
命名空间:TouchSocket.Http.WebSockets
@@ -12,6 +14,8 @@ title: 快捷事务命令行
快捷事务命令行,是用于**WebSocket**的快捷事务实现,让WS在**Text**文本中,用最简单的文字消息即可完成相关事务的执行。
+
+
## 二、使用
### 2.1 声明事务插件