Exemple #1
0
 public CustObj FuncCustObj(int a, string b = "pepe", CustObj obj = default)
 {
     return(new CustObj()
     {
         a = obj?.a ?? a,
         b = new string[] { b, "obj_a: " + obj?.a },
         c = new object[] { a, b }
     });
 }
Exemple #2
0
        static async Task TestRpcClient()
        {
            var logger = LoggerConfig.DefaultLogger;

            var config = new MqttConfiguration("Mqtt");

            config.ClientId = "Test JsonRpcMqtt" + Guid.NewGuid();
            config.Username = "******";
            config.Password = "******";

            var mqtt = new MqttClientHelper(config);

            mqtt.ConnectAsync().Wait();

            var rpc = new Rpc(new JsonRpcMqttConnection(mqtt.Client, "test/", clientId: "TestRpcClient"));

            mqtt.OnConnectionChange += (s, connected) => { if (connected)
                                                           {
                                                               rpc.UpdateRegisteredMethods();
                                                           }
            };

            var service = rpc.GetServices <IExampleService>();

            while (mqtt.IsRunning)
            {
                var taskBroadcast  = rpc.CallAsync("SendBroadcastAsync", "hola mundo " + DateTime.Now);
                var taskBroadcast2 = service.SendBroadcastAsync("hola mundo " + DateTime.Now);
                logger.LogInformation("SendBroadcastAsync taskBroadcast:{0} {1}", taskBroadcast.Status, taskBroadcast2.Status);

                var res = service.Sumar(1, DateTime.Now.Second);
                logger.LogInformation("Sumar result:{0}", res);

                var t = service.TaskVoidAsync(1, DateTime.Now.Second);
                logger.LogInformation("TaskVoidAsync t.Status:{0}", t?.Status);
                t.Wait();
                logger.LogInformation("TaskVoidAsync t.Wait() t.Status:{0}", t?.Status);

                var t2 = service.TaskIntAsync(1, DateTime.Now.Second);
                logger.LogInformation("TaskIntAsync t2.Result:{0}", t2.Result);

                string rCall1 = service.Concatenar("ASDF", 1, 3);
                logger.LogInformation("GetCaller Concatenar Result: {0}", rCall1);

                CustObj resObj1 = service.FuncCustObj(1234, "hola FuncCustObj");
                logger.LogInformation("GetCaller FuncCustObj Result: {@0}", resObj1);

                resObj1 = service.FuncCustObj(1234, "hola FuncCustObj", new CustObj {
                    a = 23, b = new string[] { "sadf" }
                });
                logger.LogInformation("GetCaller FuncCustObj Result: {@0} {1}", resObj1, resObj1.c[0]);

                await Task.Delay(5000);
            }
        }
Exemple #3
0
 public string FuncDemo2(int a, string b = "pepe", CustObj obj = default)
 {
     return($"FuncDemo 2 ... A:{a} - B:{b} Obj:{JsonSerializer.Serialize(obj)}");
 }
Exemple #4
0
 public string FuncDemo(int a, string b = "pepe", CustObj obj = default)
 {
     return($"FuncDemo: a:{a} - b:{b} {JsonSerializer.Serialize(obj)}");
 }
