Notre avis
Guide pour implémenter des protocoles serveur (type requête-réponse ou événement) dans un jeu Unity, avec MessagePack.
Points forts
- Structure claire étape par étape pour les deux types de protocoles.
- Utilise MessagePack pour une sérialisation efficace.
- Inclut l'enregistrement dans le répertoire central et la création de tests.
- Sépare nettement les cas d'usage (requête vs événement).
Limites
- Spécifique au projet moorestech et à son architecture serveur.
- Suppose la connaissance de Unity, C#, MessagePack et du pattern Provider.
- N'explique pas la gestion des erreurs avancée ou les timeouts.
Quand vous devez ajouter une nouvelle communication client-serveur dans un projet Unity utilisant MessagePack.
Pour des communications inter-serveurs ou avec des protocoles standards comme HTTP/WebSocket.
Analyse de sécurité
SûrThe skill provides a guide for implementing server protocols in a specific codebase. It contains no destructive actions, no exfiltration, no obfuscation, and no unsafe operations. It is purely instructional.
Aucun point d'attention détecté
Exemples
Create a new request-response protocol called 'PlayerInventoryProtocol' with tag 'va:playerInventory'. The request should accept a playerId (int) and return a list of item IDs (int[]). Register it in PacketResponseCreator and write a test.Create an event packet called 'BlockBrokenEvent' with tag 'va:event:blockBroken'. It should be triggered when a block is broken and broadcast to all players with the position and block type.I need a new protocol for sending a chat message from client to server. Create a request-response protocol 'ChatProtocol' that takes a string message and returns a boolean success. Use the patterns from creating-server-protocol.name: creating-server-protocol description: | moorestechサーバーのプロトコル(Request-Response型・Event型)を作成するためのガイド。 Use when:
- 新しいサーバープロトコル(IPacketResponse)を実装する時
- 新しいイベントパケット(EventPacket)を実装する時
- 「プロトコルを作って」「パケットを追加して」と依頼された時
- クライアント-サーバー間の通信を新規追加する時
サーバープロトコル作成ガイド
プロトコル選択
| 条件 | 種類 | |------|------| | クライアントが明示的に情報を要求/操作を実行 | Request-Response型 | | サーバー側の状態変化をクライアントに通知 | Event型 |
詳細パターンとコード例: references/protocol-patterns.md を参照。
Request-Response型の作成手順
Step 1: プロトコルクラスを作成
moorestech_server/Assets/Scripts/Server.Protocol/PacketResponse/ に新規ファイルを作成。
using System;
using System.Collections.Generic;
using MessagePack;
using Server.Protocol;
namespace Server.Protocol.PacketResponse
{
public class YourProtocol : IPacketResponse
{
public const string ProtocolTag = "va:yourProtocol";
private readonly ISomeDependency _dependency;
public YourProtocol(ServiceProvider serviceProvider)
{
_dependency = serviceProvider.GetService<ISomeDependency>();
}
public ProtocolMessagePackBase GetResponse(byte[] payload)
{
var data = MessagePackSerializer.Deserialize<YourRequestMessagePack>(payload);
// ビジネスロジック
return new YourResponseMessagePack(/* result */);
}
#region MessagePack
[MessagePackObject]
public class YourRequestMessagePack : ProtocolMessagePackBase
{
// Key(0)=Tag, Key(1)=SequenceId は基底クラスで予約済み
[Key(2)] public int SomeField { get; set; }
[Obsolete("デシリアライズ用のコンストラクタです。基本的に使用しないでください。")]
public YourRequestMessagePack() { }
public YourRequestMessagePack(int someField)
{
Tag = ProtocolTag;
SomeField = someField;
}
}
[MessagePackObject]
public class YourResponseMessagePack : ProtocolMessagePackBase
{
[Key(2)] public string ResultData { get; set; }
[Obsolete("デシリアライズ用のコンストラクタです。基本的に使用しないでください。")]
public YourResponseMessagePack() { }
public YourResponseMessagePack(string resultData)
{
Tag = ProtocolTag;
ResultData = resultData;
}
}
#endregion
}
}
Step 2: PacketResponseCreatorに登録
moorestech_server/Assets/Scripts/Server.Protocol/PacketResponseCreator.cs のコンストラクタに追加:
_packetResponseDictionary.Add(YourProtocol.ProtocolTag, new YourProtocol(serviceProvider));
Step 3: テストを作成
/creating-server-tests スキルを使用してテストを作成。配置先: Tests/CombinedTest/Server/PacketTest/
Step 4: コンパイル確認
MCPツールまたはunity-test.shでコンパイルを確認。
Event型の作成手順
Step 1: イベントパケットクラスを作成
moorestech_server/Assets/Scripts/Server.Event/EventReceive/ に新規ファイルを作成。
using System;
using Game.Context;
using MessagePack;
using Server.Event;
namespace Server.Event.EventReceive
{
public class YourEventPacket
{
public const string EventTag = "va:event:yourEvent";
private readonly EventProtocolProvider _eventProtocolProvider;
public YourEventPacket(EventProtocolProvider eventProtocolProvider)
{
_eventProtocolProvider = eventProtocolProvider;
// ゲームイベントを購読(UniRx .Subscribe)
ServerContext.SomeEvent.OnSomething.Subscribe(OnSomething);
}
private void OnSomething(SomeEventData eventData)
{
var messagePack = new YourEventMessagePack(eventData.Value);
var payload = MessagePackSerializer.Serialize(messagePack);
// 全プレイヤー: AddBroadcastEvent / 特定プレイヤー: AddEvent(playerId, ...)
_eventProtocolProvider.AddBroadcastEvent(EventTag, payload);
}
#region MessagePack
[MessagePackObject]
public class YourEventMessagePack
{
// EventのMessagePackはProtocolMessagePackBaseを継承しない。Key(0)から開始
[Key(0)] public string Value { get; set; }
[Obsolete("デシリアライズ用のコンストラクタです。基本的に使用しないでください。")]
public YourEventMessagePack() { }
public YourEventMessagePack(string value)
{
Value = value;
}
}
#endregion
}
}
Step 2: イベントパケットを初期化
イベントを発火する責任を持つシステムのコンストラクタでインスタンス化し、フィールドに保持する(GC防止)。
例: ブロック配置イベント → BlockUpdateSystemで初期化、レールノード作成イベント → RailGraphDatastore関連で初期化。
Step 3: テストを作成
/creating-server-tests スキルを使用。配置先: Tests/CombinedTest/Server/PacketTest/Event/
Step 4: コンパイル確認
MCPツールまたはunity-test.shでコンパイルを確認。
注意事項
- タグの一意性を必ず確認する(既存タグと重複しないこと)
- MessagePackのKey番号: Request/ResponseはKey(2)から、EventデータはKey(0)から
[Obsolete]付き引数なしコンストラクタは省略不可- イベント購読はUniRxの
.Subscribe()を使用(/csharp-event-pattern参照) - コードのコメントは日本語・英語の2行セット
Expert Next.js App Router
Developpement
Un skill qui transforme Claude en expert Next.js App Router.
Générateur de README
Developpement
Crée des README.md professionnels et complets pour vos projets.
Rédacteur de Documentation API
Developpement
Génère de la documentation API complète au format OpenAPI/Swagger.