Aspnetcore.docs: Passing objects back to client

Created on 14 Mar 2019  ·  11Comments  ·  Source: dotnet/AspNetCore.Docs

You give an example of setting up a callback here:

hubConnection.on("Send", (message) -> {
System.out.println("New Message: " + message);
}, String.class);

On the SingalR core documentation, under api considerations it is mentioned that you should send back objects from your server instead of just a single property in case you need to add additional things in the future. https://docs.microsoft.com/en-us/aspnet/core/signalr/api-design?view=aspnetcore-2.2

I tried this but on the android client I can't figure out how to get it to properly populate the object.

I tried defining a object like this:
public class BotResponse
{
public String Item1;
public String Item2;
}

and setup the callback like:

this.hubConnection.on("BotMessage", (message) -> {
doSomething(message.Item1);
}, BotResponse.class);

But Item1 and Item2 are null in this implementation. What is the proper way to do this?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

SignalR Source - Docs.ms product-question

All 11 comments

Do you have a repo app on GitHub? Make sure the object that you are trying to deserialize into matches the result your expected from the invocation.
Here's a sample of invocations containing objects https://github.com/aspnet/SignalR-samples/blob/master/PullRequestR/android/app/src/main/java/com/example/pullrequestr/MainActivity.java

The invocation looked like:

await _hubContext.Clients.Client(connectionId).SendAsync("BotMessage", new
{
Item1 = "Item1Test",
Item2= "Item2Test"
});

Which is similar to the examples in the article I linked. I figured maybe it was because I dont have a type on the variables but it also wouldn't let me add a type there.

I don't think using an anonymous type is causing the issue. What version of the Java client are you using?
For context the object just gets serialized into a json object that gets sent to the client and the client tries to de-serialize that json object into an object of the type that you pass into the handler.
It would be helpful to see the app that you are seeing this behavior with

Sorry mikaelm12, I'm not permitted to share it.

"implementation 'com.microsoft.signalr:signalr:1.0.0'"

is what I have in Gradle.

I tried moving over to using a hard object with hard typed properties and still had no success.

Passing just a string works fine though, just having toruble with objects.

That's odd since we definitely do support this exact scenario.
Maybe something that can help you a bit, here is a test that should the formatting of an invocation that get's de-serialized into a custom type
https://github.com/aspnet/AspNetCore/blob/f303a55a8edf632738eb6e15312aac443cdf21b1/src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java#L1268-L1297
It's kind of hard to diagnose further without a repro 😄

I haven't seen an "AtomicReference" before, is that just something that is needed for testing?

Could you also do the Asserts in the On callback referencing param1 directly?

as in:

hubConnection.on("inc", (param1) -> {
assertEquals(1, param1.number);
assertEquals("A", param1.str);
}, Custom.class);

"AtomicReference" before, is that just something that is needed for testing?

Yup.

I've debugged the scenario as a sanity check and it seems fine. When your on handler gets invoked what is the shape of object that gets passed in to the lambda?

I can't tell what the json coming into the on method looks like. The message object exists, however both of it's properties are null.

I was hoping I could just change the On method to expect a string when I was passing back the object (for debugging) so I could see what it looked like in it's serialized form as you described above. However that callback never got invoked, so I assume it knows the server message is passing an object and only invokes the callbacks that are expecting objects.

I'm not doing anything interesting otherwise. Just trying to send a object with 2 strings and receive it on the android end.

In my case, I had to use the camelCase notation for the objects that I receive in the android application. It worked correctly:

gradle

implementation 'com.microsoft.signalr:signalr:1.0.0'

Android code

///Object I receive from hub:

public class LocationPush {
    public String userName;
    public Double latitude;
    public Double longitude;
}

///Connection and subscribe:

 hubConnection = HubConnectionBuilder.
                create(
                        Constant.URL_WEB_API +
                                Constant.ENDPOINT_LOCALIZATIONHUB).build();

        hubConnection.on("ReceiveLocation", (location) -> {

            Log.e(Constant.HUB_RECEIVE_LOCATION_TAG, location.latitude + " " + location.longitude);

        }, LocationPush.class);

///Asp.net core 3.0 code

//Object send

public class Location
 {
        public string UserName { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
  }
/// The hub

 public class LocalizationHub : Hub
    {
        public async Task SendLocation(Location location)
        {
            Console.WriteLine(string.Format("Recieve Latitude: {0} Longitude: {1} ", location.Latitude,location.Longitude));

            await Clients.All.SendAsync("ReceiveLocation", location);
        }
    }

hello guys! what about the reverse case. i mean i wanto to post an object to the server but it gets nothing.

TbChat chat = new TbChat(); //setting chat properties lke chat.CustomerName = "Customer Name"; chat.CustomerPhone = "5555555555"; hubConnection.send("requestChat", brandGUID, chat);

but it does not work!

我也遇到这个问题啊,你们赶紧解决啊

Was this page helpful?
0 / 5 - 0 ratings