Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aspnetcore/signalr/client-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
35 changes: 33 additions & 2 deletions aspnetcore/signalr/java-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`, the following code gets the `Type`:

```java
Type fooType = new TypeReference<Foo<String>>() { }).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<String>` object, use the following code to set an action to print the arguments:

```java
hubConnection.<String, Foo<String>>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<String>` would not return `Class<ArrayList<String>>`, but rather `Class<ArrayList>`, 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

Expand Down
7 changes: 7 additions & 0 deletions aspnetcore/signalr/java-client/sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
<version>1.0.0</version>
</dependency>
<!-- </snippet_dependencyElement> -->
<!-- <snippet_dependencyElement_messagePack> -->
<dependency>
<groupId>com.microsoft.signalr.messagepack</groupId>
<artifactId>signalr</artifactId>
<version>5.0.0</version>
</dependency>
<!-- </snippet_dependencyElement_messagePack> -->
</dependencies>

</project>
24 changes: 24 additions & 0 deletions aspnetcore/signalr/messagepackhubprotocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<dependencies>` 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.
Expand Down Expand Up @@ -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)
Expand Down