HubConnection中传递API

HubConnection中传递API

本文介绍了角度:无法在SignalR HubConnection中传递API url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular6中开发了一个项目。需要开发用于实时聊天的部分。为此,我在asp.net核心Web Apiproject中使用SignalR。现在,我想在Angular项目中使用此Web Api参考。

I have developed a project in Angular6. There is a requirement to develop section for live chat. For that I am using SignalR in my asp.net core Web Apiproject. Now I want to use this Web Api reference in my Angular project.

我正在使用链接。

但是在提供App.Component.ts中的Web Api URL,我得到以下错误:

But while providing the Web Api url in App.Component.ts, I am getting below error :

App Component.ts:

import { HubConnection } from '@aspnet/signalr';
import { Message } from 'primeng/api';

export class AppComponent implements OnInit {
  public _hubConnection: HubConnection;
  msgs: Message[] = [];
  constructor() {
  }
  ngOnInit() {
    this._hubConnection = new HubConnection('http://localhost:1874/notify'); // getting error on this line.

编辑:尝试以下代码:-

修改后的App.Component.ts:

Modified App.Component.ts :

import { HubConnection } from '@aspnet/signalr';
import * as signalR from '@aspnet/signalr';
import { Message } from 'primeng/api';

export class AppComponent implements OnInit {
  public _hubConnection: HubConnection;
  msgs: Message[] = [];
  constructor() {
  }
  ngOnInit() {
    this._hubConnection = new signalR.HubConnectionBuilder()
          .withUrl('http://localhost:1874/notify')
          .configureLogging(signalR.LogLevel.Information)
          .build();

错误:


推荐答案

他们将SignalR客户端 HubConnection 的构造函数更改为不再允许您通过客户端构造函数中的路由,并制成了 HubConnection 私有构造函数以强制执行更改。相反,您需要按如下方式使用 HubConnectionBuilder

They changed the constructors for SignalR Client HubConnection to no longer allow you to pass the route in the client constructor and made the HubConnection constructor private to enforce the change. Instead, you need to use the HubConnectionBuilder as follows:

新的HubConnectionBuilder()。 withUrl( / YOURROUTEHERE)。build();

您将需要在头文件中导入HubConnectionBuilder,如下所示:

You will want to import the HubConnectionBuilder in your header as follows:

import {HubConnection,HubConnectionBuilder} from'@ aspnet / signalr';

如果有帮助,我还刚刚发布了针对我的RxSignalR示例的更新,该示例适用于@ aspnet / signalr 1.0.2,RxJs 5.5.6和Angular6。请查看。

In case it helps, I also just posted an update to my RxSignalR samples for @aspnet/signalr 1.0.2, RxJs 5.5.6 and Angular 6. Check out the ReactiveChat component for an example.

这篇关于角度:无法在SignalR HubConnection中传递API url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:06