Standard requests

In order to send remote request to the server you need to create own class that derives from the RemoteMethodRequestHandler and implement methods:
  • GetMethodName()
    • name of remote method
  • PrepareParams
    • put all necessary parameters to MethodParams dictionary
    • not needed method don't send parameters
  • ParseResponse
    • This method should parse MethodResponse into desired type

Additionaly you can implement own method that will prepare all the necessary parameters, send request to the server uging Send() and return parsed response

Example

Here's example of remote sum method, that is adding 2 integers together.

RequestHandler:

using System;
using System.Threading.Tasks;
using JsonRpcRT;
using JsonRpcRT.RemoteMethodsHandling;
using Newtonsoft.Json.Linq;

namespace XamlGame1.RemoteMethods.Client
{
    internal class SumHandler : RemoteMethodRequestHandler
    {
        public SumHandler(JsonRpcController networkManager) : base(networkManager)
        {
        }

        public override string GetMethodName()
        {
            return "sum";
        }

        public void PrepareParams(int a, int b)
        {
            MethodParams["a"] = a;
            MethodParams["b"] = b;
        }

        public override void ParseResponse(MethodResponse response)
        {
            try
            {
                if (response.Type == ResponseType.NormalResponse)
                {
                    Result = ((JValue)response.Content).ToObject<int>();
                }
            }
            catch (Exception ex)
            {
                Log(String.Format("Sum error: {0}", ex.Message));
            }
        }

        public async Task<int?> CalculateSum(int a, int b)
        {
            PrepareParams(a, b);
            ParseResponse(await Send());

            return Result as int?;
        }
    }
}


Usage:

var remoteSumMethodHandler = new SumHandler(networkManager);
long? sum = await remoteSumMethodHandler.CalculateSum(2, 3);
Log(String.Format("Response form sum {0}", sum)); // prints 5

Notifications

Notifications are similar to the standard method requests: you need to create notification handler that derives from RemoteMethodNotificationHandler and implements the same methods as standard request, besides ParseMessage (notifications don't recieve responses)

Example

Here's sample ping notification

using System;
using System.Threading.Tasks;
using JsonRpcRT;
using JsonRpcRT.RemoteMethodsHandling;

namespace XamlGame1.RemoteMethods.Client
{
    public class PingNotificationHandler : RemoteMethodNotificationHandler
    {
        public PingNotificationHandler(JsonRpcController networkManager)
            : base(networkManager)
        {
        }

        public override string GetMethodName()
        {
            return "Ping";
        }

        public async Task<bool> SendPing()
        {
            try
            {
                return await Send();
            }
            catch (Exception ex)
            {
                Log(String.Format("Ping Notification error: {0}", ex.Message));
            }

            return false;
        }
    }
}

Usage:

var pingNotificationMethodHandler = new PingNotificationHandler(networkManager);
bool pingSuccessfull = await pingNotificationMethodHandler.SendPing();
Log(String.Format("Ping notification successful: {0}", pingSuccessfull));

Batch requests

In order to send batched requests you need to create Collection of Method/Notification handlers and pass them to SendBatchRequestAsync method from JsonRpcController

If methods use parameters, prepare them by using PrepareParams methods. Results will be stored in Result handlers properties.

Example

Performing multiple method requests in batch (using SumHandler from Standard requests)

var col = new Collection<SumHandler>();
const int testsCount = 10;
for (var i = 0; i < testsCount; i++)
{
	var batchSum = new SumHandler(networkManager);
	batchSum.PrepareParams(random.Next(1000), random.Next(1000));
	col.Add(batchSum);
}
			
await networkManager.SendBatchRequestAsync(col);
						
foreach (var sumHandler in col)
{
	Log(sumHandler.Result.ToString());
}

Last edited Dec 3, 2012 at 9:30 PM by axadiw, version 9

Comments

No comments yet.