Пример #1
0
        static async Task Main1(string[] args)
        {
            //Create a new Client.
            var client = new RSocketClient(
                new WebSocketTransport("ws://localhost:9092/"));
            //	new SocketTransport("tcp://localhost:9091/"));

            //Bind a Service to this Client.
            var service = new EchoService.EchoServiceClient(client);

            //Connect to a Server and establish communications.
            await client.ConnectAsync();

            //Make a service method call with no return to the server.
            await service.FireAndForget(Value.ForString($"{nameof(service.FireAndForget)}: Calling service..."));

            //Make a service method call returning a single value.
            var result = await service.RequestResponse(Value.ForString($"{nameof(service.RequestResponse)}: Calling service..."));

            Console.WriteLine($"Sample Result: {result.StringValue}");


            //C# 8.0: Change the target language version to see IAsyncEnumerable iteration using built-in language constructs. (Project -> Properties -> Build -> Advanced -> Language Version)
#if CSHARP8
            //Make a service call and asynchronously iterate the returning values.
            var stream = service.RequestStream(Value.ForString($"{nameof(service.RequestStream)}: Calling service..."));
            await foreach (var value in stream)
            {
                Console.WriteLine($"Stream Result: {value.StringValue}");
            }
            Console.WriteLine("Stream Done");


            //Make a service call taking asynchronous values and asynchronously iterate the returning values.
            var channel = service.RequestChannel(GenerateValues());
            await foreach (var value in channel)
            {
                Console.WriteLine($"Channel Result: {value.StringValue}");
            }
            Console.WriteLine("Channel Done");
#else
            //Make a service call and asynchronously iterate the returning values.
            var stream = service.RequestStream(Value.ForString($"{nameof(service.RequestStream)}: Calling service..."));
            await ForEach(stream, value => Console.WriteLine($"Stream Result: {value.StringValue}"), () => Console.WriteLine("Stream Done"));


            //Make a service call taking asynchronous values and asynchronously iterate the returning values.
            var channel = service.RequestChannel(GenerateValues());
            await ForEach(channel, value => Console.WriteLine($"Channel Result: {value.StringValue}"), () => Console.WriteLine("Channel Done"));

            //Wait for a keypress to end session.
            { Console.WriteLine($"Press any key to continue..."); Console.ReadKey(); }
#endif
        }
Пример #2
0
        static async Task Main(string[] args)
        {
            var transport = new LoopbackTransport();
            //	new WebSocketTransport("ws://localhost:9092/"));
            //	new SocketTransport("tcp://localhost:9091/")

            var client  = new RSocketClient(transport);
            var service = new EchoService.EchoServiceClient(client);

            var server   = new RSocketServer(transport.Beyond);
            var producer = new MyEchoServer(server);

            //TODO ASK: Should these be present in the generated methods to allow one-line construction?
            await server.ConnectAsync();

            await client.ConnectAsync();

            //await service.FireAndForget(Value.ForString("Fire and Forget Test Requrest"));
            //Console.WriteLine($"{nameof(service.FireAndForget)}() => void");

            var responseresult = await service.RequestResponse(Value.ForString("Request Response Test Requrest"));

            Console.WriteLine($"{nameof(service.RequestResponse)}() => {responseresult.StringValue}");


            var streamresults = await service.RequestStream(Value.ForString("Test Stream Request"))
                                .ToListAsync();                         //Collect all of the results. In C#8, this can be an async foreach - nice!

            streamresults.ForEach(result => Console.WriteLine($"{nameof(service.RequestStream)}() => {result.StringValue}"));


            var channelresults = await service.RequestChannel((
                                                                  from value in Enumerable.Range(1, 3) select Value.ForString($"Test Channel Value {value}")
                                                                  ).ToAsyncEnumerable()).ToListAsync();

            //Wait for a keypress to end session.
            { Console.WriteLine($"Press any key to continue..."); Console.ReadKey(); }
        }