如何在客户端处理SignalR服务器异常? [英] How to handle SignalR server exception at client?

查看:328
本文介绍了如何在客户端处理SignalR服务器异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误处理程序被添加像这样在客户:

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;

因此​​,以<一个href=\"http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#handleerrors\"相对=nofollow> 这个文档应该够了。

在我的异常抛出它只是显示一个日志文本,并不会调用处理程序:

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]]] ... 

在我CSHTML页:

At my cshtml page:

<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?

推荐答案

我在一个小的聊天项目的(此处下载)看来这个方法只处理连接错误。

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);
});

我能够处理好与 HubPipelineModule 类的所有异常。

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),我增加了模块 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)我的 ChatHub 类:

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

    }

4)我用这个code,让我异常消息的JS:

4) In the js I use this code to get my exception message :

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

在这里输入的形象描述

这篇关于如何在客户端处理SignalR服务器异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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