Sun*_*mar 4 signalr signalr-hub angular6 angular-cli-v6
我在 Angular6 中开发了一个项目。需要为实时聊天开发部分。为此,我在我的 asp.net 核心 Web Apiproject 中使用 SignalR。现在我想在我的 Angular 项目中使用这个 Web Api 参考。
我正在使用此链接。
但是在 App.Component.ts 中提供 Web Api url 时,我收到以下错误:
“HubConnection”类的构造函数是私有的,只能在类声明中访问。
应用组件.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.
Run Code Online (Sandbox Code Playgroud)
编辑:尝试以下代码:-
修改后的 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();
Run Code Online (Sandbox Code Playgroud)
错误 :
无法加载 http://localhost:1874/notify/negotiate:对预检请求的响应未通过访问控制检查:响应中“Access-Control-Allow-Credentials”标头的值为“”,必须是当请求的凭据模式为“包含”时为“真”。因此,不允许访问Origin ' http://localhost:4200 '。XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
他们更改了 SignalR 客户端的构造函数,HubConnection不再允许您在客户端构造函数中传递路由,并将构造HubConnection函数设为私有以强制更改。相反,您需要使用HubConnectionBuilder以下内容:
new HubConnectionBuilder().withUrl("/YOURROUTEHERE").build();
您需要按如下方式在标头中导入 HubConnectionBuilder:
import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';
如果有帮助,我还刚刚发布了我的 RxSignalR 示例的更新,适用于 @aspnet/signalr 1.0.2、RxJs 5.5.6 和 Angular 6。查看ReactiveChat 组件以获取示例。
| 归档时间: |
|
| 查看次数: |
3195 次 |
| 最近记录: |