提交 f879380a 编写于 作者: D dwx666

优化TCP重连的策略

上级 a4b57848
...@@ -37,6 +37,7 @@ namespace TouchSocket.Core.Plugins ...@@ -37,6 +37,7 @@ namespace TouchSocket.Core.Plugins
public static IPluginsManager UseReconnection(this IPluginsManager pluginsManager, int tryCount = 10, public static IPluginsManager UseReconnection(this IPluginsManager pluginsManager, int tryCount = 10,
bool printLog = false, int sleepTime = 1000, Action<ITcpClient> successCallback = null) bool printLog = false, int sleepTime = 1000, Action<ITcpClient> successCallback = null)
{ {
bool first = true;
var reconnectionPlugin = new ReconnectionPlugin<ITcpClient>(client=> var reconnectionPlugin = new ReconnectionPlugin<ITcpClient>(client=>
{ {
int tryT = tryCount; int tryT = tryCount;
...@@ -50,9 +51,11 @@ namespace TouchSocket.Core.Plugins ...@@ -50,9 +51,11 @@ namespace TouchSocket.Core.Plugins
} }
else else
{ {
if (first) Thread.Sleep(1000);
first = false;
client.Connect(); client.Connect();
first = true;
} }
successCallback?.Invoke(client); successCallback?.Invoke(client);
return true; return true;
} }
...@@ -85,6 +88,7 @@ namespace TouchSocket.Core.Plugins ...@@ -85,6 +88,7 @@ namespace TouchSocket.Core.Plugins
Func<ITcpClient,int,Exception,bool> failCallback=null, Func<ITcpClient,int,Exception,bool> failCallback=null,
Action<ITcpClient> successCallback = null) Action<ITcpClient> successCallback = null)
{ {
bool first = true;
var reconnectionPlugin = new ReconnectionPlugin<ITcpClient>(client => var reconnectionPlugin = new ReconnectionPlugin<ITcpClient>(client =>
{ {
int tryT = 0; int tryT = 0;
...@@ -98,7 +102,10 @@ namespace TouchSocket.Core.Plugins ...@@ -98,7 +102,10 @@ namespace TouchSocket.Core.Plugins
} }
else else
{ {
if (first) Thread.Sleep(1000);
first = false;
client.Connect(); client.Connect();
first = true;
} }
successCallback?.Invoke(client); successCallback?.Invoke(client);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册