.net core signalR 服务端断开连接

{ }
{ }
*:first-child { }
*:last-child { }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
h6:first-child { }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
:first-child { }
:last-child { }
{ }
:first-child { }
:last-child { }
{ }
{ }
code { }
{ }
{ }
{ }
{ }
:first-child { }
:last-child { }
{ }
{ }
{ }
{ }
{ }
{ }

{ color: rgba(255, 255, 255, 1); padding: 5px; background-color: rgba(43, 102, 149, 1); border: 1px solid rgba(255, 255, 255, 1); border-radius: 5px; font-size: 24px; margin-top: 15px; margin-bottom: 15px }

environment

.net core 3.1

前言

项目要求弄一个即时通讯

由于.net 已经集成了websocket通讯中间件-signalR,并且运作的效率还可以,为减少开发周期便使用了signalR作为websocket连接管理中间件。

既然是通讯,那就要解决最基本的连接问题。

如何连?以什么作为凭证?

既然是用户与用户之间的通信,那边应该用用户标识作为凭证进行连接,无标识的连接(游客)将毫无意义。

然后一般情况下(参考qq/微信),通讯时是不允许有一个用户多个通讯连接的。既然如此,那便要考虑一个用户二次连接的问题:

在这里,我们选择了第二次登录,将之前登录的用户强制退出。

退出的方式有两种:

  • 客户端自己断开
  • 服务端强制客户断开

随意一点的(自己玩的那种)就客户端自己断开就足够了,但如果是正式的项目的话还是要强制断开,保证操作的完整性。

好,既然要强制断开,那便是服务端移除连接.

先说结果:

一、在会话中断开

会话中是指服务端已获取到连接

使用场景:指客户发送一个[关闭指令],然后服务端自动关闭连接

Microsoft.AspNetCore.SignalR.Hub中有一个public HubCallerContext Context { get; set; }表示调用方的上下文。

然后在Microsoft.AspNetCore.SignalR.HubCallerContext中提供了一个方法:

//
// Summary:
// Aborts the connection.
public abstract void Abort();// --> 使中止,中断连接

故只需要在对应的方法块中使用Context.Abort();便可断开连接

二、在会话外断开

会话外是指服务端还未获取到连接

使用场景:用户在小米登录了账号然后又在华为登录了账号,此时小米的账号应该被强制下线。

根据场景触发事件是华为登录了账号,此时你不清楚小米的连接

于是我们要使用一个集合保留设备-》连接的映射关系:

ConcurrentDictionary<string, HubCallerContext> _connections // 此处key为连接凭证,value为此连接对应的上下文

注:HubCallerContext是一个单例对象,即一个连接中的所有HubCallerContext都是一样的,故此方法可行

然后在连接开启时即Hub.OnConnectedAsync时维护此集合,若是存在一个用户对应多个连接,你还需要维护一个用户->连接凭证的集合

然后在华为连接触发OnConnectedAsync时,检查此集合是否已存在此凭证,若存在则取出对应上下文-HubCallerContext调用Abort进行强制退出

三、源码分析

ps:若是你只是想知道服务端怎么强制断开连接的话,下面就不用看了

由于百度、Google都没搜到需要的结果,只好自己来了...

强制断开即是服务端移除连接

首先,想要释放便得知道连接保存在哪

自己写过websocket的应该都知道,当连接建立后,服务端需要将连接进行保存避免自动释放,那么signalR既然是封装了websocket,那么必然也存在类似的操作

贴一下signalR service注册部分: Microsoft.Extensions.DependencyInjection.SignalRDependencyInjectionExtensions

services.TryAddSingleton<SignalRMarkerService>();
services.TryAddSingleton<SignalRCoreMarkerService>();

services.TryAddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));

services.TryAddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));

services.TryAddSingleton(typeof(IHubContext<>), typeof(HubContext<>));

services.TryAddSingleton(typeof(IHubContext<, >), typeof(HubContext<, >));

