提交 288a9915 编写于 作者: 若汝棋茗

修复更新websocket相关

上级 305d1e5b
......@@ -57,7 +57,7 @@ namespace TouchSocket.Http.WebSockets
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public static bool Ping(this HttpClientBase client)
public static bool PingWS(this HttpClientBase client)
{
try
{
......@@ -75,7 +75,7 @@ namespace TouchSocket.Http.WebSockets
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public static bool Pong(this HttpClientBase client)
public static bool PongWS(this HttpClientBase client)
{
try
{
......@@ -434,11 +434,11 @@ namespace TouchSocket.Http.WebSockets
#region 服务器
/// <summary>
/// 发送Ping报文。
/// 发送WebSocket协议的Ping报文。
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public static bool Ping(this HttpSocketClient client)
public static bool PingWS(this HttpSocketClient client)
{
try
{
......@@ -452,11 +452,11 @@ namespace TouchSocket.Http.WebSockets
}
/// <summary>
/// 发送Pong报文。
/// 发送WebSocket协议的Pong报文。
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public static bool Pong(this HttpSocketClient client)
public static bool PongWS(this HttpSocketClient client)
{
try
{
......@@ -470,7 +470,7 @@ namespace TouchSocket.Http.WebSockets
}
/// <summary>
/// 发送Close报文。
/// 发送WebSocket协议的Close报文。
/// </summary>
/// <param name="client"></param>
/// <param name="msg"></param>
......
......@@ -27,5 +27,14 @@ namespace TouchSocket.Core
{
return pluginsManager.Add<WebSocketServerPlugin>();
}
/// <summary>
/// 使用WebSocket心跳插件(仅客户端生效)
/// </summary>
/// <returns>插件类型实例</returns>
public static WebSocketHeartbeatPlugin UseHeartbeat(this IPluginsManager pluginsManager)
{
return pluginsManager.Add<WebSocketHeartbeatPlugin>();
}
}
}
\ No newline at end of file
......@@ -41,12 +41,10 @@ namespace TouchSocket.Http.WebSockets
IsPermitOperation = true
};
client.PluginsManager?.Raise<IWebSocketPlugin>(nameof(IWebSocketPlugin.OnHandshaking), client, args);
if (args.Context.Response.Responsed)
{
return false;
}
if (args.IsPermitOperation)
{
client.SetDataHandlingAdapter(new WebSocketDataHandlingAdapter());
......
......@@ -10,6 +10,7 @@
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Threading;
using TouchSocket.Core;
using TouchSocket.Sockets;
......@@ -22,16 +23,23 @@ namespace TouchSocket.Http.WebSockets
[SingletonPlugin]
public class WebSocketHeartbeatPlugin : WebSocketPluginBase
{
private readonly int m_timeTick;
private TimeSpan m_timeTick=TimeSpan.FromSeconds(5);
/// <summary>
/// 初始化一个适用于WebSocket的心跳插件
/// </summary>
/// <param name="interval"></param>
[DependencyInject(1000 * 5)]
public WebSocketHeartbeatPlugin(int interval)
public WebSocketHeartbeatPlugin()
{
m_timeTick = interval;
}
/// <summary>
/// 设置心跳间隔,默认5秒。
/// </summary>
/// <param name="timeSpan"></param>
public void Tick(TimeSpan timeSpan)
{
this.m_timeTick = timeSpan;
}
/// <summary>
......@@ -49,8 +57,8 @@ namespace TouchSocket.Http.WebSockets
}
client.SetValue(WebSocketExtensions.HeartbeatTimerProperty, new Timer((o) =>
{
httpClientBase.Ping();
}, null, 0, m_timeTick));
httpClientBase.PingWS();
}, null, m_timeTick, m_timeTick));
}
base.OnHandshaked(client, e);
}
......@@ -63,9 +71,9 @@ namespace TouchSocket.Http.WebSockets
protected override void OnDisconnected(ITcpClientBase client, DisconnectEventArgs e)
{
base.OnDisconnected(client, e);
if (client.GetValue<Timer>(WebSocketExtensions.HeartbeatTimerProperty) is Timer timer)
if (client.GetValue(WebSocketExtensions.HeartbeatTimerProperty) is Timer timer)
{
timer.Dispose();
timer.SafeDispose();
client.SetValue(WebSocketExtensions.HeartbeatTimerProperty, null);
}
}
......
......@@ -53,6 +53,11 @@ namespace TouchSocket.Http.WebSockets
/// </summary>
public bool AutoClose { get; set; } = true;
/// <summary>
/// 当收到ping报文时,是否自动回应pong。
/// </summary>
public bool AutoPong { get; set; } = true;
/// <summary>
/// 处理WS数据的回调
/// </summary>
......@@ -78,6 +83,16 @@ namespace TouchSocket.Http.WebSockets
return this;
}
/// <summary>
/// 当收到ping报文时,不自动回应pong。
/// </summary>
/// <returns></returns>
public WebSocketServerPlugin NoAutoPong()
{
AutoPong = false;
return this;
}
/// <summary>
/// 设置处理WS数据的回调。
/// </summary>
......@@ -128,14 +143,18 @@ namespace TouchSocket.Http.WebSockets
/// <param name="e"></param>
protected virtual void OnHandleWSDataFrame(ITcpClientBase client, WSDataFrameEventArgs e)
{
if (e.DataFrame.Opcode == WSDataType.Close && AutoClose)
if (AutoClose&&e.DataFrame.Opcode == WSDataType.Close)
{
string msg = e.DataFrame.PayloadData?.ToString();
m_pluginsManager.Raise<IWebSocketPlugin>(nameof(IWebSocketPlugin.OnClosing), client, new MsgEventArgs() { Message = msg });
client.Close(msg);
return;
}
if (AutoPong&& e.DataFrame.Opcode == WSDataType.Ping)
{
((HttpSocketClient)client).PongWS();
return;
}
if (m_pluginsManager.Raise<IWebSocketPlugin>(nameof(IWebSocketPlugin.OnHandleWSDataFrame), client, e))
{
return;
......
......@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>D:\MyStore\13_Doc\Keys\TouchSocket.snk</AssemblyOriginatorKeyFile>
<Version>1.2.14</Version>
<Version>1.2.15</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2023 若汝棋茗</Copyright>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册