UDN
Search public documentation:

ReplicationPatternServerToClientRPCJP
English Translation
中国翻译
한국어

Interested in the Unreal Engine?
Visit the Unreal Technology site.

Looking for jobs and company info?
Check out the Epic games site.

Questions about support via UDN?
Contact the UDN Staff

UE3 ホーム > ネットワーク & レプリケーション > サーバーからクライアントへのリモートプロシージャコールのパターン

サーバーからクライアントへのリモートプロシージャコールのパターン


概要


サーバーからクライアントへのリモートプロシージャコールのパターンは、そのクライアントに何らかのロジックをクライアント上で実行させる場合に使用されます。データをクライアントにプッシュするだけであれば、 RepNotify のパターン などの変数レプリケーションのパターンを使用することによって可能です。これを使用するのは、入ってくるデータを使って何らかのロジックをクライアントに実行させる場合です。例としては、接続されているクライアントすべてに対してサーバーがメッセージを通達する場合が考えられます。

Player Controller (プレイヤーコントローラ) クラス


プレイヤーコントローラは、クライアントとサーバー間の通信ブリッジとして機能します。以下の例では、ReceiveBroadcast 関数とそのパラメータをサーバーがクライアントにレプリケートすることができます。

STCRPC_PlayerController.uc
class STCRPC_PlayerController extends PlayerController;


/**
 * Client function which receives a broadcast from the server
 *
 * Network: Client
 */
reliable client function ReceiveBroadcast(String ReceivedText)
{
  `Log(Self$":: Server sent me '"$ReceivedText$"'.");
}

defaultproperties
{
}

Game Info (ゲーム情報) クラス


GameInfo クラスはサーバー上にのみ存在します。このパターンでは、SetTimer 関数によって、毎秒 1 度 RandomBroadcast が実行されます。RandomBroadcast は、サーバー上にあるプレイヤーコントローラのインスタンス (WorldInfo 内に保存されている) のすべてをイタレートして、ReceiveBroadcast 関数とそのパラメータを、クライアントバージョンのプレイヤーコントローラにレプリケートします。

STCRPC_GameInfo.uc
class STCRPC_GameInfo extends GameInfo;

/**
 * Post Begin Play is executed after the GameInfo is created
 *
 * Network: Dedicated/Listen Server
 */
function PostBeginPlay()
{
  Super.PostBeginPlay();

  // Set the timer which broad casts a random message to all players
  SetTimer(1.f, true, 'RandomBroadcast');
}

/**
 * Broadcast a random message to all players
 *
 * Network: Dedicated/Listen Server
 */
function RandomBroadcast()
{
  local STCRPC_PlayerController PlayerController;
  local int i;
  local string BroadcastMessage;

  if (WorldInfo != None)
  {
    // Constuct a random text message
    for (i = 0; i < 32; ++i)
    {
      BroadcastMessage $= Chr(Rand(255));
    }

    ForEach WorldInfo.AllControllers(class'STCRPC_PlayerController', PlayerController)
    {
      `Log(Self$":: Sending "$PlayerController$" a broadcast message which is '"$BroadcastMessage$"'.");
      PlayerController.ReceiveBroadcast(BroadcastMessage);
    }
  }
}

defaultproperties
{
  PlayerControllerClass=class'STCRPC_PlayerController'
}

テスティング


クライアント上では、毎秒 (あるいはおよそ毎秒) ランダムな通達メッセージとともに ReceiveBroadcast が受信されます。 STCRPC_ClientConsoleWindow.jpg

サーバー上では、毎秒 (あるいはおよそ毎秒)、RandomBroadcast が呼び出され、サーバーに接続されているあらゆるプレイヤーコントローラ上にある ReceiveBroadcast が実行されます。 STCRPC_ServerConsoleWindow.jpg

ダウンロード


  • このパターンで使用されているソースコードは、 ここ からダウンロードすることができます。(ServerToClientRPCExample.zip)