SignalR指定用户推送

添加SignalR推送官方文档:
https://docs.microsoft.com/zh-cn/aspnet/core/signalr/groups?view=aspnetcore-5.0

项目安装Microsoft.AspNetCore.SignalR包

配置集线器类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;

namespace WebNetCore5_Img_Storage.Handler
{
    //继承IUserIdProvider用于告诉SignalR连接用谁来绑定用户id
    public class GetUserId : IUserIdProvider
    {
        string IUserIdProvider.GetUserId(HubConnectionContext connection)
        {
            //获取当前登录用户id
            string userid = connection.GetHttpContext().Session.GetString("user_id");
            return userid;
        }
    }

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

        //特定用户推送消息
        public async Task SendPrivateMessage(string user, string message)
        {
            await Clients.User(user).SendAsync("ReceiveMessage", message);
        }
    }
}

C#推送代码

 public class FileUploadController : BaseController
    {
        private readonly IHubContext<ChatHub> hubContext;

        public FileUploadController(IHubContext<ChatHub> _hubContext)
        {        
            hubContext = _hubContext;
        }
		
		
public async Task<ActionResult> UploadBigFile(IFormFile file)
{
	//当前登录用户id
	string loginUserId=Current.Id;
	
	//后台上传进度
	decimal process = Math.Round((decimal)fileNo * 100 / total_minio_file_count, 2);	 
	
	//推送消息到前端
	hubContext.Clients.User(loginUserId).SendAsync("ReceiveMessage", loginUserId, process);								
}

配置启动类注册IUserIdProvider的实例

   // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //解决中文输出后被编码了
            services.Configure<Microsoft.Extensions.WebEncoders.WebEncoderOptions>(options =>
            {
                options.TextEncoderSettings = new System.Text.Encodings.Web.TextEncoderSettings(System.Text.Unicode.UnicodeRanges.All);
            });
            //services.AddControllersWithViews();

            //添加全局筛选器:
            services.AddControllersWithViews(options =>
            {
                options.Filters.Add(typeof(CustomExceptionFilter));
            })
             //自定义格式化json使用Newtonsoft.Json
             .AddNewtonsoftJson();

            //services.addNew
            services.AddRazorPages();

            //session配置缓存组件依赖,分布式缓存
            //services.AddDistributedMemoryCache();

            //会话存活时间,单位(秒)
            int sessionTime = 0;
            int.TryParse(MyConfigReader.GetConfigValue("session_live_time"), out sessionTime);
            if (sessionTime == 0)
            {
                sessionTime = 7200;
            }
            services.AddSession(options =>
            {
                //设置cookie名称
                //options.Cookie.Name = ".AspNetCore.Session";
                //会话滑动过期时间,距下次访问最小时间,超过则session失效
                options.IdleTimeout = TimeSpan.FromSeconds(sessionTime);
                options.Cookie.HttpOnly = true;
                options.Cookie.IsEssential = true;
            });

            //业务层注入  
            //services.AddScoped<IImgBLL, ImgBLLImpl>();

            string path = AppDomain.CurrentDomain.BaseDirectory;
            Assembly bll_impl = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.BLL.dll");
            Assembly bll_interface = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.IBLL.dll");
            var typesInterface = bll_interface.GetTypes();
            var typesImpl = bll_impl.GetTypes();
            foreach (var item in typesInterface)
            {
                var name = item.Name.Substring(1);
                string implBLLImpName = name + "Impl";
                var impl = typesImpl.FirstOrDefault(w => w.Name.Equals(implBLLImpName));
                if (impl != null)
                {
                    //services.AddTransient(item, impl);
                    //services.AddSingleton(item, impl);
                    services.AddScoped(item, impl);
                }
            }

            //数据层注册
            Assembly dalAssemblys = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.DAL.dll");
            Assembly dalInterface = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.IDAL.dll");
            var dalTypesImpl = dalAssemblys.GetTypes();
            var dalTypesInterface = dalInterface.GetTypes();
            foreach (var item in dalTypesInterface)
            {
                var name = item.Name.Substring(1);
                string implDalName = name + "Impl";
                var impl = dalTypesImpl.FirstOrDefault(w => w.Name.Equals(implDalName));
                if (impl != null)
                {
                    //services.AddTransient(item, impl);
                    services.AddScoped(item, impl);
                }
            }
 
            services.AddMvcCore();
    
		    //用户IUserIdProvider实例注册
            services.AddScoped<Microsoft.AspNetCore.SignalR.IUserIdProvider, GetUserId>();
			
		    //SignalR添加到服务
            services.AddSignalR();
  
        }
		
		
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }
            app.UseHttpsRedirection();
                  
            app.UseStaticFiles(new StaticFileOptions()
            {
                //不限制content-type下载
                ServeUnknownFileTypes = true,

                配置的虚拟路径映射
                //RequestPath = "/local",
                物理地址
                //FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider("D:\\Work\\西南油气田图库系统\\WebNetCore5_Img_Storage\\WebNetCore5_Img_Storage\\bin\\Debug\\net5.0"),
            });

            app.UseRouting();

            //app.UseAuthentication();
            app.UseAuthorization();

            app.UseSession();
          

            //app.UseResponseCaching();
            //app.UseResponseCompression();

            //用MVC模式, 针对services的services.AddControllersWithViews();
            app.UseEndpoints(endpoints =>
            {
                //endpoints.MapDefaultControllerRoute();
                endpoints.MapRazorPages();

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");

                endpoints.MapHub<ChatHub>("/chathub");
            });
			

