zoukankan      html  css  js  c++  java
  • .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<Startup>("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<string> _ids = new List<string>();
        }
  • 相关阅读:
    在Js或者cess后加版本号 防止浏览器缓存
    svn操作
    Hash表
    网站js埋点
    c#优秀文章
    CentOS修改默认yum源为国内yum镜像源
    mysql开启远程连接
    安装jdk环境
    Eclipse的一下设置
    好用的在线HTML、CSS工具
  • 原文地址:https://www.cnblogs.com/lhwpc/p/15136723.html
Copyright © 2011-2022 走看看