EasySignalr 2.2.0
dotnet add package EasySignalr --version 2.2.0
NuGet\Install-Package EasySignalr -Version 2.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasySignalr" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasySignalr" Version="2.2.0" />
<PackageReference Include="EasySignalr" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EasySignalr --version 2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasySignalr, 2.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=EasySignalr&version=2.2.0
#tool nuget:?package=EasySignalr&version=2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
欢迎使用 EasySignalr 减化了signalr在服务端的配置
EasySignalr,减化了signalr在netCore web服务器端的配置,用户直接参考下面的使用说明就可再项目中集成signalr
EasySignalr 使用说明
引用 EasySignalr
执行命令:Install-Package EasySignalr -Version 2.2.0
服务端配置
StartUp.cs 中 ConfigureServices 配置。
public void ConfigureServices(IServiceCollection services){
//securityKey 为你web项目 jwt token 生成的密钥 ,
// 传入 securityKey 密钥是为了 singalr再解析 客户连接时携带的 jwt token,来确认用户的身份
EasySignalrConfig.ConfigureServices(services,securityKey);
//NameUserIdProvider 集成IUserIdProvider,是用户为signalr 提供连接客户端与用户的对应关系
services.AddSingleton<IUserIdProvider, NameUserIdProvider>();
// 以上配置再 services.AddMvc(); 之前
services.AddMvc();
}
StartUp.cs 中 Configure 配置。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions<SenparcSetting> senparcSetting, IOptions<SenparcWeixinSetting> senparcWeixinSetting){
EasySignalrConfig.Configure(app);
}
自定义用户标识处理
NameUserIdProvider 样例
public class NameUserIdProvider : IUserIdProvider
{
public string GetUserId(HubConnectionContext connection)
{
// customUserService 你项目中的用户服务 ,根据认证中的标识获取用户的唯一标识ID
//这段代码只是示列代码 你根据你项目中的认证模式返回唯一的用户标识即可
return customUserService.find(connection.User?.Identity?.Name).userID;
//return connection.User?.FindFirst(ClaimTypes.Email)?.Value; 如果token中包含邮箱可以直接用邮箱昨晚唯一标识
//return connection.User?.Identity?.Name 如果token中的name为唯一标识 也可以直接返回作为唯一标识
}
}
在带的 Hub
[Authorize]
public class DefaultHub : Hub
{
/// <summary>
/// 向所有客户端广播消息
/// </summary>
/// <param name="method">调用所有客户端的方法</param>
/// <param name="message">消息体</param>
/// <returns></returns>
public async Task broadcastMessage(string method, string message)
{
await Clients.All.SendAsync(method, message);
}
/// <summary>
/// 向指定客户端发送消息
/// </summary>
/// <param name="method">客户端监听的方法</param>
/// <param name="userID">用户ID 与 IUserIdProvider 返回的userID一致</param>
/// <param name="message">消息体</param>
/// <returns></returns>
public async Task sendMessageToClient(string method,string userID, string message)
{
await Clients.User(userID)?.SendAsync(method, message);
}
}
配置完成后 自带路径/realtimemsg
服务端配置完成.
客户端使用示列
var connection = new signalR.HubConnectionBuilder()
.withUrl(`/realtimemsg`, {
accessTokenFactory: () => this.token
})
.build();
console.log("创建连接:", connection);
//开始连接服务器
connection.start().then(function () {
console.log("连接成功");
}).catch(function (err) {
return console.error('连接异常:', err.toString());
});
//客户端监听自定义消息key onbroadcastMessage
connection.on("onbroadcastMessage", (message) => {
console.log(message);
});
//下面代码是一个测试,在1秒后 向所有客户端发送消息
//broadcastMessage 为 EasySignalr中默认的 hub中的广播方法
//onbroadcastMessage 为客户端监听的消息key 可以自定义
setTimeout(() => {
//连接服务器数据中心,向 SendMessage 发出请求:broadcastMessage
connection.invoke('broadcastMessage', 'onbroadcastMessage', '消息内容');
}, 10000);
//下面代码是一个测试,在1秒后 向指定客户端发送消息
//sendMessageToClient 为 EasySignalr中默认的 hub中的指定客户消息发送方法
//onbroadcastMessage 为客户端监听的消息key,可以自定义
setTimeout(() => {
//连接服务器数据中心,向 SendMessage 发出请求:broadcastMessage
connection.invoke('sendMessageToClient', 'onbroadcastMessage',,'userID', '消息内容');
}, 10000);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.2 is compatible. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 2.2
- IdentityServer4 (>= 2.2.0)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 2.2.0)
- Microsoft.AspNetCore.Mvc (>= 2.2.0)
- Microsoft.AspNetCore.SignalR (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.2.0 | 637 | 3/21/2020 |