I am developing an Angular app, where i have to send data from angular forms to an external server using .netcore server and signalR. I am able to setup a connection between Angular client and controller using signalr hub but i am confused how to send json object from client to controller. From controller i want to send the Json object to external server. I want to implement this using signalr because i want realtime communication between external server and client. when i submit the data and recieve response from external server i want to alert client.I am looking for some guidance on how can i implement this.
Angular client
_hubConnection: HubConnection;
transportType = signalR.HttpTransportType.LongPolling;
ngOnInit() {
this._hubConnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Information)
.withUrl("http://localhost:5000/message", this.transportType)
.build();
}
fun1(){
this._hubConnection.on("Send", (user, message) => {
const received = `name: ${user}, message: ${message}`;
console.log(received);
});
this._hubConnection.start()
.then(() =>
this._hubConnection.invoke('Send', "rk", "wassup").catch(function (err) {
console.error(err.toString());
})
)
.then(() =>
console.log('connected')
)
.then(() => this._hubConnection.stop())
.then(() => console.log('Disconnected'))
.catch(e => console.error(e.message));
Controller
namespace ang.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DistController : Controller
{
private IHubContext<DistHub> _hubContext;
public DistController(IHubContext<DistHub> hubContext)
{
_hubContext = hubContext;
}
}
}
SignalRhub
namespace ang.HubConfig
{
public class DistHub: Hub
{
public async Task Send (string user , string message)
{
// return Clients.All.SendAsync("Send", message);
await Clients.All.SendAsync("Send", user, message);
}
}
}
startup.cs
app.UseSignalR(routes =>
{
routes.MapHub<DistHub>("/message");
});
json object
const Root = {
"Unit" : "mm",
"test": [{
"Val": this.Val1.isChecked,
'Val1' : this.val2.isChecked,
}],
"test1" :{
"sub":[{
'val2' : this.valFormGroup.get('val2').value,
'Val3' : this.valFormGroup.get('val3').value,
}]
},
}
