zoukankan      html  css  js  c++  java
  • ASP.NET MVC SignalR 配合VUE

    添加Hub

    右键项目→添加新项

    NotifyHub.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hubs;
    
    namespace GZCCC.API.SignalRLibs
    {
        [HubName("NotifyHub")]
        public class NotifyHub : Hub
        {
            public override Task OnConnected()
            {
                return base.OnConnected();
            }
    
            ////用户进入页面时执行的(连接操作)
            //public void controllerConnected(string UserID)
            //{
            //    UserHandler.Users.Add(Context.ConnectionId, UserID);
            //}
    
    
            //发送信息给特定人
            public void sendMessage(string Message)
            {
                Clients.All.sendMessage(Message);
                //var lst = UserHandler.Users.Where(p => p.Value == toUserID).ToArray();
                //foreach (var Client in lst)
                //{
                //    Clients.Client(Client.Key).sendMessage(Message);
                //}
    
            }
    
            public override Task OnDisconnected(bool stopCalled)
            {
                Clients.All.removeList(Context.ConnectionId);
                return base.OnDisconnected(stopCalled);
            }
            ////当使用者断线时执行
            //public override Task OnDisconnected(bool stopCalled)
            //{
            //    //当使用者离开时,移除在清单内的ConnectionId
    
            //    Clients.All.removeList(Context.ConnectionId);
            //    //UserHandler.Users.Remove(Context.ConnectionId);
            //    //return base.OnDisconnected(stopCalled);
            //}
        }
        //声明静态变量存储当前在线用户
        public static class UserHandler
        {
            public static Dictionary<string, string> Users = new Dictionary<string, string>();
        }
    }

    添加Startup.cs

     Startup.cs

    using System;
    using System.Threading.Tasks;
    using System.Web.Http;
    using Microsoft.Owin;
    using Owin;
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hubs;
    using Microsoft.Owin.Cors;
    
    [assembly: OwinStartup(typeof(GZCCC.API.SignalRLibs.Startup))]
    
    namespace GZCCC.API.SignalRLibs
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
               
                // 有关如何配置应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkID=316888
                //app.MapSignalR();
    
                app.Map("/signalr", map =>
                {
                    // Setup the CORS middleware to run before SignalR.
                    // By default this will allow all origins. You can 
                    // configure the set of origins and/or http verbs by
                    // providing a cors options with a different policy.
                    
                    map.UseCors(CorsOptions.AllowAll);
                    var hubConfiguration = new HubConfiguration
                    {
                        // You can enable JSONP by uncommenting line below.
                        // JSONP requests are insecure but some older browsers (and some
                        // versions of IE) require JSONP to work cross domain
                        // EnableJSONP = true
                    };
                    // Run the SignalR pipeline. We're not using MapSignalR
                    // since this branch already runs under the "/signalr"
                    // path.
                    map.RunSignalR(hubConfiguration);
                });
    
    
            }
    
        }
    }

    跨域配置需要添加Nuget

    Vue项目中引用:

    添加Npm包

    npm i -D -S signalr-no-jquery

     

    vue代码,在created中

    慎于行,敏于思!GGGGGG
  • 相关阅读:
    Windows Internals学习笔记(八)IO系统
    FPGA相关术语(一)
    Bilinear Filter
    总线与接口
    GPIO相关知识
    .NET IL学习笔记(一)
    Windows Internals学习笔记(七)Image Loader
    Windows Internals学习笔记(五)Synchronization
    struts2官方 中文教程 系列五:处理表单
    struts2官方 中文教程 系列四:Action
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/12939724.html
Copyright © 2011-2022 走看看