Exemple #5
0
        static void TestRpcClient()
        {
            HttpClient client = new HttpClient();

            client.Timeout     = TimeSpan.FromSeconds(100);
            client.BaseAddress = new Uri("http://localhost:8081/");

            var rpcClient = new Rpc(new JsonRcpHttpClientConnection(client));

            var service = rpcClient.GetServices <IExampleService>();

            rpcClient.Dispatcher.RegisterFunction("NotifySumar", (message) =>
            {
                Console.WriteLine("************************ RECIBIO NotifySumar ***** message:{0} params:{1}",
                                  message.Serialize(), message.GetParameters <JsonElement>());

                var param = message.GetParameters <JsonElement[]>();
                var arg1  = param[0].GetInt32();
                var arg2  = param[1].GetInt32();
            });

            rpcClient.UpdateRegisteredMethods();

            try
            {
                Console.WriteLine("************************");
                service.TaskVoid(12, 2);
                var res31 = service.TaskVoidAsync(100, 200);
                var res32 = service.TaskIntAsync(100, 200);
                var res33 = service.TaskIntAsync(100, 200);
                var res34 = service.TaskIntAsync(100, 200);
                Console.WriteLine("************************");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            while (true)
            {
                try
                {
                    var res = rpcClient.Call <int>("Sumar", 12, 123);
                    Console.WriteLine("Sumar return:" + res);

                    var res2 = service.Sumar(10, 123);
                    Console.WriteLine("service Sumar return:" + res2);

                    string rCall1 = service.Concatenar("ASDF", 1, 3);
                    Console.WriteLine("GetCaller Concatenar Result: {0}", rCall1);

                    int resSuma1 = service.Sumar(1, 3);
                    Console.WriteLine("GetCaller Sumar Result: {0}", resSuma1);

                    CustObj resObj1 = service.FuncCustObj(1234, "hola FuncCustObj");
                    Console.WriteLine("GetCaller FuncCustObj Result: {0}", JsonSerializer.Serialize(resObj1));

                    resObj1 = service.FuncCustObj(1234, "hola FuncCustObj", new CustObj {
                        a = 23, b = new string[] { "sadf" }
                    });
                    Console.WriteLine("GetCaller FuncCustObj Result: {0}", JsonSerializer.Serialize(resObj1));
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception  :" + e);
                }

                Thread.Sleep(20000);
            }
        }
        public async Task RunAsync()
        {
            var config = new MqttConfiguration();

            config.ClientId = "Test JsonRpcMqtt" + Guid.NewGuid();
            config.Username = "******";
            config.Password = "******";

            var mqtt = new MqttClientHelper(config);

            // Rpc Ojbect
            var rpc = new Rpc(new JsonRpcMqttConnection(mqtt, "test/", clientId: "ExampleClient"));

            // RPC Register methods for dispach notifications/events from server
            rpc.Dispatcher.RegisterMethod(this, nameof(OnNotifyBroadcast));
            rpc.Dispatcher.RegisterMethod(this, nameof(OnNotifySum));

            // RPC: UpdateRegisteredMethods for subscribe and receive notifications
            mqtt.OnConnectionChange += (s, connected) =>
            {
                if (connected)
                {
                    rpc.UpdateRegisteredMethods();
                }
            };

            // Map interface with rpc for call services methods.
            var service = rpc.GetServices <IExampleService>();

            // Wait for connect
            mqtt.ConnectAsync().Wait();

            while (mqtt.IsRunning)
            {
                // Call async without service
                var taskBroadcast = rpc.CallAsync("SendBroadcastAsync", $"Mensaje_1 enviado desde el cliente {rpc.Connection.ClientId} " +
                                                  $"a todos lo clientes, Date:{DateTime.Now}");

                // Call with service
                var taskBroadcast2 = service.SendBroadcastAsync($"Mensaje_2 enviado desde el cliente {rpc.Connection.ClientId} " +
                                                                $"a todos lo clientes, Date:{DateTime.Now}");

                logger.LogInformation("SendBroadcastAsync task 1:{0} task 2:{1}", taskBroadcast.Status, taskBroadcast2.Status);

                var a   = 1;
                var b   = DateTime.Now.Second;
                var res = service.Sum(a, b);
                logger.LogInformation("Sum result:{0} expected:{1}", res, (a + b));

                var t = service.TaskVoidAsync(1, DateTime.Now.Second);
                logger.LogInformation("TaskVoidAsync t.Status:{0}", t?.Status);
                t.Wait();
                logger.LogInformation("TaskVoidAsync affter wait t.Status:{0}", t?.Status);

                var t2 = service.TaskFloatAsync(a, b);
                t2.Wait();
                logger.LogInformation("TaskFloatAsync result:{0} expected:{1} status:{2}", t2.Result, a + b + 1.2323, t2.Status);

                string rCall1 = service.Concat("ASDF", a, b);
                logger.LogInformation("Concat result:<{0}> expected:<{1}>", rCall1, $"Formated format:ASDF a:{a} b:{b}");

                CustObj resObj1 = service.FuncCustObj(1234, "hola FuncCustObj");
                logger.LogInformation("GetCaller FuncCustObj Result: {@0}", resObj1);

                resObj1 = service.FuncCustObj(1234, "hola FuncCustObj", new CustObj {
                    a = 23, b = new string[] { "sadf" }
                });
                logger.LogInformation("GetCaller FuncCustObj Result: {@0} {1}", resObj1, resObj1.c[0]);

                await Task.Delay(5000);
            }
        }