栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.Net Framework框架下实现SignalR客户端和服务端

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.Net Framework框架下实现SignalR客户端和服务端

1、SignalR客户端,新建控制台程序,.Net Framework平台

添加Nuget包

Microsoft.AspNet.SignalR.Client

 class Program
    {
        static void Main(string[] args)
        {
            IHubProxy hub;
            //服务端配置 Startup类中  app.MapSignalR();
            //string url = "http://localhost:5000/signalr";

            //服务端配置 Startup类中 app.MapSignalR("/hubs", new HubConfiguration());
            string url = "http://localhost:5000/hubs";
            HubConnection connection = new HubConnection(url);
            connection.ConnectionSlow += Connection_ConnectionSlow;
            connection.Error += Connection_Error;
            connection.StateChanged += Connection_StateChanged;
            ///服务端 配置 [HubName("MyTestHub")]
            hub = connection.CreateHubProxy("MyTestHub");
            connection.Start().Wait();
            hub.On("addMessage", x =>
            Console.WriteLine(x));
            hub.On("SendClient", x =>
            {
                Console.WriteLine(x);
            }
         ) ;

            string groupName = Console.ReadLine();
            hub.Invoke("Sign", groupName).Wait();

            string sendgroupName = Console.ReadLine();
            hub.Invoke("SameGroupMessage", sendgroupName).Wait();

            string line = "";
            while ((line = Console.ReadLine()) != null)
            {
                hub.Invoke("Send", "测试", line).Wait();
            }

            Console.ReadKey();

            //connection.Stop();
            //connection.Dispose();
        }

        private static void Connection_StateChanged(StateChange obj)
        {
            if (obj.NewState == ConnectionState.Disconnected)
            {
                Console.WriteLine("连接状态为未连接啦");
            }
        }

        private static void Connection_Error(Exception obj)
        {
            Console.WriteLine("连接发生错误啦");
        }

        private static void Connection_ConnectionSlow()
        {
            Console.WriteLine("连接超时啦");
        }
    }

2、SignalR服务端,新建控制台程序,.Net Framework平台

添加Nuget包

Microsoft.AspNet.SignalR.Core
Microsoft.Owin.Hosting
Microsoft.Owin.Cors
Microsoft.Owin.Host.HttpListener

 class Program
    {
        static void Main(string[] args)
        {
            using (var app = WebApp.Start("http://localhost:5000/"))
            {

                Console.WriteLine("服务器运行 http://localhost:5000/");
                Console.ReadLine();
            }
        }
    }

    public class Startup
    {
        //GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);
        //GlobalHost.Configuration.DefaultMessageBufferSize = 500;
        //GlobalHost.ConnectionManager.GetHubContext
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            //app.MapSignalR();
            app.MapSignalR("/hubs", new HubConfiguration());
        }
    }
    [HubName("MyTestHub")]
    public class MyHub : Hub
    {

        public void Send(string name, string message)
        {
            String msg = String.Format("Message send by {0}: {1}", name, message);
            Console.WriteLine(msg);
            Clients.Client(GlobalModel._ids[0]).addMessage(msg);

        }

        public void Sign(string projectNo)
        {
            Groups.Add(Context.ConnectionId, projectNo);

        }

        public void SameGroupMessage(string name)
        {
            Clients.Group(name).SendClient($"咱们是同一个组{name}");
        }

        public override Task OnConnected()
        {
            Console.WriteLine("Client connected: " + Context.ConnectionId);
            GlobalModel._ids.Add(Context.ConnectionId);

            return base.OnConnected();
        }
        public override Task OnDisconnected(Boolean stopCall)
        {
            Console.WriteLine("Client disconnected: " + Context.ConnectionId);
            return base.OnDisconnected(true);
        }
    }

    public class GlobalModel
    {

        public static List _ids = new List();
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923231.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号