我如何使用Signalr将JSON对象发送到.net服务器 [英] How can i send a json object to .net server using signalr
问题描述
我正在开发Angular应用程序,在这里我必须使用.netcore服务器和signalR将数据从角度形式发送到外部服务器.我能够使用Signalr集线器在Angular客户端和控制器之间建立连接,但是我很困惑如何从客户端向控制器发送json对象.从控制器,我想将Json对象发送到外部服务器.我想使用Signalr来实现这一点,因为我想在外部服务器和客户端之间进行实时通信.当我提交数据并接收来自外部服务器的响应时,我想向客户端发出警报.我正在寻找一些有关如何实现此目标的指导.
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客户
_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));
控制器
namespace ang.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DistController : Controller
{
private IHubContext<DistHub> _hubContext;
public DistController(IHubContext<DistHub> hubContext)
{
_hubContext = hubContext;
}
}
}
SignalRhub
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
startup.cs
app.UseSignalR(routes =>
{
routes.MapHub<DistHub>("/message");
});
json对象
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,
}]
},
}
推荐答案
您可以通过将根数据传递给调用来做到这一点
You can do like this by passing in the root data to the invoke
connection.invoke('Send', "rk", "wassup", Root);
然后在中心中为根数据添加另一个参数
Then in your hub add another params for the root data
public async Task Send (string user , string message, string root)
{
await Clients.All.SendAsync("Send", user, message);
}
由于您提交了json字符串,因此可以更新
Update since you are submit json string you can use
JsonConvert.DeserializeObject<List<object>>(root);
这篇关于我如何使用Signalr将JSON对象发送到.net服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!