尝试协商时出现SignalR 404错误

卢克夫

因此,我对SignalR还是很陌生,实际上我只使用了几天。无论如何,我的应用程序首次启动时出现以下错误:

SignalR协商错误

有关应用程序的代码位于两个项目中,即Web API和单页应用程序(SPA)。第一个有我的后端代码(C#),第二个有我的客户端代码(AngularJS)。我认为问题可能是由于所讨论的项目在不同的端口上运行。SignalR集线器所在的Web API在端口60161上,SPA在60813上。我的集线器声明如下:

public class ReportHub : Hub
{
    public void SendReportProgress(IList<ReportProgress> reportProgress)
    {                
        this.Clients.All.broadcastReportProgress(reportProgress);
    }

    public override Task OnConnected()
    {
        this.Clients.All.newConnection();

        return base.OnConnected();
    }
}

然后在我的Web API的Startup.cs文件中,初始化SignalR像这样:

public void Configuration(IAppBuilder app)
{           
    HttpConfiguration config = new HttpConfiguration();
    config.Services.Replace(typeof(IHttpControllerActivator), new NinjectFactory());
    config.MessageHandlers.Add(new MessageHandler());

    //set up OAuth and Cors
    this.ConfigureOAuth(app);
    config.EnableCors();
    config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

    // Setting up SignalR
    app.Map("/signalr", map =>
    {
        map.UseCors(CorsOptions.AllowAll);
        map.RunSignalR(new HubConfiguration { EnableJSONP = true });
    });

    //set up json formatters
    FormatterConfig.RegisterFormatters(config.Formatters);

    WebApiConfig.Register(config);

    app.UseWebApi(config);
}

对于我的客户端代码,我使用了一个称为angular-signalr-hub(Angular-signalr-hub的Angular SignalR API 客户端如下:

angular
.module("mainApp")
.factory("reportHubService", ["$rootScope", "Hub", reportHubService]);

/// The factory function
function reportHubService($rootScope, Hub) {
    var vm = this;
    vm.reportName = "None";

    // Setting up the SignalR hub
    var hub = new Hub("reportHub", {
        listeners: {
            'newConnection': function(id) {
                vm.reportName = "SignalR connected!";
                $rootScope.$apply();
            },
            'broadcastReportProgress': function (reportProgress) {
                vm.reportName = reportProgress.reportName;
                $rootScope.$apply();
            }
        },
        errorHandler: function(error) {

        },
        hubDisconnected: function () {
            if (hub.connection.lastError) {
                hub.connection.start();
            }
        },
        transport: 'webSockets',
        logging: true
        //rootPath: 'http://localhost:60161/signalr'
    });

昨天我做了一些谷歌搜索,后来想到的建议之一就是将SignalR URL设置为我的Web API之一(我在上面做了注释)。当我取消注释该行时,这似乎确实起作用,因为如果现在在浏览器中转到http:// localhost:60161 / signalr / hubs,它的确会向我显示动态生成的代理文件:

SignalR动态代理文件

当我运行我的应用程序时,我不再得到上面的错误,但是现在似乎没有连接。它到达协商行,并在此处停止:

签名日志

我认为它应该看起来像这样(这是从我发现的SignalR教程中得出的):

SignalR日志2

In addition, none of my listeners (declared in my Angular code above) get called, so something is still now working quite right. There should be more lines in the log to the effect that connection was successfully established, etc. What could be the problem here?

UPDATE: upon further debugging i found out the problem is most likely being caused by the ProtocolVersion property being different between the client and the result here:

在此处输入图片说明 在此处输入图片说明

Because of that it seems it just exists and fails to establish connection.

lukegf

I figured out what the problem was. My SignalR dependencies were out of date and because of that my client and server versions differed. All I had to do was update (via NuGet Package Manager) all SignalR dependencies to the latest version and now it works.

附带说明一下,SignalR不太擅长告诉我什么地方出了问题。实际上,没有显示错误消息,除非除了我已经拥有(打开)的日志记录,否则当然必须在其他地方找到或打开一些其他日志记录。无论哪种方式,它要么没有记录某些错误,要么使弄清楚如何打开所有日志记录变得困难。我必须去调试JQuery SignalR api才能找出问题所在,这是一项耗时的工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章