SignalR send object - newbie

巧了我就是萌 提交于 2020-01-14 10:21:51

问题


With Signal R, if trying to send an object, what is the syntax to pass the model?

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));
}

回答1:


Looking at the question (text, and not the code) I understand you are trying to send Complex object to JS side and use it there? Is that correct?

In that case the solution is supposed to be simple:

From the 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.

Example 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);
    });

Where ContosoChatMessage is:

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

(read on for examples...)

So basically, in JS once 'model' received, you should be able to use 'model.XY', where XY is a member of the model complex object.

I hope it helps.



来源:https://stackoverflow.com/questions/30664487/signalr-send-object-newbie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!