Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
若汝棋茗
RRQMSocket
提交
0b3831e7
RRQMSocket
项目概览
若汝棋茗
/
RRQMSocket
大约 1 年 前同步成功
通知
20
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
RRQMSocket
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0b3831e7
编写于
8月 03, 2022
作者:
若
若汝棋茗
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
插件新增UseWebSocket的快捷方式。
ReconnectionPlugin插件可以获得重连次数的重载设置。
上级
eb2e1d03
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
123 addition
and
35 deletion
+123
-35
src/TouchSocket/Http/WebSockets/Extensions/WebSocketPluginsManagerExtension.cs
...WebSockets/Extensions/WebSocketPluginsManagerExtension.cs
+24
-0
src/TouchSocket/Http/WebSockets/Plugins/WebSocketServerPlugin.cs
...chSocket/Http/WebSockets/Plugins/WebSocketServerPlugin.cs
+2
-0
src/TouchSocket/Rpc/WebApi/Attribute/RouterAttribute.cs
src/TouchSocket/Rpc/WebApi/Attribute/RouterAttribute.cs
+1
-1
src/TouchSocket/Sockets/Extensions/SocketPluginsManagerExtension.cs
...ocket/Sockets/Extensions/SocketPluginsManagerExtension.cs
+85
-2
src/TouchSocket/Sockets/Plugins/ReconnectionPlugin.cs
src/TouchSocket/Sockets/Plugins/ReconnectionPlugin.cs
+10
-31
src/TouchSocket/TouchSocket.csproj
src/TouchSocket/TouchSocket.csproj
+1
-1
未找到文件。
src/TouchSocket/Http/WebSockets/Extensions/WebSocketPluginsManagerExtension.cs
0 → 100644
浏览文件 @
0b3831e7
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
>();
}
}
}
src/TouchSocket/Http/WebSockets/Plugins/WebSocketServerPlugin.cs
浏览文件 @
0b3831e7
...
...
@@ -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>
...
...
src/TouchSocket/Rpc/WebApi/Attribute/RouterAttribute.cs
浏览文件 @
0b3831e7
...
...
@@ -38,7 +38,7 @@ namespace TouchSocket.Rpc.WebApi
{
if
(!
routeTemple
.
StartsWith
(
"/"
))
{
routeTemple
.
Insert
(
0
,
"/"
);
routeTemple
=
routeTemple
.
Insert
(
0
,
"/"
);
}
this
.
RouteTemple
=
routeTemple
;
}
...
...
src/TouchSocket/Sockets/Extensions/SocketPluginsManagerExtension.cs
浏览文件 @
0b3831e7
...
...
@@ -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
;
}
}
}
src/TouchSocket/Sockets/Plugins/ReconnectionPlugin.cs
浏览文件 @
0b3831e7
...
...
@@ -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
(
t
cpClient
.
Online
)
if
(
t
his
.
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
);
}
}
});
...
...
src/TouchSocket/TouchSocket.csproj
浏览文件 @
0b3831e7
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录