github Cysharp/MagicOnion 4.2.0
Ver.4.2.0

latest releases: 7.0.1, 7.0.0, 7.0.0-rc4...
3 years ago

New Features

Unity: Introduce gRPC channel management integration #414

Wraps gRPC channels and provides a mechanism to manage them with Unity's lifecycle.
This prevents your application and the Unity Editor from freezing by releasing channels and StreamingHub in one place.

The editor extension also provides the ability to display the communication status of channels.

Screen08

NOTE: The data rate is calculated only for the message body of methods, and does not include Headers, Trailers, or Keep-alive pings.

New APIs

MagicOnion.GrpcChannelx class

  • GrpcChannelx.FromTarget(GrpcChannelTarget) method
  • GrpcChannelx.FromAddress(Uri) method

MagicOnion.Unity.GrpcChannelProviderHost class

  • GrpcChannelProviderHost.Initialize(IGrpcChannelProvider) method

MagicOnion.Unity.IGrpcChannelProvider interface

  • DefaultGrpcChannelProvider class
  • LoggingGrpcChannelProvider class

Usages

1. Prepare to use GrpcChannelx in your Unity project.

Before creating a channel in your application, you need to initialize the provider host to be managed.

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void OnRuntimeInitialize()
{
    // Initialize gRPC channel provider when the application is loaded.
    GrpcChannelProviderHost.Initialize(new DefaultGrpcChannelProvider(new []
    {
        // send keepalive ping every 5 second, default is 2 hours
        new ChannelOption("grpc.keepalive_time_ms", 5000),
        // keepalive ping time out after 5 seconds, default is 20 seconds
        new ChannelOption("grpc.keepalive_timeout_ms", 5 * 1000),
    }));
}

GrpcChannelProviderHost will be created as DontDestroyOnLoad and keeps existing while the application is running. DO NOT destory it.

image

2. Use GrpcChannelx.FromTarget or GrpcChannelx.FromAddress to create a channel.

Use GrpcChannelx.FromTarget or GrpcChannelx.FromAddress to create a channel instead of new Channel(...).

var channel = GrpcChannelx.FromTarget(new GrpcChannelTarget("localhost", 12345, ChannelCredentials.Insecure));
// or
var channel = GrpcChannelx.FromAddress(new Uri("http://localhost:12345"));

3. Use the channel instead of Grpc.Core.Channel.

var channel = GrpcChannelx.FromAddress(new Uri("http://localhost:12345"));

var serviceClient = MagicOnionClient.Create<IGreeterService>(channel);
var hubClient = StreamingHubClient.ConnectAsync<IGreeterHub, IGreeterHubReceiver>(channel, this);

Extensions for Unity Editor (Editor Window & Inspector)

image

Improvements

  • Improve StreamingHub's grouping writer #416

Fixes

  • Fix handling of Generics for StreamingHub code generation #419

Don't miss a new MagicOnion release

NewReleases is sending notifications on new releases.