繁体   English   中英

是否有在 Angular 5 项目中运行 signalR 的解决方案,或者是否有替代 signalR 来提供实时通知的方法?

[英]Is there a solution to run signalR in angular 5 project or is there an alternative to signalR to bild real time notification ??

我有一个 Angular 5 和 .Net 项目,我需要集成实时通知,解决方案是使用 signalR。 我用 .Net5 和angular 13测试了一个简单的 signalR 示例,它工作得很好。

现在这个错误发生在角度 5

ERROR in ./node_modules/@microsoft/signalr/dist/esm/Utils.js
Module parse failed: Unexpected token (101:19)
You may need an appropriate loader to handle this file type.
|     const response = await httpClient.post(url, {
|         content,
|         headers: { ...headers, ...options.headers },
|         responseType,
|         timeout: options.timeout,

我认为该问题与节点有关,但我被阻止并且没有找到解决方案。

这是我的角度服务

@Injectable()
export class SignalRService {

  private hubConnection: HubConnection;

  setPlayerName$ = new Subject<string>();
  private url = 'https://localhost:44375/hub/';

  constructor() { 
   this.buildConnection() ; 
   this.registerOnServerEvents() ; 
   this.startConnection()
  }


  private buildConnection(): HubConnection {
    return new HubConnectionBuilder()
      .withUrl(this.url, { accessTokenFactory: () => "eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY1NzE4OTQ4NywiaWF0IjoxNjU3MTg5NDg3fQ" })
      .withAutomaticReconnect()
      .build();
      
  }
  private registerOnServerEvents(): void {

    this.hubConnection.on('PlayerNameSet', (playerName: string) => {
      this.setPlayerName$.next(playerName);
    });
  }
  private startConnection(): void {
    if (this.hubConnection.state === HubConnectionState.Connected) {
      return;
    }

    this.hubConnection.start().then(
      () => {
        console.log('Hub connection started!');
      },
      error => console.error(error)
    );
  }
    setPlayerName(playerName: string): void {
    this.hubConnection.send('SetPlayerName', playerName);
  }
}

解决方案是使用与 angular5 兼容的较早版本的 signalR

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM