• vue netcore signalr写法


    let endpoint = localStorage.server_url + "/chathub";
        let s = new signalR.HubConnectionBuilder()
          .withUrl(endpoint)
          .configureLogging(signalR.LogLevel.Error)
          .build();
        s.on("RefreshMessage", (data) => {
          console.log(data);
          this.executeQueryPage();
        });
        s.on("NoticeMessage", (data) => {
          console.log(data);
          this.NoticeMessage(data);
        });
        s.start();
    public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers().AddNewtonsoftJson();
                
                services.AddSignalR().AddJsonProtocol();
            }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Microsoft.AspNetCore.Hosting.IApplicationLifetime lifetime)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }            
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                    endpoints.MapHub<ChatHub>("/ChatHub");
                });
            }
    
    public class ChatHub : Hub
        {
           
        }
    
    public class CustomController : ControllerBase
        {
            private readonly IHubContext<ChatHub> _hub;
            public CustomController(IHubContext<ChatHub> hub)
            {
                _hub = hub;
            }
            [HttpGet]
            public ActionResult<object> RefreshMessage(string info)
            {
                _hub.Clients.All.SendAsync("RefreshMessage", info);
                return "ok";
            }
            [HttpGet]
            public ActionResult<object> NoticeMessage(string info)
            {
                _hub.Clients.All.SendAsync("NoticeMessage", info);
                return "ok";
            }
        }

    后端直接触发接口,即可往前端主动发送消息

  • 相关阅读:
    census 安全处理模式
    基于squid 暴露k8s 服务
    nginx 动态模块问题
    juicefs 多s3 bucket 使用
    k8s 数据卷需要很长时间才能挂载成功
    一种基于s3 管理haproxy 配置的模式
    WebSub 互联网分布式\订阅标准
    maven 多模块父模块问题deploy 问题
    nginx 作为s3 的gateway
    juicefs 单机试用
  • 原文地址:https://www.cnblogs.com/huanyun/p/15303837.html
Copyright © 2020-2023  润新知