Open18

MagicOnionのクライアントとしてUnityで使える grpc-dotnet が利用できるYetAnotherHttpHandlerを使う

toutou nodetoutou node

クライアントの準備

  • Unity2022.3.12f1でプロジェクトを作成
  • Cysharp/YetAnotherHttpHandler: YetAnotherHttpHandler brings the power of HTTP/2 (and gRPC) to Unity and .NET Standard.
    • Installationを進める
  • Cysharp/MagicOnion: Unified Realtime/API framework for .NET platform and Unity.
    • Support for Unity clientの項目を進める
      • gRPC library for Unity clientはスキップ
      • MagicOnion.Client.Unity.package (Unity asset package for MagicOnion library)
      • MessagePack for C#
  • Grpc.Coreの依存エラーがでる
    • Assets\Scripts\MagicOnion\MagicOnion.Unity\UnityDebugLogger.cs(2,17): error CS0234: The type or namespace name 'Logging' does not exist in the namespace 'Grpc.Core' (are you missing an assembly reference?)
  • Assets\Scripts\MagicOnion\MagicOnion.Unity\GrpcChannelProvider.cs(174,47): error CS0246: The type or namespace name 'GrpcCCoreGrpcChannelProvider' could not be found (are you missing a using directive or an assembly reference?)
  • Projectsettings -> Player -> Script Compilationで Scripting Define Synbolsから
    • USE_GRPC_NET_CLIENT_ONLYUSE_GRPC_NET_CLIENTを追加
toutou nodetoutou node

サーバーの準備

  • .NET7でプロジェクトを作る
  • dotnet add package MagicOnion.Server
toutou nodetoutou node

MessagePackとMagicOnionとyetanotherhttphandlerをUPM経由でPackageとしてインストール

補足

toutou nodetoutou node

MOCでコード生成

mpcだけじゃなくてmocのコード生成も必要
https://github.com/Cysharp/MagicOnion#moc-magiconion-codegen-command-line-tool

D:\UnityProjects\Fuuro\Fuuro\Assets>dotnet moc -i ../OniSync.Shared.csproj -o Scripts/Infra/OniSyncClient/Generated/Magi
cOnion.Generated.cs

一緒
https://zenn.dev/link/comments/c5fc1133c5c9cb

.NET Core 3.1 SDKじゃないとだめなんかな.....?


いれた

実行するSDKバージョンを変更する
使用する .NET のバージョンを選択する - .NET | Microsoft Learn

だめだった
そもそも最新のMagicOnionはdotnet6以上じゃないとダメ

toutou nodetoutou node

StremaingHubClientを作ろうとすると以下のエラー

わからん

NotSupportedException: Unable to get client factory for StreamingHub type 'OniSync.Shared.Interfaces.IPlayHub'.
MagicOnion.Client.StreamingHubClient.CreateClient[TStreamingHub,TReceiver] (Grpc.Core.CallInvoker callInvoker, TReceiver receiver, System.String host, Grpc.Core.CallOptions option, MagicOnion.Serialization.IMagicOnionSerializerProvider serializerProvider, MagicOnion.Client.IStreamingHubClientFactoryProvider factoryProvider, MagicOnion.Client.IMagicOnionClientLogger logger)

ここでgRPCチャンネルのオプションを適切に渡すか、適切なfactoryProviderを渡せば解決できそうな気がするが......

こんな感じで渡せば....?

関係あるか.........?

toutou nodetoutou node

ShareプロジェクトにUnityShimsが入ってなかたので入れた

toutou nodetoutou node

MagicOnion5.18 + MessgePack 2.5.124 + YetAnotherHttpHandler 0.10 + StremaingHubの実装で動きました。
IL2CPPビルドも成功してちゃんと動いた