【问题标题】:Could not connect Error: Error during negotiation request无法连接错误:协商请求期间出错
【发布时间】:2020-12-10 07:23:53
【问题描述】:

我的代码应该在 angular 8 asp.net web api 2 和 signalR 2 之间创建聊天通信 服务器 枢纽

 public class ChatHub : Hub
{
    public  string Send(string data="hello")
    {           
       return Clients.All.SendAsync(data);
    }
  }

启动

public void Configuration(IAppBuilder app)
        {
           
            app.MapSignalR();
        }

角度客户端

我得到了错误:

协商请求出错

【问题讨论】:

  • 非常感谢 Dima Kozhevin 的编辑工作

标签: angular asp.net-web-api signalr


【解决方案1】:

你可以尝试这样配置:

public void Configuration(IAppBuilder app)
{
    //Branch the pipeline here for requests that start with "/signalr"
    app.Map("/signalr", map =>
   {
       map.UseCors(CorsOptions.AllowAll);
       var hubConfiguration = new HubConfiguration { };
       map.RunSignalR(hubConfiguration);
   });
}
[HubName("ChatHub")]
public class ChatHub : Hub
{
    public  string Send(string data="hello")
    {           
       return Clients.All.SendAsync(data);
    }
}
//jQuery
declare var $: any;

//server configuration
export let CONFIGURATION = {
    baseUrls: {
        server: 'https://localhost:51962'
    },
}
private proxy: any;
private proxyName: string = 'ChatHub'; //defined in the hub
private configureConnection() {

        // create hub connection  
        this.connection = $.hubConnection(CONFIGURATION.baseUrls.server);

        // create new proxy as name already given in top  
        this.proxy = this.connection.createHubProxy(this.proxyName);
    }
// check in the browser console for either signalr connected or not  
    private startConnection(): void {
        this.connection.start().done((data: any) => {
            console.log('Now connected ' + data.transport.name + ', connection ID= ' + data.id);
            this.connectionEstablished.emit(true);
            this.connectionExists = true;

        }).fail((error: any) => {
            console.log('Could not connect ' + error);
            this.connectionEstablished.emit(false);
        });
    }

【讨论】:

  • 我试过你的提议,但不幸的是它不起作用,因为同样的错误。无论如何都非常感谢!
  • 它也没有工作,是否可能是因为客户端是用angular 8编写的?
  • 可能是你在角度方面缺少 jQuery。
  • 并安装:npm install — save @types/jquery npm install — save @types/signalr 还在 angular-cli.json “apps”中添加了脚本: [{ “scripts”: [ “../node_modules/jquery/dist/jquery.min.js” , “../node_modules/signalr/jquery.signalR.min.js” ], }]`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多