zoukankan      html  css  js  c++  java
  • 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";
            }
        }

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

  • 相关阅读:
    .NET CF 枚举设备窗口
    .NET CF WM设备(手机)振动
    如何将 byte[] 转换为 IntPtr?
    Mobile 重启设备
    如何删除只读文件?
    随笔
    故乡的原风景
    岁月神偷
    opengl纹理映射
    bootstrap 学习笔记
  • 原文地址:https://www.cnblogs.com/huanyun/p/15303837.html
Copyright © 2011-2022 走看看