有没有一种方法来检查SignalR服务器/集线器是通过服务器端代码的主动/上/就绪/接受连接? [英] Is there a way to check if a SignalR server/hub is active/up/ready/accepting connections via server side code?

查看:227
本文介绍了有没有一种方法来检查SignalR服务器/集线器是通过服务器端代码的主动/上/就绪/接受连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到集线器SignalR并发送信息等工作完全正常的控制台应用程序。



今天,当我开始我的控制台应用程序,我忘了。-also-开始我SignalR集线器(所以没有要冲上去,主动接受/发送邮件)



当然,我得到了一个低级错误连接:





服务器正在积极拒绝连接,等等...





有没有办法,我可以检查,看看是否集线器处于联机状态,主动接受和连接的方式?或者是使用一个try / catch并作一个假设,任何追赶的唯一途径,它的下线



下面是我的示例代码:

  _hubConnection.Start()ContinueWith(任务=方式> 
{
如果(task.IsFaulted)
{
的WriteLine(
的String.Format(!有一个错误打开连接:{0},
task.Exception.GetBaseException()),
ConsoleColor.Red) ;
}
,否则
{
的WriteLine(*连接到集线器@ HTTP://本地主机:80);
}
}) 。等待();



干杯!


解决方案

如果您使用永久连接,这将是您更容易发现它是否连接与否。如果你想使用集线器而不是永久连接,我不知道如何检查。



下面的例子是使用永久连接检查()开始之前的连接。



持续连接

 公共类HubPersistentConnection:PersistentConnection 
{
保护覆盖任务OnConnected(IRequest要求,字符串的ConnectionId)
{
返回Connection.Send(ConnectionId之外,这是连接。);
}
//覆盖更多的在这里的方法
}

< STRONG>的jQuery

  $(函数(){
变种_hubConnection = $■连接('/回声');
_hubConnection.received(功能(数据){
//做些什么
});(。)来完成(功能()
_hubConnection.start {
//动作
});
});



在启动映射永久连接文件

  app.MapSignalR< HubPersistentConnection>(/回声); 


I have a console application that connects to SignalR Hub and sends messages, etc. Works perfectly fine.

Today, when I started my console app, I forgot to -also- start my SignalR hub (so there was no hub up, active and accepting/sending messages).

Of course, I got a low level connect error:

Server is actively refusing connections, etc...

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 Connection

public 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 file

app.MapSignalR<HubPersistentConnection>("/echo");

这篇关于有没有一种方法来检查SignalR服务器/集线器是通过服务器端代码的主动/上/就绪/接受连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