gpt4 book ai didi

SignalR 发送对象 - 新手

转载 作者:行者123 更新时间:2023-12-03 04:55:17 26 4
gpt4 key购买 nike

使用 Signal R,如果尝试发送对象,传递模型的语法是什么?

private async void FormLoaded(object sender, RoutedEventArgs e)
{
//Connection stuff...
Proxy.On("sendHello", () => OnSendDataConnection(ModelBuilder()));
}

private void OnSendDataConnection(ConnectionModel model)
{
Dispatcher.Invoke(DispatcherPriority.Normal,model?????)
this.Dispatcher.Invoke((Action)(LoadW));
}

最佳答案

查看问题(文本,而不是代码)我了解您正在尝试将复杂对象发送到 JS 端并在那里使用它?这是正确的吗?

在这种情况下,解决方案应该很简单:

来自ASP.NET.SignalR site :

You can specify a return type and parameters, including complex types and arrays, as you would in any C# method. Any data that you receive in parameters or return to the caller is communicated between the client and the server by using JSON, and SignalR handles the binding of complex objects and arrays of objects automatically.

C# 示例:

    public void SendMessage(string name, string message)
{
Clients.All.addContosoChatMessageToPage(new ContosoChatMessage() { UserName = name, Message = message });
}

JS:

    var contosoChatHubProxy = $.connection.contosoChatHub;
contosoChatHubProxy.client.addMessageToPage = function (message) {
console.log(message.UserName + ' ' + message.Message);
});

ContosoChatMessage 所在位置:

   public class ContosoChatMessage
{
public string UserName { get; set; }
public string Message { get; set; }
}

(继续阅读示例...)

所以基本上,在 JS 中,一旦收到“model”,您应该能够使用“model.XY”,其中 XY 是模型复杂对象的成员。

希望对您有所帮助。

关于SignalR 发送对象 - 新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30664487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com