Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR send object - newbie

Tags:

signalr

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));
}
like image 456
D-W Avatar asked Jun 05 '15 10:06

D-W


1 Answers

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.

like image 158
DDan Avatar answered Sep 16 '22 15:09

DDan