提交 0b3831e7 编写于 作者: 若汝棋茗

插件新增UseWebSocket的快捷方式。

ReconnectionPlugin插件可以获得重连次数的重载设置。
上级 eb2e1d03
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Http.WebSockets;
namespace TouchSocket.Core.Plugins
{
/// <summary>
/// WebSocketPluginsManagerExtension
/// </summary>
public static class WebSocketPluginsManagerExtension
{
/// <summary>
/// 使用WebSocket插件
/// </summary>
/// <returns>插件类型实例</returns>
public static WebSocketServerPlugin UseWebSocket(this IPluginsManager pluginsManager)
{
return pluginsManager.Add<WebSocketServerPlugin>();
}
}
}
......@@ -12,6 +12,7 @@
//------------------------------------------------------------------------------
using System;
using TouchSocket.Core.Dependency;
using TouchSocket.Core.Plugins;
using TouchSocket.Http.Plugins;
using TouchSocket.Sockets;
......@@ -21,6 +22,7 @@ namespace TouchSocket.Http.WebSockets
/// 基于Http的WebSocket的扩展。
/// <para>此组件只能挂载在<see cref="HttpService"/>中</para>
/// </summary>
[SingletonPlugin]
public class WebSocketServerPlugin : HttpPluginBase
{
/// <summary>
......
......@@ -38,7 +38,7 @@ namespace TouchSocket.Rpc.WebApi
{
if (!routeTemple.StartsWith("/"))
{
routeTemple.Insert(0, "/");
routeTemple = routeTemple.Insert(0, "/");
}
this.RouteTemple = routeTemple;
}
......
......@@ -11,6 +11,9 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Threading;
using TouchSocket.Core.Dependency;
using TouchSocket.Core.Log;
using TouchSocket.Sockets;
using TouchSocket.Sockets.Plugins;
......@@ -31,10 +34,90 @@ namespace TouchSocket.Core.Plugins
/// <param name="printLog">是否输出日志。</param>
/// <param name="sleepTime">失败时,停留时间</param>
/// <returns></returns>
public static IPluginsManager UseReconnection(this IPluginsManager pluginsManager, int tryCount = 10, bool printLog = false, int sleepTime = 1000, Action<ITcpClient> successCallback = null)
public static IPluginsManager UseReconnection(this IPluginsManager pluginsManager, int tryCount = 10,
bool printLog = false, int sleepTime = 1000, Action<ITcpClient> successCallback = null)
{
pluginsManager.Add(new ReconnectionPlugin<ITcpClient>(tryCount, printLog, sleepTime, successCallback));
var reconnectionPlugin = new ReconnectionPlugin<ITcpClient>(client=>
{
int tryT = tryCount;
while (tryCount < 0 || tryT-- > 0)
{
try
{
if (client.Online)
{
return true;
}
else
{
client.Connect();
}
successCallback?.Invoke(client);
return true;
}
catch (Exception ex)
{
if (printLog)
{
client.Logger.Debug(LogType.Error, client, "断线重连失败。", ex);
}
Thread.Sleep(sleepTime);
}
}
return true;
});
pluginsManager.Add(reconnectionPlugin);
return pluginsManager;
}
/// <summary>
/// 使用断线重连。
/// <para>该效果仅客户端在完成首次连接,且为被动断开时有效。</para>
/// </summary>
/// <param name="pluginsManager"></param>
/// <param name="sleepTime">失败时间隔时间</param>
/// <param name="failCallback">失败时回调(参数依次为:客户端,本轮尝试重连次数,异常信息)。如果回调为null或者返回false,则终止尝试下次连接。</param>
/// <param name="successCallback">成功连接时回调。</param>
/// <returns></returns>
public static IPluginsManager UseReconnection(this IPluginsManager pluginsManager, int sleepTime = 1000,
Func<ITcpClient,int,Exception,bool> failCallback=null,
Action<ITcpClient> successCallback = null)
{
var reconnectionPlugin = new ReconnectionPlugin<ITcpClient>(client =>
{
int tryT = 0;
while (true)
{
try
{
if (client.Online)
{
return true;
}
else
{
client.Connect();
}
successCallback?.Invoke(client);
return true;
}
catch (Exception ex)
{
Thread.Sleep(sleepTime);
if (failCallback?.Invoke(client, ++tryT, ex)!=true)
{
return true;
}
}
}
});
pluginsManager.Add(reconnectionPlugin);
return pluginsManager;
}
}
}
......@@ -14,6 +14,7 @@ using System;
using System.Threading;
using System.Threading.Tasks;
using TouchSocket.Core.Dependency;
using TouchSocket.Core.Log;
using TouchSocket.Core.Plugins;
namespace TouchSocket.Sockets.Plugins
......@@ -24,26 +25,16 @@ namespace TouchSocket.Sockets.Plugins
[SingletonPlugin]
public sealed class ReconnectionPlugin<TClient> : TcpPluginBase where TClient : class, ITcpClient
{
private readonly bool m_printLog;
private readonly int m_sleepTime;
private readonly Action<TClient> m_successCallback;
private readonly int m_tryCount;
private readonly Func<TClient, bool> m_tryCon;
/// <summary>
/// 初始化一个重连插件
/// </summary>
/// <param name="tryCount"></param>
/// <param name="successCallback"></param>
/// <param name="printLog"></param>
/// <param name="sleepTime"></param>
[DependencyInject(10, true, 1000, null)]
public ReconnectionPlugin(int tryCount, bool printLog, int sleepTime, Action<TClient> successCallback)
/// <param name="tryCon">无论如何,只要返回True,则结束本轮尝试</param>
public ReconnectionPlugin(Func<TClient,bool> tryCon)
{
this.m_tryCount = tryCount;
this.m_successCallback = successCallback;
this.m_printLog = printLog;
this.m_sleepTime = sleepTime;
this.Order = int.MinValue;
this.m_tryCon = tryCon;
}
/// <summary>
......@@ -63,30 +54,18 @@ namespace TouchSocket.Sockets.Plugins
}
Task.Run(() =>
{
int tryT = this.m_tryCount;
while (this.m_tryCount < 0 || tryT-- > 0)
while (true)
{
try
{
if (tcpClient.Online)
if (this.m_tryCon.Invoke((TClient)tcpClient))
{
return;
break;
}
else
{
tcpClient.Connect();
}
this.m_successCallback?.Invoke((TClient)tcpClient);
break;
}
catch (Exception ex)
catch
{
if (this.m_printLog)
{
client.Logger.Debug(TouchSocket.Core.Log.LogType.Error, client, "断线重连失败。", ex);
}
Thread.Sleep(this.m_sleepTime);
}
}
});
......
......@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>RRQM.pfx</AssemblyOriginatorKeyFile>
<Version>0.3.1</Version>
<Version>0.3.2</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2022 若汝棋茗</Copyright>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册