简体   繁体   English

如何在客户端处理SignalR服务器异常?

[英]How to handle SignalR server exception at client?

The error handler is added like this at client: 错误处理程序在客户端添加如下:

$.connection.hub.url = "/signalr";
$.connection.hub.logging = true;
$.connection.hub.error(function(error) {
    console.log('SignalrAdapter: ' + error);
});

$.connection.hub.start().done(function() { me.onStartDone(); });
// ...

At server it is: 在服务器上它是:

hubConfiguration.EnableDetailedErrors = true;

Accordingly to the docs this should be enough. 根据文档,这应该足够了。

At my exception throwing it just displays a log text for it and does not invoke the handler: 在我的异常中抛出它只显示它的日志文本而不调用处理程序:

[18:18:19 GMT+0000()] SignalR: ... [[[my error description here]]] ... 

At my cshtml page: 在我的cshtml页面:

<script src="~/Scripts/vendor/jquery.signalR-2.1.2.js"></script>
<script src="~/signalr/hubs"></script>

However if I attach an error handler to the method itself it is got called: 但是,如果我将错误处理程序附加到方法本身,则会调用它:

$.connection.operatorHub.server.myMethodName(someParam).fail(function(error) {
    console.log("Error handler called: " + error);
});

How to handle a general error? 如何处理一般错误?

UPDATE. UPDATE。 The answer is below. 答案如下。 Also see these: 另见这些:

Hope it helps someone. 希望它可以帮助某人。

I tested on a little chat project (downloaded here) it seems this method handle only connection errors. 我测试了一个小聊天项目(这里下载)似乎这个方法只处理连接错误。

$.connection.hub.error(function(error) {
    console.log('SignalrAdapter: ' + error);
});

I was able to handle all exceptions with the HubPipelineModule class. 我能够使用HubPipelineModule类处理所有异常。

1) I created a SOHubPipelineModule 1)我创建了一个SOHubPipelineModule

public class SOHubPipelineModule : HubPipelineModule
{
    protected override void OnIncomingError(ExceptionContext exceptionContext,
                                            IHubIncomingInvokerContext invokerContext)
    {
        dynamic caller = invokerContext.Hub.Clients.Caller;
        caller.ExceptionHandler(exceptionContext.Error.Message);
    }
}

2) I Added the module to GlobalHost.HubPipeline 2)我将模块添加到GlobalHost.HubPipeline

  // Any connection or hub wire up and configuration should go here
  GlobalHost.HubPipeline.AddModule(new SOHubPipelineModule());
  var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true };
  app.MapSignalR(hubConfiguration);

3) My ChatHub class : 3)我的ChatHub类:

 public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            throw new Exception("exception for Artyom");
            Clients.All.broadcastMessage(name, message);
        }

    }

4) In the js I use this code to get my exception message : 4)在js中我使用此代码来获取我的异常消息:

$.connection.chatHub.client.exceptionHandler = function (error) {
      console.log('SignalrAdapter: ' + error);
      alert('SignalrAdapter: ' + error);
};       

在此输入图像描述

I've used the error handling on the client like this: 我在客户端上使用了错误处理,如下所示:

It's for SignalR version 2 它适用于SignalR版本2

More detail: SignalR documentation - How to handle errors in the Hub class 更多细节: SignalR文档 - 如何处理Hub类中的错误

Hub - AppHub.cs: Hub - AppHub.cs:

public class AppHub : Hub
    {
        public void Send(string message)
        {
            throw new HubException("Unauthorized", new { status = "401" });
        }
    }

Client-side: 客户端:

$(function () {

            var appHub = $.connection.appHub;

            $.connection.hub.start().done(function () {

                appHub.server.send("test message")
                    .fail(function (e) {

                        if (e.source === 'HubException') {
                            console.error("Error message: ", e.message);
                            console.error("Error status: ", e.data.status);
                        }

                    });
            });

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM