• 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";
            }
        }

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

  • 相关阅读:
    链接程序和库
    setjmp.h 文件解析与使用
    程序中的.dll .lib .def 和 .exp文件
    GDB开发测试相关
    gdb调试方法
    gdb代码分析
    ln对目录下所有文件做软链接
    python pexecpt模块 实现自动交互
    vscode中less自动输出为wxss或者css
    cmake
  • 原文地址:https://www.cnblogs.com/huanyun/p/15303837.html
Copyright © 2020-2023  润新知