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.
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.
In what problem?
I tried to create SignalR hub server and Angular client within same project from scratch, using same code to create hub server and configure SignalR.
And for Angular client, I find this package '@aspnet/signalr-client' has been deprecated, so I use '@aspnet/signalr' instead.
The project works well on my side without error, if possible, you can create new project or use '@aspnet/signalr' to build SignalR Angular client and check if it works for you.
Angular client
import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr';
import signalR = require('@aspnet/signalr');
@Component({
selector: 'app-signalrchatroom',
templateUrl: './signalrchatroom.component.html',
styleUrls: ['./signalrchatroom.component.css']
})
export class SignalrchatroomComponent implements OnInit {
constructor() { }
private _hubConnection: HubConnection;
nick = '';
message = '';
messages: string[] = [];
ngOnInit() {
this.nick = window.prompt('Your name:', 'John');
this._hubConnection = new signalR.HubConnectionBuilder()
.withUrl("/chat")
.build();
this._hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection :('));
this._hubConnection.on('sendToAll', (nick: string, receivedMessage: string) => {
const text = `${nick}: ${receivedMessage}`;
this.messages.push(text);
});
}
public sendMessage(): void {
this._hubConnection
.invoke('sendToAll', this.nick, this.message)
.catch(err => console.error(err));
}
}
Test Result