使用 SignalR、WCF 双工服务和 ASP.Net 通用处理程序向客户端推送通知?

Push notifications to client with SignalR,WCF duplex service and ASP.Net generic handler?

我应该使用开发的 WCF 双工服务将文件发送到指定的 IP 地址, 所以我有一些回调操作,它们应该用作客户端的通知,我得出这个结论是使用 SignalR 但我是 SignalR 的新手,因为这个原因实际上不知道 SignalR 是否适合做这个.

让我们看看我在处理什么代码,在 ASP.Net 通用处理程序中我的 "SendToServer" 操作和使用 WCF 客户端代理如下:

SendClient sendClient = new SendClient(new SendCallback(),new System.ServiceModel.NetTcpBinding(),new System.ServiceModel.EndpointAddress(endPointAddress)); 
        sendClient.OperationFailed += sendClient_OperationFailed;
        sendClient.OperationTimedOut += sendClient_OperationTimedOut;
        sendClient.SendingFinished += sendClient_SendingFinished;
        sendClient.ConnectionClosed += sendClient_ConnectionClosed;
        sendClient.ConnectionRefused += sendClient_ConnectionRefused;
        sendClient.InstanceStored += sendClient_InstanceStored;
sendClient.Send(/*Array of resources ids*/,  /*Server instance*/);

我的事件处理程序如下:

public void sendClient_InstanceStored(object sender, int currentInstance, int totalInstance, int currentStudy, int TotalStudy)
    { 
        //Get fired when one file successfully sent
    } 
    public void sendClient_ConnectionRefused(object sender, EventArgs e)
    {
        //Connection refused
    }

    public void sendClient_ConnectionClosed(object sender, EventArgs e)
    {
        //Connection closed
    }

    public void sendClient_SendingFinished(object sender, EventArgs e)
    {
        //Sending finished
    }

    public void sendClient_OperationTimedOut(object sender, EventArgs e)
    {
        //Operation timed out
    }

    public void sendClient_OperationFailed(object sender, EventArgs e)
    {
        //Operation failed
    }

在JS中调用这个动作如下:

 $.ajax({
            cache: false,
            type: "POST",
            url: '../Handlers/Study/Send.ashx',
            dataType: "json",
            data: {
                Action: "SendToServer",
                Hostname: DeviceHostname, Port: DevicePort, Description: Description, Ids2Send: JSON.stringify(rows)
            },
            async: true,
            success: function (data) {
                if (data.Success == false) {
                    $("#loader-Send").remove();
                    $(".ui-dialog-buttonpane button:contains('Send')").button("enable");
                    showNoticeMessage("Can not send to Server!");
                    return;
                }
            },
            error: function (x, e) {
                $("#loader-Send").remove();
                $(".ui-dialog-buttonpane button:contains('Send')").button("enable");
                showNoticeMessage("Can not send to Server!");
            }
        });

是否可以在启动后在客户端使用该事件处理程序作为 SignalR 的通知$.ajax

有没有其他方法可以在不使用 SignalR 的情况下做到这一点?如何做?

提前致谢。

如果有人需要这里是解决方案:

1- SignalR 应该安装。

2- 进行 Owin 启动 class,例如:

   public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        app.MapSignalR();
    }
}

3- 定义并实现您自己的通知 class 如:

[HubName("sendNotifier")]
public class SendNotifier : Hub
{
    public string CurrentConnectionID { get; set; }
    public void SendStarted()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).sendStarted();
    }
    public void SendFailed()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).sendFailed();
    }
    public void InstanceStored(int currentInstance, int totalInstance, int currentStudy, int totalStudy)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).instanceStored(currentInstance, totalInstance, currentStudy, totalStudy);
    }
    public void SendFinished()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).sendFinished();
    }
    public void ConnectionClosed()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).connectionClosed();
    }
    public void ConnectionTimedOut()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).connectionTimedOut();
    }
    public void ConnectionRefused()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).connectionRefused();
    }

    public void ConnectionFailed()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<SendNotifier>();
        if (CurrentConnectionID != null)
            context.Clients.Client(CurrentConnectionID).connectionFailed();
    }
}

4- 在事件处理程序中,我们可以调用我们的通知方法,例如

 public void sendClient_ConnectionEstablished(object sender, EventArgs e)
    {
        SendNotifier sendNotifier = new SendNotifier();
        sendNotifier.CurrentConnectionID = ClientID;
        sendNotifier.SendStarted();
    }

注意:我在 Ajax 请求中传递 ClientID

和客户端代码:

 var hub = $.connection.sendNotifier;
    hub.client.sendStarted = function () {

    };

    hub.client.sendFinished = function () {
    };

    hub.client.sendFailed = function () {
    };
    //var temp;
    hub.client.instanceStored = function (currentInstance, totalInstance, currentStudy, totalStudy) {
    };

    hub.client.connectionClosed = function () {
    };

    hub.client.connectionTimedOut = function () {
    };

    hub.client.connectionRefused = function () {
    };

    hub.client.connectionFailed = function () {
    };

    $.connection.hub.logging = true;
    $.connection.hub.start().done(function () {
    });

ClientIDClientID: $.connection.hub.id

SignalR 及其内容应在页眉中引用。