Using RPCs in Unity and AppWarp S2

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?

  1. Zone RPC
  2. 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

1
2
3
4
5
6
public class RPCZoneAdaptor extends BaseZoneAdaptor {    
    public int add(int a, int b)
    {
        return a+b;
    }
}

Or,

1
2
3
4
5
6
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.

1
2
3
4
5
6
7
8
9
10
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,

1
WarpClient.GetInstance().invokeZoneRPC(“add”,6,7);

or,

1
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.

1
2
3
4
5
6
7
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.

twitter Using RPCs in Unity and AppWarp S2facebook Using RPCs in Unity and AppWarp S2google Using RPCs in Unity and AppWarp S2linkedin Using RPCs in Unity and AppWarp S2pinterest Using RPCs in Unity and AppWarp S2reddit Using RPCs in Unity and AppWarp S2stumbleupon Using RPCs in Unity and AppWarp S2tumblr Using RPCs in Unity and AppWarp S2

Leave a Reply

Your email address will not be published. Required fields are marked *


nine − 4 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>