Angular 6 SignalR在启动连接时给出“ WebSocket不处于OPEN状态”

时间:2019-02-19 14:28:47

标签: angular signalr azure-signalr

我已经在Angular 6项目中安装了@ aspnet / signalr

我尝试了遍历堆栈流中发现的所有其他选项来解决信号器问题。 以下是我的服务代码:

import { Injectable } from '@angular/core';
import * as signalR from "@aspnet/signalr";
import { UserService } from 'app/@core/data/users.service';

@Injectable()
export class SignalRService {

  private url = baseurl+'&users='
  public data;

  private hubConnection: signalR.HubConnection;

  constructor(private user: UserService) { }

  public startConnection = () => {
    let loggedInuser = this.user.getUserEmail();
    let accessToken ="some token";
    var options = {
      transport: signalR.HttpTransportType.WebSockets,
      logging: signalR.LogLevel.Trace,
    accessTokenFactory: () => accessToken
  };

    this.hubConnection = new signalR.HubConnectionBuilder()
                            .withUrl(this.url + loggedInuser , options)
                            .build();
    this.hubConnection.serverTimeoutInMilliseconds = 9999999999999;
    this.hubConnection
      .start()
      .then(() => console.log('Connection started'))
      .catch(err => {
        console.log('url: ' + this.url + loggedInuser)
        console.log('Error while starting connection: ', err)
      })
  }

  public addTransferChartDataListener = () => {
    this.hubConnection.on('AppNotifications', (data) => {
      this.data = data;
      console.log(data);
    });
  }
}

当我运行通知组件的ngOninit()时,在控制台中收到“ Websocket未处于OPEN状态”消息。 我在哪里做错了?

我执行该示例的示例是https://code-maze.com/netcore-signalr-angular/

1 个答案:

答案 0 :(得分:0)

将传输方式从“ WebSockets”更改为“ ServerSentEvents”,并将hubConnection.on移到hubConnection.start()的前面 所以,这是我在工作状态下的最终代码

protected override void OnAttached()
{
    AssociatedObject.ManipulationMode = ManipulationModes.TranslateX;
    AssociatedObject.ManipulationCompleted += AssociatedObjectManipulationCompleted;
}