ASP.NET Core SignalR系列之Hub教程

本文内容

                     什么是 SignalR 中心

                     配置 SignalR

                    创建和使用SignalR

                    向客户端发送消息

                    强类型中心

1. SignalR 中心

利用SignalR,你可以从服务端对链接的客户端调用方法。在服务端定义客户端调用的方法;在客户端定义服务的需要调用的方法。SignalR负责使实时的客户端到服务端和服务端到客户端的通信成为可能。

 

2. 配置 SignalR

         SignalR中间件需要一些服务,这些服务通过调用services.AddSignalR进行在Startup的ConfigureServices进行注册配置

public void ConfigureServices(IServiceCollection services)
{
	services.Configure<CookiePolicyOptions>(options =>
	{
		// This lambda determines whether user consent for non-essential cookies is needed for a given request.
		options.CheckConsentNeeded = context => true;
		options.MinimumSameSitePolicy = SameSiteMode.None;
	});


	services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
	services.AddCors(options => options.AddPolicy("CorsPolicy",
   builder =>
   {
	   builder.AllowAnyMethod().AllowAnyHeader()
			  .WithOrigins("http://localhost:51617")
			  .AllowCredentials();
   }));
	services.AddSignalR();//注册SignalR服务
}

将 SignalR 功能添加到 ASP.NET Core 应用程序时,请在 Startup.Configure 方法中调用 app.UseSignalR 来设置 SignalR 路由。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	if (env.IsDevelopment())
	{
		app.UseDeveloperExceptionPage();
	}
	else
	{
		app.UseExceptionHandler("/Error");
	}

	app.UseStaticFiles();
	app.UseCookiePolicy();
	app.UseSignalR(routes =>
	{
		routes.MapHub<StronglyTypedChatHub>("/chatHub");
	});
	app.UseCors("CorsPolicy");
	app.UseMvc();
}

3.创建和使用SignalR

public class ChatHub : Hub
{
    public Task SendMessage(string user, string message)
    {
        return Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

您可以指定返回类型和参数(包括复杂类型和数组),就像在任何C#方法中一样。 SignalR 处理参数和返回值中的复杂对象和数组的序列化和反序列化。

注:

       中心是暂时性的:

              不要将状态存储在 hub 类的属性中。 每个中心方法调用在新的中心实例上执行。

              调用依赖于中心保持活动状态的异步方法时,请使用 await。 例如,如果在没有 await 的情况下调用,则方法(如 Clients.All.SendAsync(...))可能会失败。

例如:在 ChatHub中添加一个列表_ ConnectionId, 用与获取由 SignalR分配的连接的唯一 ID。 每个连接都有一个连接 ID。

public class ChatHub : Hub
{

        public static List<string> _connectionId = new List<string>();

        public  Task SendMessage(string user, string message)
        {
           
            _connectionId.Add(Context.ConnectionId);
           return  Clients.All.SendAsync("ReceiveMessage", user, message, string.Join("#",_connectionId));


        }
}

20200105160443105.png

从图中我们发现_connectionId中存储的个数永远都只有一个就是当前点击客户端的ConnectionId这是由于 每个中心方法调用在新的中心实例上执行

 

如果创建一个静态类:

public static class staticClass
{
        public static List<string> connectionId = new List<string>();

}

 

public class ChatHub : Hub
{

   

	public  Task SendMessage(string user, string message)
	{
		staticClass.connectionId.Add(Context.ConnectionId);

		if (staticClass.connectionId.Count >1)
		{
			///随机生成索引
			Random r1 = new Random();
			int index = r1.Next(0, staticClass.connectionId.Count);

			return Clients.Client(staticClass.connectionId[index]).SendAsync("ReceiveMessage", user, message, Context.ConnectionId, Context.UserIdentifier);


		}
		else
		{
			return Clients.All.SendAsync("ReceiveMessage", user, message, Context.ConnectionId, Context.UserIdentifier);

		}
	   

	}
}

重新运行程序,获得结果:

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3ViYW5nYmFuZzE=,size_16,color_FFFFFF,t_70

4.强类型中心

 

优点:强类型的Hub可以避免魔法函数名,相比弱类型更容易维护和发现问题,直接上代码

缺点:代码编写多

具体步骤:

1.使用 Hub<T>强键入 Hub 代替SendAsync 方法 。 

服务器端链接客户端调用的方法将提取到 IChatClient接口中,实际上就是将ChatHub中使用字符串调用客户端方法,抽象为接口。

public interface IChatClient
{
    Task ReceiveMessage(string user, string message);
    
}

2.实现接口重构前面的 ChatHub 示例

 public class StronglyTypedChatHub: Hub<IChatClient>
    {
        public async Task SendMessage(string user, string message)
        {

            staticClass.connectionId.Add(Context.ConnectionId);

            if (staticClass.connectionId.Count > 1)
            {
                ///随机生成索引
                Random r1 = new Random();
                int index = r1.Next(0, staticClass.connectionId.Count);

                await Clients.Client(staticClass.connectionId[index]).ReceiveMessage( user, message, Context.ConnectionId);


            }
            else
            {
                await Clients.All.ReceiveMessage( user, message, Context.ConnectionId);

            }


           
        }
    }

3.将Configur中的路由

原来的

app.UseSignalR(route =>
{
	route.MapHub<StronglyTypedChatHub>("/chathub");
});

变为:

app.UseSignalR(route =>
{
    route.MapHub<StronglyTypedChatHub>("/chathub");
});

这是我的微信公共号,希望和大家一起成长,我会保持一天左右更新一次。 

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3ViYW5nYmFuZzE=,size_16,color_FFFFFF,t_70

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值