前端界面js代码

    <script src="~/lib/microsoft/signalr/dist/browser/signalr.min.js"></script>
    
    //signalR推送消息,用于显示后台实时处理进度
            var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
            connection.on("ReceiveMessage", function (user, message) {
                console.log("user=" + user + ",message=" + message);
                //后台上传视频到文件系统进度值
                $("#back_process_value").html(message+"%");
            });
            connection.start().then(function () {
                console.log("signalR推送连接成功");
            }).catch(function (err) {
                console.error("signalR推送连接异常");
                return console.error(err.toString());
            });
            //document.getElementById("sendButton").addEventListener("click", function (event) {
            //    var user = document.getElementById("userInput").value;
            //    var message = document.getElementById("messageInput").value;
            //    connection.invoke("SendMessage", user, message).catch(function (err) {
            //        return console.error(err.toString());
            //    });
            //    event.preventDefault();
            //});
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在 ASP.NET MVC 中使用 SignalR 实现推送功能需要以下步骤: 1. 在 Visual Studio 中创建一个 ASP.NET MVC 项目。 2. 通过 NuGet 安装 SignalR 库。 3. 创建一个 SignalR Hub 类。Hub 类是用来处理客户端和服务器之间的连接和消息传递的核心组件。 4. 在 Startup.cs 中配置 SignalR 中间件。 5. 在客户端 JavaScript 中引用 SignalR 库,并连接到 SignalR Hub。 6. 在 SignalR Hub 中定义需要推送的消息方法,然后在服务器端调用该方法以向客户端发送消息。 下面是一个简单的示例: 1. 创建一个名为 ChatHub 的 SignalR Hub 类: ```csharp using Microsoft.AspNetCore.SignalR; using System.Threading.Tasks; namespace MvcSignalR.Hubs { public class ChatHub : Hub { public async Task SendMessage(string user, string message) { await Clients.All.SendAsync("ReceiveMessage", user, message); } } } ``` 2. 在 Startup.cs 中配置 SignalR 中间件: ```csharp using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using MvcSignalR.Hubs; namespace MvcSignalR { public class Startup { public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Configuration = configuration; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSignalR(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseStaticFiles(); app.UseSignalR(routes => { routes.MapHub<ChatHub>("/chathub"); }); app.UseMvcWithDefaultRoute(); } } } ``` 3. 在客户端 JavaScript 中引用 SignalR 库,并连接到 SignalR Hub: ```javascript <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="/lib/signalr/dist/browser/signalr.js"></script> <script> var connection = new signalR.HubConnectionBuilder().withUrl("/chathub").build(); connection.start().then(function () { console.log("connected"); }).catch(function (err) { return console.error(err.toString()); }); connection.on("ReceiveMessage", function (user, message) { console.log(user + " says " + message); }); $("#sendButton").click(function () { var user = $("#userInput").val(); var message = $("#messageInput").val(); connection.invoke("SendMessage", user, message).catch(function (err) { return console.error(err.toString()); }); }); </script> ``` 4. 在 SignalR Hub 中定义需要推送的消息方法,然后在服务器端调用该方法以向客户端发送消息。 在这个示例中,当客户端调用 SendMessage 方法时,服务器将向所有连接的客户端发送 ReceiveMessage 消息。 以上就是在 ASP.NET MVC 中使用 SignalR 实现推送功能的基本步骤。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值