UDN
Search public documentation:

ReplicationPatternClientToServerRPCCH
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

虚幻引擎3主页 > 网络&复制 > 客户端到服务器远程过程调用模式

客户端到服务器远程调用模式


概述


当您想发送客户端数据给服务器时,可以使用客户端到服务器远程调用模式。客户端数据本身不会自己把数据复制到服务器。这确保服务器维护控制游戏状态中的所有类变量。通过强制客户端使用客户端到服务器远程调用,服务器便总是有机会拦截所有正在来临的客户端数据。

Player Controllers(玩家控制器)类


玩家控制器是客户端和服务器之间的通信桥梁之一。每个玩家有他们自己的玩家控制器。这里仅是为了演示而是用了可靠通信。当远程调用和它的参数绝对需要到达服务器时应该使用可靠通信。如果由于带宽饱和导致远程调用和其参数丢失或丢掉没有关系时,那么应该使用不可靠通信。

CTSRPC_PlayerController.uc
class CTSRPC_PlayerController extends PlayerController;

/**
 * 使得客户端发送文本给服务器的可执行函数。
 *
 * Network: 客户端
 */
exec function SendTextToServer(String TextToSend)
{
  // 如果玩家控制器的角色不是权威的
  // 文本不为空
  if (Role < Role_Authority && TextToSend != "")
  {
    `Log(Self$":: Client wants to send '"$TextToSend$"' to the server.");
    ServerReceiveText(TextToSend);
  }
}

/**
 * Server function which receives text from the client
 *
 * Network: Dedicated/Listen Server
 */
reliable server function ServerReceiveText(String ReceivedText)
{
  // If the role of the player controller is authoritive
  // Text is not empty
  if (Role == Role_Authority && ReceivedText != "")
  {
    `Log(Self$":: Server received text, '"$ReceivedText$"'.");
  }
}

defaultproperties
{
}

Game Info(游戏信息)类


GameInfo类仅存在于服务器上。在这个模式中,它仅用于为连接的玩家生成正确的PlayerController。

CTSRPC_GameInfo.uc
class CTSRPC_GameInfo extends GameInfo;

defaultproperties
{
  PlayerControllerClass=class'CTSRPC_PlayerController'
}

测试


在客户端,执行SendTextToServer函数,并带有参数信息“My message to the server. Road house!"。

CTSRPC_ClientConsoleWindow.jpg

在服务器上,接收到了远程调用'ServerReceiveText'和参数"My message to the server. Road house!"。Road house!"。

CTSRPC_ServerConsoleWindow.jpg

下载


  • 下载这个模式中所使用的源码。(ClientToServerRPCExample.zip)