【问题标题】:Uncaught (in promise): Cannot send data if the connection is not in the 'Connected' State. while connecting the signalR with angular未捕获(承诺中):如果连接未处于“已连接”状态,则无法发送数据。同时将信号R与角度连接
【发布时间】:2022-01-07 04:59:11
【问题描述】:

在使用角度连接 signalR 时出现错误

未捕获(承诺中):错误:如果连接未处于“已连接”状态,则无法发送数据。”

我的代码是

dashboard.service.ts

private hubConnection: signalR.HubConnection
initWebSocket() {
    this.hubConnection = new signalR.HubConnectionBuilder()
      //.configureLogging(signalR.LogLevel.Debug)
      .withUrl('http://localhost:5000/Notification',{
       skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets
      })
      .build();
  this.hubConnection
      .start()
      .then(() => console.log('Connection started'))
      //.then(() => console.log(this.hubConnection.connectionId)
      .catch(err => console.log('Error while starting connection: ' + err))

      const EmailId = "abc@mail.com";
      this.hubConnection.invoke("Registerd", EmailId);
      console.log("Registered", EmailId);

谁能帮我克服这个错误

【问题讨论】:

    标签: angular signalr signalr-hub asp.net-core-signalr


    【解决方案1】:

    您应该阅读 Javascript 承诺:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

    问题是您异步运行一些代码 (hubConnection.start()),然后不等待结果就尝试在尚未启动的连接上调用 hubConnection.invoke(...)

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 2021-10-09
      • 2020-01-21
      • 2020-05-16
      • 2019-06-26
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多