services.TryAddSingleton(typeof(HubConnectionHandler<>), typeof(HubConnectionHandler<>));

services.TryAddSingleton(typeof(IUserIdProvider), typeof(DefaultUserIdProvider));

services.TryAddSingleton(typeof(HubDispatcher<>), typeof(DefaultHubDispatcher<>));

services.TryAddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>));

services.AddAuthorization();

SignalRServerBuilder signalRServerBuilder = new SignalRServerBuilder(services);

signalRServerBuilder.AddJsonProtocol();

先看app使用hub的地方:

app.UseEndpoints(endpoints =>
{
endpoints.MapHub<XxxHub>("/xxxHub");
});

navigation->Microsoft.AspNetCore.Builder.HubEndpointRouteBuilderExtensions.HubEndpointRouteBuilderExtensions

public static class HubEndpointRouteBuilderExtensions
{
public static HubEndpointConventionBuilder MapHub<THub>(this IEndpointRouteBuilder endpoints, string pattern) where THub : Hub
{
return endpoints.MapHub<THub>(pattern, null);
}
public static HubEndpointConventionBuilder MapHub&lt;THub&gt;(this IEndpointRouteBuilder endpoints, string pattern, Action&lt;HttpConnectionDispatcherOptions&gt; configureOptions) where THub : Hub
{
if (endpoints.ServiceProvider.GetService&lt;SignalRMarkerService&gt;() == null)
{
throw new InvalidOperationException("Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddSignalR' inside the call to 'ConfigureServices(...)' in the application startup code.");
}
HttpConnectionDispatcherOptions httpConnectionDispatcherOptions = new HttpConnectionDispatcherOptions();
configureOptions?.Invoke(httpConnectionDispatcherOptions);
ConnectionEndpointRouteBuilder connectionEndpointRouteBuilder = endpoints.MapConnections(pattern, httpConnectionDispatcherOptions, delegate(IConnectionBuilder b)
{
b.UseHub&lt;THub&gt;();
});
object[] attributes = typeof(THub).GetCustomAttributes(inherit: true);
connectionEndpointRouteBuilder.Add(delegate(EndpointBuilder e)
{
object[] array = attributes;
foreach (object item in array)
{
e.Metadata.Add(item);
}
e.Metadata.Add(new HubMetadata(typeof(THub)));
});
return new HubEndpointConventionBuilder(connectionEndpointRouteBuilder);
}

}

key code : b.UseHub<THub>();

navigation -> Microsoft.AspNetCore.SignalR.SignalRConnectionBuilderExtensions.SignalRConnectionBuilderExtensions

public static class SignalRConnectionBuilderExtensions
{
public static IConnectionBuilder UseHub<THub>(this IConnectionBuilder connectionBuilder) where THub : Hub
{
if (connectionBuilder.ApplicationServices.GetService(typeof(SignalRCoreMarkerService)) == null)
{
throw new InvalidOperationException("Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddSignalR' inside the call to 'ConfigureServices(...)' in the application startup code.");
}
return connectionBuilder.UseConnectionHandler<HubConnectionHandler<THub>>();
}
}

navigation -> Microsoft.AspNetCore.Connections.ConnectionBuilderExtensions.ConnectionBuilderExtensions

public static IConnectionBuilder UseConnectionHandler<TConnectionHandler>(this IConnectionBuilder connectionBuilder) where TConnectionHandler : ConnectionHandler
{
TConnectionHandler handler = ActivatorUtilities.GetServiceOrCreateInstance<TConnectionHandler>(connectionBuilder.ApplicationServices);
return connectionBuilder.Run((ConnectionContext connection) => handler.OnConnectedAsync(connection));
}

OnConnectedAsync!!!,见名思以当连接打开时触发,这个应该就是关键点了

navigation -> Microsoft.AspNetCore.Connections.ConnectionHandler

