本文介绍了有没有一种方法来检查SignalR服务器/集线器是通过服务器端代码的主动/上/就绪/接受连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个连接到集线器SignalR并发送信息等工作完全正常的控制台应用程序。 今天,当我开始我的控制台应用程序,我忘了。-also-开始我SignalR集线器(所以没有要冲上去,主动接受/发送邮件) 当然,我得到了一个低级错误连接:Is there a way that I can check to see if the Hub is online, active and accepting connections? Or is the only way to use a try/catch and make the assumption that any catch, it's offline?Here's my sample code:_hubConnection.Start().ContinueWith(task =>{ if (task.IsFaulted) { WriteLine( string.Format(" !!! There was an error opening the connection:{0}", task.Exception.GetBaseException()), ConsoleColor.Red); } else { WriteLine(" * Connected to the Hub @ http://localhost:80"); }}).Wait();Cheers! 解决方案 If you use Persistent Connection, it would be easier for you to find out if it's connected or not. If you want to use Hub instead of Persistent Connection, I'm not sure how to check.Below example is using Persistent Connection to check for the connection before start().Persistent Connectionpublic class HubPersistentConnection : PersistentConnection{ protected override Task OnConnected(IRequest request, string connectionId) { return Connection.Send(connectionId, "It's connected."); } // Override more method here}jQuery$(function () { var _hubConnection= $.connection('/echo'); _hubConnection.received(function (data) { //Do something }); _hubConnection.start().done(function () { //Do something });});Mapping Persistent Connection in Startup fileapp.MapSignalR<HubPersistentConnection>("/echo"); 这篇关于有没有一种方法来检查SignalR服务器/集线器是通过服务器端代码的主动/上/就绪/接受连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 05-30 03:23