Chat with SignalR | Angular 8 | ASP.NET-Core 2.1

I try to repeat example with chat. Such here . But as I undesrtand I have a problem with adress of my host. This is what I have when my component started.

enter image description here

As you can see I have the twice adress. I tried only /chat but the same result

The app I start in VS. I dont start as a separate client part and api-becked.

This is web-adress of my app where I start my component with chat. http://localhost:59955/home/message

My code

public class ChatHub : Hub
{
public async Task Send(string name, string message)
{
    await this.Clients.All.SendAsync("sendToAll", name, message);
}
}

Start-up and what I added.

services.AddSignalR();
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
    {
        builder
        .AllowAnyMethod()
        .AllowAnyHeader()
        .WithOrigins("http://localhost:4200");
    }));

And I method public void Configure(IApplicationBuilder app, IHostingEnvironment env)

app.UseSignalR(routes =>
    {
        routes.MapHub<Chat>("/chat");
    });

Now the client

export class ChatComponent implements OnInit {
// tslint:disable-next-line:variable-name
private _hubConnection: HubConnection;
nick = '';
message = '';
messages: string[] = [];

constructor() { }

ngOnInit() {
this.nick = window.prompt('Your name:', 'John');

this._hubConnection = new 
 HubConnectionBuilder().withUrl('http://localhost:59955/chat').build();

 this._hubConnection
  .start()
  .then(() => console.log('Connection started!'))
  .catch(err => console.log('Error while establishing connection :('));
 }


public sendMessage(): void {
 this._hubConnection.on('sendToAll', (nick: string, receivedMessage: string) 
 => {
  const text = `${nick}: ${receivedMessage}`;
  this.messages.push(text);
 });
 this._hubConnection
  .invoke('sendToAll', this.nick, this.message)
  .catch(err => console.error(err));
}
}

There is launchSettings json file.

enter image description here

In what problem?



Read more here: https://stackoverflow.com/questions/58358782/chat-with-signalr-angular-8-asp-net-core-2-1

Content Attribution

This content was originally published by FX_Sektor at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: