如何在任务完成之前使C#中的集线器连接保持活动状态

时间:2018-06-29 07:08:31

标签: c# signalr

我正在使用send-Grid API发送电子邮件。大约10000封邮件。 我正在使用集线器进行通知,以检查邮件状态然后将其显示给用户。集线器每3分钟重新连接一次。然后该过程重新开始。

在任务完成之前如何保持相同的集线器连接有效。

我的代码

--> In js

         ///Reference the auto-generated proxy for the hub.
        var progress = $.connection.progressHub;

        // Create a function that the hub can call back to display messages.
        progress.client.AddProgress = function (message, percentage, delivered, error) {
            console.log(message);
            ProgressBarModal("show", delivered, error, message);
            $("#ProgressMessage").css("width", percentage + "%");
            //$('#ProgressMessage').width(percentage+"%");
            $('#ProgressMessage').text(percentage + "%");
            if (percentage == "100%") {
                ProgressBarModal();
            }
        };

        $.connection.hub.start({ pingInterval: null }).done(function () {
            var connectionId = $.connection.hub.id;
        });

        $.connection.hub.connectionSlow(function () {
            toastr.warning('Warning - Connection Is Slow...');
            //alert("connectionSlow");
            console.log("connectionSlow");
        });

        $.connection.hub.reconnecting(function () {
            toastr.info('Connection Reconnection..');
            //alert("Reconnecting Hub ");
            console.log("Reconnecting Hub ");
        });

        $.connection.hub.disconnected(function () {
            toastr.error('Connection Disconnected...');
            if ($.connection.hub.lastError) {
                alert("Disconnected. Reason: " + $.connection.hub.lastError.message);
            }
        });

->在Global.asax中

GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromDays(5.00);  GlobalHost.Configuration.KeepAlive = null;

--->在C#中,我在每个循环中调用集线器以显示每个邮件发送的通知及其状态。

1 个答案:

答案 0 :(得分:0)

我想您需要更改超时和Keepalive设置。像这样的东西:

protected void Application_Start(object sender, EventArgs e)
{
    // Make long polling connections wait a maximum of 110 seconds for a
    // response. When that time expires, trigger a timeout command and
    // make the client reconnect.
    GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);

    // Wait a maximum of 30 seconds after a transport connection is lost
    // before raising the Disconnected event to terminate the SignalR connection.
    GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);

    // For transports other than long polling, send a keepalive packet every
    // 10 seconds. 
    // This value must be no more than 1/3 of the DisconnectTimeout value.
    GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);

    RouteTable.Routes.MapHubs();
}

整个理解和Handling Connection Lifetime Events in SignalR article可能与您的问题有关。