.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. python基础之操作数据库(pymysql)操作

    import pymysqlimport datetime#安装 pip install pymysql"""1.连接本地数据库2.建立游标3.创建表4.插入表数据.查询 ...

  2. vue(23)Vuex的5个核心概念

    Vuex的核心概念 Vuex有5个核心概念,分别是State,Getters,mutations,Actions,Modules. State   Vuex使用单一状态树,也就是说,用一个对象包含了所 ...

  3. 谈一下python中的列表

    Python标准库基于C语言实现了丰富的序列类型包括元组,列表,字典... 今天我们只谈list(列表) 1 列表(list) 最基础也是最重要的序列类型,他本身可以存放不同数据类型的元素.列表推导是 ...

  4. argparse模块基本用法

    argparse模块基本用法 在 python 编写的程序中,我们经常会看到的 argparse 相关代码,而它究竟怎么使用呢?接招! argparse 是一个命令行参数解析模块 现在提出需求,我需要 ...

  5. vue-- 利用过滤-实现搜索框的姓名的搜索

    <div class="fl w-200 m-l-30"> <el-input placeholder="输入用户名" v-model=&qu ...

  6. 一文带你搞定AOP切面

    摘要:AOP在spring中又叫"面向切面编程",是对传统我们面向对象编程的一个补充,主要操作对象就是"切面",可以简单的理解它是贯穿于方法之中,在方法执行前. ...

  7. jboss未授权访问

    测试 poc地址 https://github.com/joaomatosf/jexboss

  8. 浏览器中hook对象属性

    先获取window对象属性 来源: 夜幕爬虫安全论坛 原文链接: http://bbs.nightteam.cn/thread-485.htm?orderby=desc&user=7

  9. “入职一年,那个被高薪挖来的Android开发被劝退了。”

    其实,在很多小伙伴的想法中,是希望通过跳槽实现薪酬涨幅,可是跳槽不是冲动后决定,应该谨慎啊~ 01 我的学弟,最近向我吐槽,2020 年上半年入职一家公司,当时是高薪挖走的他,所谓钱到位,工作也是充满 ...

  10. Linux常见问题解决方案

    1.Kali2020添加BCM43142的网卡驱动 来源:https://www.fujieace.com/kali-linux/wifi-drive.html 我只是执行了第三步:安装网卡驱动,即: ...