public override async Task OnConnectedAsync(ConnectionContext connection)
{
IList<string> list = _hubOptions.SupportedProtocols ?? _globalHubOptions.SupportedProtocols;
if (list == null || list.Count == 0)// 未配置连接协议
{
throw new InvalidOperationException("There are no supported protocols");
}
// 超时时间
TimeSpan timeout = _hubOptions.HandshakeTimeout ?? _globalHubOptions.HandshakeTimeout ?? HubOptionsSetup.DefaultHandshakeTimeout;
// 连接上下文配置
HubConnectionContextOptions contextOptions = new HubConnectionContextOptions
{
KeepAliveInterval = (_hubOptions.KeepAliveInterval ?? _globalHubOptions.KeepAliveInterval ?? HubOptionsSetup.DefaultKeepAliveInterval),
ClientTimeoutInterval = (_hubOptions.ClientTimeoutInterval ?? _globalHubOptions.ClientTimeoutInterval ?? HubOptionsSetup.DefaultClientTimeoutInterval),
StreamBufferCapacity = (_hubOptions.StreamBufferCapacity ?? _globalHubOptions.StreamBufferCapacity ?? 10),
MaximumReceiveMessageSize = _maximumMessageSize
};
Log.ConnectedStarting(_logger);
// **** 构建连接对象
HubConnectionContext connectionContext = new HubConnectionContext(connection, contextOptions, _loggerFactory);
IReadOnlyList&lt;string&gt; supportedProtocols = (list as IReadOnlyList&lt;string&gt;) ?? list.ToList(); // 然后进行握手连接
if (await connectionContext.HandshakeAsync(timeout, supportedProtocols, _protocolResolver, _userIdProvider, _enableDetailedErrors))
{ // 握手成功
try
{
await _lifetimeManager.OnConnectedAsync(connectionContext);
await RunHubAsync(connectionContext);
}
finally
{
Log.ConnectedEnding(_logger);
await _lifetimeManager.OnDisconnectedAsync(connectionContext);
}
}

}

主要看握手成功之后的内容:

try
{
await _lifetimeManager.OnConnectedAsync(connectionContext);
await RunHubAsync(connectionContext);
}
finally
{
Log.ConnectedEnding(_logger);
await _lifetimeManager.OnDisconnectedAsync(connectionContext);
}

首先可以看到在finally中调用了OnDisconnectedAsync,见名思以我觉得它应该就是我们要找的释放连接,查看定义:

private readonly HubLifetimeManager<THub> _lifetimeManager;

而且通过之前的注册来看此成员是一个单例,感觉非常符合,继续查看定义: Microsoft.AspNetCore.SignalR.HubLifetimeManager

