SignalR:检测在C#中的客户活动连接 [英] SignalR: Detecting Alive Connection in C# clients

查看:860
本文介绍了SignalR:检测在C#中的客户活动连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发使用SignalR(2.1)轮毂的应用程序。

I am currently developing an application using SignalR (2.1) Hubs.

我有1 WPF客户端,另一个是WCF客户端。
一切正常的,因为它们完美地传递消息。

I have 1 WPF client and the other is a WCF client. Everything works fine in that they are passing the messages perfectly.

我现在面临的唯一问题是,我注意到OnDisconnected将不会触发在所有的时候关闭应用程序的原因,如自动重新启动,WCF服务器宕机和其他几个人。超时是30秒的默认。即使后第1天已经过去了这是从来没有所谓的(我试过)。然而,超时工作的Web客户端。

The only problem I faced now is that I noticed the OnDisconnected is not fired at all when the application shuts down for reasons such as auto-restarts, WCF server went down and a few others. The timeout is the default of 30 seconds. It is never called even after 1 day has passed (I tried). However, the timeout works for Web Clients.

当我打电话hub.connection.stop作品()。

It only works when I call hub.connection.stop().

Ondisconnected方法,但是效果非常好,当客户端是一个浏览器。

The Ondisconnected method however works very well when the client is a browser.

因此​​,我想询问是否有任何方法对信号R集线器方能够检查客户端是否仍然连接或已经辍学(如平)?

Thus, I would like to ask whether there is any way for Signal R Hub side to be able to check whether the client is still connected or has already dropped out (such as a ping)?

推荐答案

在SignalR 2.1.0,有一个的new超载OnDisconnected ,需要一个布尔值,指示客户端是否正常,或没有断开。这种变化背后的推理 2.1.0发行说明

In SignalR 2.1.0, there is a new overload to OnDisconnected that takes a bool indicating whether the client disconnected gracefully or not. The reasoning behind this change is explained in the "Breaking Changes" section of the 2.1.0 release notes.

您新OnDisconnected方法可能看起来是这样的:

Your new OnDisconnected method could look something like this:

public override Task OnDisconnected(bool stopCalled)
{
    if (stopCalled)
    {
        // We know that Stop() was called on the client,
        // and the connection shut down gracefully.
    }
    else
    {
        // This server hasn't heard from the client in the last ~35 seconds.
        // If SignalR is behind a load balancer with scaleout configured, 
        // the client may still be connected to another SignalR server.
    }

    return base.OnDisconnected(stopCalled);
}

这并不需要一个布尔旧OnDisconnected方法不要求非-graceful断开,所以如果你正在使用的情况下,这可以解释你所看到的问题。

The old OnDisconnected method that doesn't take a bool is not called for non-graceful disconnects, so if you are using that event, that could explain the issue you are seeing.

此前2.1.0,则(只)OnDisconnected方法,没'T采取的一个参数被称为两个优美的的非正常断开。由于这种行为改变已经引起多个已报告的问题,老OnDisconnected超载的被删除github.com/SignalR/SignalR/pull/3126\">is。

Prior to 2.1.0, the (only) OnDisconnected method which didn't take a parameter was called for both graceful and non-graceful disconnects. Since this change in behavior has caused several reported issues, the old OnDisconnected overload is being removed in SignalR's upcoming 2.1.1 release.

这将导致使用SignalR的应用老OnDisconnected方法失败时对SignalR 2.1.1内置编译。虽然这不是理想的,它有望使开发商意识到这个重大更改,所以他们必须部署之前适当地修改他们的应用程序的机会。

This will cause applications that use SignalR's old OnDisconnected method to fail to compile when built against SignalR 2.1.1. While this isn't ideal, it will hopefully make developers aware of this breaking change so they have the opportunity to modify their apps appropriately before deploying them.

这篇关于SignalR:检测在C#中的客户活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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