示例#1
0
        protected async override void OnApplicationReady()
        {
            base.OnApplicationReady();

            _transporter = ResonanceTransporter.Builder
                           .Create()
                           .WithUdpAdapter()
                           .WithServer(TcpAdapter.GetLocalIPAddress(), 9999)
                           .WithJsonTranscoding()
                           .Build();

            _transporter.ConnectionLost += (x, e) =>
            {
                e.FailTransporter = true;
                Logger.LogError($"Remote server has closed. {e.Exception.Message}");
            };

            _client = _transporter.CreateClientProxy <IRemoteDrawingBoardService>();
            _client.RectangleAdded += _client_RectangleAdded;

            try
            {
                await _transporter.ConnectAsync();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Could not connect to the remote server. Please restart the application.");
                return;
            }

            //This is just for testing async methods...
            String welcomeMessage = await _client.GetWelcomeMessage("Roy", 99);

            int countAsync = await _client.GetRectanglesCountAsync();

            int sum = await _client.CalcAsync(10, 15);

            try
            {
                foreach (var rect in _client.Rectangles)
                {
                    Rectangles.Add(rect);
                }
            }
            catch (Exception ex)
            {
                //TODO: Add log here that we could not fetch any server logs or something.
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting Resonance UDP Broadcast Demo...");

            Console.WriteLine("Connecting transporter with UDP adapter on all addresses...");

            //Initialize a transporter with UDP adapter. Listening and sending to all interfaces.
            IResonanceTransporter transporter = ResonanceTransporter.Builder
                                                .Create()
                                                .WithUdpAdapter()
                                                .Broadcast(1234)
                                                .PreventLoopback()
                                                .WithJsonTranscoding()
                                                .Build();

            //Connect the transporter.
            transporter.Connect();

            //Register the service to handle incoming messages from other clients.
            transporter.RegisterService <IRPCBroadcastService, RPCBroadcastService>(RpcServiceCreationType.Singleton);

            //Create a service client proxy to send messages to other clients.
            IRPCBroadcastService client = transporter.CreateClientProxy <IRPCBroadcastService>();

            Console.WriteLine("Now connected. Make sure at least one more client is also connected before sending messages.");
            Console.WriteLine();

            String input = String.Empty;

            while (input.ToLower() != "exit")
            {
                Console.Write("Enter a message to broadcast: ");
                input = Console.ReadLine();

                try
                {
                    client.BroadcastMessage(input);
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex.Message);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                Console.WriteLine();
            }
        }