Skip to content

Encoders

SignalR Core can use different encodings to send and receive messages. Current supported encodings are JSon and MessagePack. Using these are not automatic, as most of these implementations require 3rd party plugins and/or changes to the server.

If possible use MessagePack. It's a binary protocol, and more efficient both memory and cpu wise than the text based Json!

JSon

For the plugin, the JSon encoder is available without any additional steps. The JsonProtocol class is a generic implementation that can work with different encoders that can use concreate JSON parsers. The one that comes with the plugin is the LitJsonEncoder.

var hub = new HubConnection(new Uri("https://server/hub"), new JsonProtocol(new LitJsonEncoder()));

How to Enable and Use Newtonsoft's JSON .NET For Unity Encoder

The plugin will detect its presence if installed through the Package Manager (directly or indirectly as a dependency of another package).

There's an encoder implementation that uses the Newtonsoft's JSON .NET For Unity package.

Steps to enable it and use it:

  1. Download and import the Newtonsoft's JSON .NET For Unity package.
  2. Add the BEST_SIGNALR_ENABLE_NEWTONSOFT_JSON_DOTNET_ENCODER define to the Scripting Define Symbols input under PlayerSettings/Other Settings:

    Scrypting Define SymbolsScrypting Define Symbols

  3. Use the now-available JsonDotNetEncoder class: !!! Example "var hub = new HubConnection(new Uri("https://server/hub"), new JsonProtocol(new JsonDotNetEncoder()));"

Event if newtonsoft-json is installed through the Package Manager and detected automatically, its usage isn't automatic, you have to use it as the parameter of the JsonProtocol constructor!

MessagePack-CSharp

The official SignalR Core server implementation uses MessagePack-CSharp to serialize/deserialize message on the server side.

To use the same package, here are the steps for Best SignalR:

  1. Follow the instructions to install MessagePack-CSharp.
  2. Locate the com.Tivadar.Best.SignalR.asmdef file. Depending how you installed the package, it can be under your /Assets/com.tivadar.best.signalr or /Packages/com.tivadar.best.signalr folders.
  3. Add MessagePack.asmdef (located in Scripts\MessagePack) to the list of Assembly Definition References and press Apply.
  4. Add the BEST_SIGNALR_ENABLE_MESSAGEPACK_CSHARP symbol to the Scripting Define Symbols input under PlayerSettings/Other Settings and press Apply.
  5. The MessagePackCSharpProtocol class is now available, it can be used to create a new HubConnection object.

    var hub = new HubConnection(new Uri("https://server/hub"), new MessagePackCSharpProtocol());

Carefully read through the Unity Support and AOT Code Generation topics to be able to properly set up MessagePack-CSharp!

GameDevWare MessagePack

By default the server has support for the JSon encoding but by adding new packages and a few lines of code the MessagePack encoding can be enabled too.

There's a MessagePack encoding implementation for the plugin, but it's disabled by default. To enable and use it, follow these steps:

  1. Download and import the Json & MessagePack Serialization package.
  2. Create a new Asembly Definition file under the Plugins\GameDevWare.Serialization folder.
  3. Locate the com.Tivadar.Best.SignalR.asmdef file. Depending how you installed the package, it can be under your /Assets/com.tivadar.best.signalr or /Packages/com.tivadar.best.signalr folders.
  4. Reference the newly create asmdef file and press Apply.
  5. Add the BEST_SIGNALR_ENABLE_GAMEDEVWARE_MESSAGEPACK define to the Scripting Define Symbols input under PlayerSettings/Other Settings.
  6. Use the now available MessagePackProtocol class:

    var hub = new HubConnection(new Uri("https://server/hub"), new MessagePackProtocol());

As you can see, the MessagePackProtocol uses only one class, there's no MessagePackEncoder as it's very specific and uses directly the Json & MessagePack Serialization classes.

The MessagePackProtocol class can be found in the com.Tivadar.Best.SignalR\Runtime\Encoders\ folder.

Comments