public abstract class HubLifetimeManager<THub> where THub : Hub
{
public abstract Task OnConnectedAsync(HubConnectionContext connection);
public abstract Task OnDisconnectedAsync(HubConnectionContext connection);

public abstract Task SendAllAsync(string methodName, object[] args, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendAllExceptAsync(string methodName, object[] args, IReadOnlyList&lt;string&gt; excludedConnectionIds, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendConnectionAsync(string connectionId, string methodName, object[] args, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendConnectionsAsync(IReadOnlyList&lt;string&gt; connectionIds, string methodName, object[] args, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendGroupAsync(string groupName, string methodName, object[] args, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendGroupsAsync(IReadOnlyList&lt;string&gt; groupNames, string methodName, object[] args, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList&lt;string&gt; excludedConnectionIds, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendUserAsync(string userId, string methodName, object[] args, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task SendUsersAsync(IReadOnlyList&lt;string&gt; userIds, string methodName, object[] args, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task AddToGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default(CancellationToken));

public abstract Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default(CancellationToken));

}

到此有点小懵逼了???,这里的方法都是返回Task,但我释放连接需要HubConnectionContext,岂不是无解???

虽然很像但是不是就很郁闷,既然_lifetimeManager做不了,就只能去看:

await RunHubAsync(connectionContext);
private async Task RunHubAsync(HubConnectionContext connection)

{

try

{

await _dispatcher.OnConnectedAsync(connection);

}

catch (Exception exception)

{

Log.ErrorDispatchingHubEvent(_logger, "OnConnectedAsync", exception);

await SendCloseAsync(connection, exception, allowReconnect: false);

return;

}

try

{

await DispatchMessagesAsync(connection);

}

catch (OperationCanceledException)

{

}

catch (Exception exception2)

{

Log.ErrorProcessingRequest(_logger, exception2);

await HubOnDisconnectedAsync(connection, exception2);

return;

}

await HubOnDisconnectedAsync(connection, null);

}

一个一个来,先看await _dispatcher.OnConnectedAsync(connection);

navigation -> Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher

public override async Task OnConnectedAsync(HubConnectionContext connection)
{
IServiceScope scope = null;
try
{
// 通过 service 拿到了THub
scope = _serviceScopeFactory.CreateScope();
IHubActivator<THub> hubActivator = scope.ServiceProvider.GetRequiredService<IHubActivator<THub>>();
THub hub = hubActivator.Create();
try
{
// 然后通过hub 和 连接进行初始化
InitializeHub(hub, connection);
await hub.OnConnectedAsync();
}
finally
{
hubActivator.Release(hub);
}
}
finally
{
await scope.DisposeAsync();
}
}
private void InitializeHub(THub hub, HubConnectionContext connection)

{

hub.Clients = new HubCallerClients(_hubContext.Clients, connection.ConnectionId); // 只用到了ConnectionId显然不是

hub.Context = connection.HubCallerContext; // 这个就有点可疑了

hub.Groups = _hubContext.Groups;// 只用到了分组应该也不是

}

查看HubConnectionContext的构造方法看看HubCallerContext是如何被构造的:

public HubConnectionContext(ConnectionContext connectionContext, HubConnectionContextOptions contextOptions, ILoggerFactory loggerFactory)
{
...
HubCallerContext = new DefaultHubCallerContext(this);
...
}

navigation -> Microsoft.AspNetCore.SignalR.Internal.DefaultHubCallerContext

internal class DefaultHubCallerContext : HubCallerContext
{
private readonly HubConnectionContext _connection;
public override string ConnectionId =&gt; _connection.ConnectionId;

public override string UserIdentifier =&gt; _connection.UserIdentifier;

public override ClaimsPrincipal User =&gt; _connection.User;

public override IDictionary&lt;object, object&gt; Items =&gt; _connection.Items;

public override IFeatureCollection Features =&gt; _connection.Features;

public override CancellationToken ConnectionAborted =&gt; _connection.ConnectionAborted;

public DefaultHubCallerContext(HubConnectionContext connection)
{
_connection = connection;
} public override void Abort()
{
// ************************
_connection.Abort();
}

}

Abort -> 使中止 推测是中止连接,而且通过源码可知调的是HubConnectionContext.Abort.

Hub中的定义:

public HubCallerContext Context
{
get
{
CheckDisposed();
return _context;
}
set
{
CheckDisposed();
_context = value;
}
}

通过测试结果可知,这个便是服务器中断连接的方法了

[Over~]

.net core signalR 服务端强制中断用户连接的更多相关文章

  1. 记录一次SignalR服务端实现过程

    前言:最近手上一个项目需要后端实时推送数据到前端,第一个想到的就是微软的SignalR,由于之前都是平时没事写的Demo,没有用到实际项目中,这次恰好用到了,因此记录下来整个实现过程(网上也有很多类似 ...

  2. SignalR入门之多平台SignalR服务端

    之前创建SignalR服务端是基于Web应用程序而言的.那么能不能把SignalR服务端做成控制台应用程序.Winform或windows服务呢? 答案是肯定的. 之前尽管看起来好像是IIS和ASP. ...

  3. 创建自托管的SignalR服务端

    微软官方例子地址:http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host 1.说明: SignalR服务端可 ...

  4. asp.net core webapi 服务端配置跨域

    在前后端分离开发中服务端仅仅只为前端提供api接口,并且前后端往往单独部署,此时就会出现浏览器跨域问题.asp.net core提供了简单优雅的解决方案. 在startup文件的Configure添加 ...

  5. HMS Core分析服务助您掌握用户分层密码,实现整体收益提升

    随着市场愈发成熟,开发者从平衡收益和风险的角度开始逐步探索混合变现的优势,内购+广告就是目前市场上混合变现的主要方式之一. 对于混合变现模式,您是否有这样的困惑: 如何判断哪些用户更愿意看广告.哪些用 ...

  6. RedHat下安装Telnet服务端及客户端远程连接配置

    Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力. 配置之前请确保网络连通,如防火墙影响连接,请先关 ...

  7. signalr服务端-基础搭建

    signalr 支持 iis托管.winform.windowsservices.wpf 托管 这里我采用winfrom托管 首先画一个这样的窗体 在服务项目通过项目管理包安装signalr类库 安装 ...

  8. .net core api服务端跨域配置

    第1步:添加包引用(.net core 2.2 已自带此包,可跳过此步骤) Install-Package Microsoft.AspNetCore.Cors 第2步:在Startup.cs文件的Co ...

  9. WPF创建SignalR服务端(转)

    在网上看到了一个帖子,比较详细,博主写的很好. 地址:http://blog.csdn.net/lordwish/article/details/51786200

随机推荐

  1. LeetCode 778. Swim in Rising Water

    题目链接:https://leetcode.com/problems/swim-in-rising-water/ 题意:已知一个n*n的网格,初始时的位置为(0,0),目标位置为(n-1,n-1),且 ...

  2. 微信小程序云开发-云存储-使用云开发控制台存储文件

    一.存储 进入[云开发控制台]>点击[存储].将需要存储的文件通过[上传文件]方式上传上去.或者通过拖拽的方式上传文件.  二.存储文件的类型 可以存储的文件有很多,常见的文件类型包括:word ...

  3. 一次搞懂JavaScript对象

    索引 目录 索引 1. 对象与类 2.对象使用 2.1 语法 2.2 属性 3.对象特性 4.对象的创建 4.1 字面量 4.2 工厂函数 4.3 构造函数 4.4 class类 4.5 对象与单例模 ...

  4. 项目启动报错 The server time zone value '�й���׼ʱ��' is unrecognize...

    背景介绍: 把项目在新的电脑上运行,MySQL版本不同出现错误 错误: 报错The server time zone value '�й���׼ʱ��' is unrecognized or repr ...

  5. odoo里的rpc用法

    import odoorpcdb_name = 'test-12'user_name = 'admin'password = 'admin'# Prepare the connection to th ...

  6. 第二十九篇 -- UDP和TCP

    最近在写WIFI模块,所以就想明确一些TCP和UDP的区别,发现以前的理解还是有点误区.现在重新学习. 相同点 UDP协议和TCP协议都是传输层协议 TCP(Transmission Control ...

  7. (opencv10)膨胀和侵蚀(Dilation与Erosion)

    (opencv10)膨胀和侵蚀(Dilation与Erosion) 图像形态学操作 图像形态学操作-基于形状的一系列图像处理操作的合集,主要是基于集合论基础上的形态学数学 形态学有四个基本操作:腐蚀, ...

  8. QML用Instantiator动态创建顶级窗口

    关键点 使用Model驱动Instantiator QML里面的hashmap: QQmlPropertyMap 上一次说到用 QQmlApplicationEngine 多次load的方式创建多个一 ...

  9. git从远程仓库里拉取一条本地不存在的分支

    查看远程分支和本地分支 git branch -va 当我想从远程仓库里拉取一条本地不存在的分支时: git checkout -b 本地分支名 origin/远程分支名 例如: 切换远程分支 git ...

  10. CC攻击和C2的区别

    [一]背景 今天被旁边姐姐问C2.CC是什么,虽然平时老看到这个词,身边也有自己写C2工具的大佬.但好像突然被问到有点懵,不知道怎么回答. [二]内容 CC ( Challenge Collapsar ...