Note : The following article describes RPCs in context of AppWarp
What is RPC?
RPC(Remote Procedure Call) is a mechanism available in AppWarp S2 which enables developers to call methods that are defined on server directly from the clients. Hence, using RPCs developers can extend the server side by implementing his own methods.
Types of RPCs?
- Zone RPC
- Room RPC
Zone RPCs are the methods defined in zone adaptor. They are invoked on client side by calling invokeZoneRPC() method whereas Room RPCs are defined in room adaptor and are called through invokeRoomRPC() method on client side.
Why you need RPC?
RPC is a great feature, when it comes to extending the server side code. We provide SDK for all major platforms and try to keep the SDK generic so that it can be adopted in various genre of games. But you may need some specific APIs for your game. You can then define RPCs and extend the APIs. Simply create the methods that you want to perform on server side and invoke them from clients.
For example, we provide getOnlineUsers() to get the list of all online users, but what if you want to get all the users who have been playing for past 1 hour. To do so, you can start counting time when a user joins the game and write a method that returns all such players. You may think of sending this information as chat messages, but then all players will receive that message.
Similar activities like picking a weapon, shooting, etc. can be implemented through RPCs.
Using RPCs in Unity
To use RPCs first you need to define your methods on server side and then you can call them from clients. Your methods can be defined either in zone adaptor or room adaptor. Remember to set their access specifier as public.
Suppose you want to define a method that adds two numbers and returns their sum.
Server Side
public class RPCZoneAdaptor extends BaseZoneAdaptor {
public int add(int a, int b)
{
return a+b;
}
}
Or,
public class RPCRoomAdaptor extends BaseRoomAdaptor{
public int add(int a, int b)
{
return a+b;
}
}
Client Side
On Client Side, first you will need to define onInvokeZoneRPCDone in zone listener and onInvokeRoomRPCDone in room request listener.
public class Listener : ZoneRequestListener, RoomRequestListener
{
public void onInvokeZoneRPCDone(RPCEvent eventObj)
{
}
public void onInvokeRoomRPCDone(RPCEvent eventObj)
{
}
}
After this simply call the method invokeZoneRPC for invoking the method defined in zone adaptor or invokeRoomRPC for invoking the method defined in room.
For example,
WarpClient.GetInstance().invokeZoneRPC(“add”,6,7);
or,
WarpClient.GetInstance().invokeRoomRPC(“9180385”,“add”,6,7)
Now, you can handle the response in listeners. RPCEvent will contain the return value along with function name, zone key and roomID in case of room RPC.
public void onInvokeRoomRPCDone(RPCEvent eventObj)
{
string ret = eventObj.getReturn();
string func = eventObj.getFunction();
string zone = eventObj.getAppKey();
string room = eventObj.getRoomId();
}
Conclusion
To learn more about AppWarp S2, please visit our dev center here.
Leave A Reply