协商请求期间出现Angular SignalR错误 [英] Angular SignalR Error during negotiation request

查看:361
本文介绍了协商请求期间出现Angular SignalR错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将SignalR与Angular 2和TypeScript一起使用.看来我的客户端代码无法正常工作,尤其是建立连接(没有生成的代理)

We are trying to use SignalR with Angular 2 and TypeScript. It seems my client side code is not working, especially Establish a connection (without the generated proxy)

我遵循了使用SignalR的Angular 2 TypeScript ,但是我没有包括SignalR/Hubs脚本我想要的没有自动生成的代理的路径,因为我看不到为我创建的任何自动生成的代理类.也被称为

I Followed Angular 2 TypeScript using SignalR but I didnt include SignalR/Hubs script path as I want to with out auto generated proxy as I don't see any auto generated proxy classes created for me. Also referred SignalR documentation written code using Javascript

我们使用Windows身份验证和Visual Studio 2015,Windows 7,IIS Express

We use windows authentication and Visual Studio 2015 , Windows 7, IIS Express

我收到此错误

协商请求期间出错.

Error during negotiation request.

集线器

public class ServerRefreshHub : Hub
{    
    public ServerRefreshHub()
    {

    }        

    public override Task OnConnected()
    {
     //It seems hub is not connected as client code fails
    }

    public static void NotifyUsers()
    {
    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ServerRefreshHub>();
    context.Clients.All.MessageFromServer("Hello");
    }
}

Asp.Net WebAPI启动

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Map(
                "/signalr",
                map =>
                {
                    var hubConfiguration = new HubConfiguration { 
                                    EnableDetailedErrors = true };
                    map.RunSignalR(hubConfiguration);
                });
   }
}

角度代码

declare var $: any;

@Injectable()
export class ServerNotifier {
    private connection: any;        
    private proxy: any;

    constructor() {

        this.connection = $.hubConnection();
        this.proxy = this.connection.createHubProxy('userCacheRefreshHub');
        this.proxy.on('MessageFromServer', (message: string) => 
                                  this.onMessageFromServer(message));

         this.connection.start().done((data: any) => {  
        console.log('Now connected ' + data.transport.name + ', connection ID= ' + data.id);  
         }).fail((error: any) => {  
         console.log('Could not connect ' + error);  
    });  
    }

    private onMessageFromServer(message: string) {

    }
}

Package.Json

"@types/jquery": "2.0.47",
"@types/signalr": "^2.2.35",
"signalr": "^2.2.2"

注意:

"@ types/jquery":"^ 3.2.16"将打字稿更新引入了最新版本,并且 我开始遇到更多问题,因此我仅针对打字将其降级 仅

"@types/jquery": "^3.2.16" introduces typescript update to latest and I started getting more issues, so I downgraded this just for typings only

在控制台中,我看到 200 对此请求的URL http://localhost:58965/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22serverrefreshhub%22%7D%5D&_=1509976299819

In Console I see 200 Response code for this requested URL http://localhost:58965/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22serverrefreshhub%22%7D%5D&_=1509976299819

推荐答案

SignalR不会为您生成代理类.

SignalR do not generate proxy classes for you.

您只需使用MapSignalR()配置SignalR即可:

You have just to configure SignalR using MapSignalR() :

    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();

        var settings = new JsonSerializerSettings();
        settings.ContractResolver = new SignalRContractResolver();
        var serializer = JsonSerializer.Create(settings);
        GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => serializer);

        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }

在集线器类上方添加HubName属性:

Add HubName attribute above your hub class :

[HubName("ServerRefreshHub")]
public class ServerRefreshHub : Hub

在声明hubConnection时不要忘记指定您的hubConnection URL,在创建代理时请不要忘记指定您的hubName.

Don't forget to specify your hubConnection url when declaring hubConnection and specify your hubName when creating your proxy.

this.connection = $.hubConnection();
this.connection.url = "http://localhost:58965/signalr";
this.proxy = this.connection.createHubProxy('ServerRefreshHub');

这篇关于协商请求期间出现Angular SignalR错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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