diff --git a/aspnetcore/signalr/client-features.md b/aspnetcore/signalr/client-features.md index c32ffc546c2e..25607b95cf71 100644 --- a/aspnetcore/signalr/client-features.md +++ b/aspnetcore/signalr/client-features.md @@ -40,7 +40,7 @@ The table below shows the features and support for the clients that offer real-t | Server-Sent Events Transport |2.1.0|1.0.0|1.0.0|❌| | Long Polling Transport |2.1.0|1.0.0|1.0.0|3.0.0| | JSON Hub Protocol |2.1.0|1.0.0|1.0.0|1.0.0| -| MessagePack Hub Protocol |2.1.0|1.0.0|1.0.0|❌| +| MessagePack Hub Protocol |2.1.0|1.0.0|1.0.0|5.0.0| Support for enabling additional client features is tracked in [our issue tracker](https://github.com/dotnet/AspNetCore/issues). diff --git a/aspnetcore/signalr/java-client.md b/aspnetcore/signalr/java-client.md index 3268378307d0..c2ae3cc09ce6 100644 --- a/aspnetcore/signalr/java-client.md +++ b/aspnetcore/signalr/java-client.md @@ -91,12 +91,43 @@ HubConnection hubConnection = HubConnectionBuilder.create("YOUR HUB URL HERE") })).build(); ``` +::: moniker range=">= aspnetcore-5.0" + +### Passing Class information in Java + +When calling the `on`, `invoke`, or `stream` methods of `HubConnection` in the Java client, users should pass a `Type` object rather than a `Class` object to describe any generic `Object` passed to the method. A `Type` can be acquired using the provided `TypeReference` class. For example, using a custom generic class named `Foo`, the following code gets the `Type`: + +```java +Type fooType = new TypeReference>() { }).getType(); +``` + +For non-generics, such as primitives or other non-parameterized types like `String`, you can simply use the built-in `.class`. + +When calling one of these methods with one or more object types, use the generics syntax when invoking the method. For example, when registering an `on` handler for a method named `func`, which takes as arguments a String and a `Foo` object, use the following code to set an action to print the arguments: + +```java +hubConnection.>on("func", (param1, param2) ->{ + System.out.println(param1); + System.out.println(param2); +}, String.class, fooType); +``` + +This convention is necessary because we can not retrieve complete information about complex types with the `Object.getClass` method due to type erasure in Java. For example, calling `getClass` on an `ArrayList` would not return `Class>`, but rather `Class`, which does not give the deserializer enough information to correctly deserialize an incoming message. The same is true for custom objects. + +::: moniker-end + ## Known limitations -::: moniker range=">= aspnetcore-3.0" +::: moniker range=">= aspnetcore-5.0" + +* Transport fallback and the Server Sent Events transport aren't supported. + +::: moniker-end + +::: moniker range=">= aspnetcore-3.0 < aspnetcore-5.0" -* Only the JSON protocol is supported. * Transport fallback and the Server Sent Events transport aren't supported. +* Only the JSON protocol is supported. ::: moniker-end diff --git a/aspnetcore/signalr/java-client/sample/pom.xml b/aspnetcore/signalr/java-client/sample/pom.xml index 7994917233ea..325d87dbf6ad 100644 --- a/aspnetcore/signalr/java-client/sample/pom.xml +++ b/aspnetcore/signalr/java-client/sample/pom.xml @@ -28,6 +28,13 @@ 1.0.0 + + + com.microsoft.signalr.messagepack + signalr + 5.0.0 + + \ No newline at end of file diff --git a/aspnetcore/signalr/messagepackhubprotocol.md b/aspnetcore/signalr/messagepackhubprotocol.md index d721fcca2e40..9c80d56e9bce 100644 --- a/aspnetcore/signalr/messagepackhubprotocol.md +++ b/aspnetcore/signalr/messagepackhubprotocol.md @@ -104,6 +104,26 @@ const connection = new signalR.HubConnectionBuilder() > [!NOTE] > At this time, there are no configuration options for the MessagePack protocol on the JavaScript client. +### Java client + +To enable MessagePack with Java, install the `com.microsoft.signalr.messagepack` package. When using Gradle, add the following line to the `dependencies` section of the *build.gradle* file: + +```gradle +implementation 'com.microsoft.signalr.messagepack:signalr-messagepack:5.0.0' +``` + +When using Maven, add the following lines inside the `` element of the *pom.xml* file: + +[!code-xml[pom.xml dependency element messagePack](java-client/sample/pom.xml?name=snippet_dependencyElement_messagePack)] + +Call `withHubProtocol(new MessagePackHubProtocol())` on `HubConnectionBuilder`. + +```java +HubConnection messagePackConnection = HubConnectionBuilder.create("YOUR HUB URL HERE") + .withHubProtocol(new MessagePackHubProtocol()) + .build(); +``` + ## MessagePack quirks There are a few issues to be aware of when using the MessagePack Hub Protocol. @@ -172,6 +192,10 @@ InvalidDataException: Error binding arguments. Make sure that the types of the p For more information on this limitation, see GitHub issue [aspnet/SignalR#2937](https://github.com/aspnet/SignalR/issues/2937). +### Chars and Strings in Java + +In the java client, `char` objects will be serialized as one-character `String` objects. This is in contrast with the C# and JavaScript client, which serialize them as `short` objects. The MessagePack spec itself does not define behavior for `char` objects, so it is up to the library author to determine how to serialize them. The difference in behavior between our clients is a result of the libraries we used for our implementations. + ## Related resources * [Get Started](xref:tutorials/signalr)