Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

.NET Core 3.1 and Angular 6 SignalR setup connects but not receiving messages

I am experiencing some issues with configuring my project to use SignalR. The connection gets established from my Client to the Hub, and I can see my logs in Seq indicating that the messages are being sent, but I am not receiving any messages in the Client. I have struggled with this for a long time, and I am not sure what exactly I have done wrong?

I have my Hub class:

public class SourceControlHub : Hub
{

}

In Startup I add signalR service:

services.AddSignalR();

And map the hub:

app.UseEndpoints(endpoints =>
{
    endpoints.MapHealthChecks("/health");
    endpoints.MapSwagger();
    endpoints.MapControllers();
    endpoints.MapHub<SourceControlHub>("/sourceControlHub");
});

I send my message like this:

await _hub.Clients.All.SendAsync("SendProgressReportMessage", snapshotId, uploadCount, uploaded);

I connect to the hub connection like this:

import { Injectable } from '@angular/core';
import { environment } from '../../../environments/environment';
import * as signalR from '@microsoft/signalr';

@Injectable({
  providedIn: 'root'
})
export class SignalRService {
  SIGNALR_URL = environment.signalRBaseUrl;

  public hubConnection: signalR.HubConnection;
  public startConnection = () => {
    this.hubConnection = new signalR.HubConnectionBuilder()
      .withUrl(this.SIGNALR_URL + '/sourceControlHub')
      .build();

    this.hubConnection
      .start()
      .then(() => console.log('Connection started'))
      .catch(err => console.log('Error while starting connection: ', err));
  };

  public addProgressReportingListener() {
    this.hubConnection.on(
      'SendProgressReportMessage',
      (snapshotId, toUpload, uploaded) => {
        console.log(
          'reporting the data: ' +
            snapshotId +
            ' ' +
            uploaded +
            ' of ' +
            toUpload
        );
      }
    );
  }
}

And initiate it in my component:

    this.signalRService.startConnection();
    this.signalRService.addProgressReportingListener();

I tried with an older version of @aspnet/signalr which didn't work either. I am currently using latest version of @microsoft/signalr in the Angular project.

Edit: Moved the last paragraph, where I explained the issue, to the top of the post.

question from:https://stackoverflow.com/questions/65953127/net-core-3-1-and-angular-6-signalr-setup-connects-but-not-receiving-messages

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...