首页 > 代码库 > SignalR---DOTNET客户端
SignalR---DOTNET客户端
直接上翻译,交流和指错173882121@qq.com。
这里面有用到异步的相关知识,本人前几篇文章也简单的提到。
SignalR客户端要寄宿在.NET的客户端,必须安装Microsoft.AspNet.SignalR.Client。 如何建立一个连接 var hubConneciton=new HubConnection("http://www.contos.com/"); IHubProxy stockTickerHubProxy=hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHubProxy.On<Stock>("UpdateStockPrice",stock=>Console.WrireLine("Stock update"); await hubConnection.Start(); ); Start是一个异步方法 C# await connection.Start(); css connection.Start().Wait(); 配置连接 在建立连接之前可以做一些配置。 设置最大并发数 var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)); ServicePointManager.DefaultConnectionLimit = 10; await hubConnection.Start(); 默认最大并发数为2。 添加连接字符串 客户端 var querystringData = http://www.mamicode.com/new Dictionary();"contosochatversion", "1.0"); var connection = new HubConnection("http://contoso.com/", querystringData); 服务端 public class StockTickerHub : Hub { public override Task OnConnected() { var version = Context.QueryString["contosochatversion"]; if (version != "1.0") { Clients.Caller.notifyWrongVersion(); } return base.OnConnected(); } } 设置传输方法 var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)); await hubConnection.Start(new LongPollingTransport()); 关于传输方式在Microsoft.AspNet.SignalR.Client.Transports命名空间下。 LongPollingTransport、ServerSentEventsTransport、WebSocketTranspor、WebSocketTranspor。 ForevenFrame不被包含在内,因为它只在浏览器中使用。 指定HTTP头信息 hubConnection = new hubConnection("http://www.contoso.com/"); connection.Headers.Add("headername", "headervalue"); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)); await connection.Start(); 指定客户端的证书 使用connection对象的AddClientCertificate方法。 hubConnection = new hubConnection("http://www.contoso.com/"); hubConnection.AddClientCertificate(X509Certificate.CreateFromCertFile("MyCert.cer")); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)); await connection.Start(); 创建Hub代理 服务端 public class StockTickerHub : Hub [HubName("stockTicker")] public class StockTickerHub : Hub 客户端 var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)); await hubConnection.Start(); var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("stockTicker"); stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)); await hubConnection.Start(); 定义客户端方法 使用代理的On方法去注册事件处理器,方法名符合驼峰命名法。 服务端的Client.All.UpdateStockPrice方法名将会匹配客户端的 udateStockPrice、updatestockprice、UpdateStockPrice方法。 没有参数的方法 服务端 public class StockTickerHub : Hub { public void NotifyAllClients() { Clients.All.Notify(); } } 客户端 var hubConnection = new HubConnection("http://www.contoso.com/"); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHub.On("notify", () => // Context is a reference to SynchronizationContext.Current Context.Post(delegate { textBox.Text += "Notified!\n"; }, null) ); await hubConnection.Start(); 有参数的方法 使用On方法的泛型On<T>指定参数的类型。 服务端 public void BroadcastStockPrice(Stock stock) { context.Clients.Others.UpdateStockPrice(stock); } public class Stock { public string Symbol { get; set; } public decimal Price { get; set; } } 客户端 stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => // Context is a reference to SynchronizationContext.Current Context.Post(delegate { textBox.Text += string.Format("Stock update for {0} new price {1}\n", stock.Symbol, stock.Price); }, null) ); 也可以为动态对象 stockTickerHubProxy.On("UpdateStockPrice", stock => // Context is a reference to SynchronizationContext.Current Context.Post(delegate { textBox.Text += string.Format("Stock update for {0} new price {1}\n", stock.Symbol, stock.Price); }, null) ); 不通过On的泛型指定参数类型。 移除方法 C# var updateStockPriceHandler = stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Context.Post(delegate { textBox.Text += string.Format("Stock update for {0} new price {1}\n", stock.Symbol, stock.Price); }, null) ); css updateStockPriceHandler.Dispose(); 客户端调用服务端的方法 使用Hub代理的Invoke方法。 没有返回值 服务端 public class StockTickerHub : Hub { public void JoinGroup(string groupName) { Groups.Add(Context.ConnectionId, groupName); } } 客户端 stockTickerHubProxy.Invoke("JoinGroup", "SignalRChatRoom"); 有返回值 服务端 public IEnumerable<Stock> AddStock(Stock stock) { _stockTicker.AddStock(stock); return _stockTicker.GetAllStocks(); } public class Stock { public string Symbol { get; set; } public decimal Price { get; set; } } 客户端 var stocks = stockTickerHub.Invoke<IEnumerable<Stock>>("AddStock", new Stock() { Symbol = "MSFT" }).Result; foreach (Stock stock in stocks) { textBox.Text += string.Format("Symbol: {0} price: {1}\n", stock.Symbol, stock.Price); } 连接生命周期事件 SignalR提供以下可以处理的连接生命周期事件: Received:在连接上收到任何数据时触发。提供收到的数据。 ConnectionSlow:当客户端检测到缓慢或频繁丢弃的连接时引发。 Reconnecting:当底层传输开始重新连接时引发。 Reconnected:当底层运输工具重新连接时提起。 StateChanged:连接状态发生变化时触发。提供旧状态和新状态 Closed:连接断开连接时触发。 示例: hubConnection.ConnectionSlow += () => Console.WriteLine("Connection problems."); 注册事件。 处理错误 如果没有在服务器上显式启用详细的错误消息,则SignalR在错误后返回的异常对象包含有关错误的最少信息。 服务端开启详细信息 var hubConfiguration = new HubConfiguration(); hubConfiguration.EnableDetailedErrors = true; App.MapSignalR(hubConfiguration); 为signalR引发的错误注册事件 hubConnection.Error += ex => Console.WriteLine("SignalR error: {0}", ex.Message); 在try-catch块中处理 try { IEnumerable<Stock> stocks = await stockTickerHub.Invoke<IEnumerable<Stock>>("GetAllStocks"); foreach (Stock stock in stocks) { Console.WriteLine("Symbol: {0} price: {1}", stock.Symbol, stock.Price); } } catch (Exception ex) { Console.WriteLine("Error invoking GetAllStocks: {0}", ex.Message); } 启用客户端日记记录 通过连接对象的TrackLevel和TraceWriter属性设置。 var hubConnection = new HubConnection("http://www.contoso.com/"); hubConnection.TraceLevel = TraceLevels.All; hubConnection.TraceWriter = Console.Out; IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub"); stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price)); await hubConnection.Start();
SignalR---DOTNET客户端
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。