Example #1
0
        private static async Task <SignalRStreamingCall> GetSignalRStateAsync(int i)
        {
            if (!SignalRCache.TryGetValue(i, out var state))
            {
                var builder = new HubConnectionBuilder();
                builder.WithUrl("http://localhost:5000/greeterhub");
                builder.AddMessagePackProtocol();
                var hubConnection = builder.Build();
                await hubConnection.StartAsync();

                var channel = System.Threading.Channels.Channel.CreateUnbounded <string>(new UnboundedChannelOptions
                {
                    SingleReader = true,
                    SingleWriter = true
                });

                var result = hubConnection.StreamAsync <string>("SayHelloBiDi", channel.Reader);
                state = new SignalRStreamingCall
                {
                    RequestStream  = channel.Writer,
                    ResponseStream = result.GetAsyncEnumerator()
                };

                bool success = SignalRCache.TryAdd(i, state);
                if (!success)
                {
                    throw new InvalidOperationException();
                }
            }

            return(state);
        }
Example #2
0
        private static async Task <HelloReply> MakeSignalRCall(HelloRequest request, SignalRStreamingCall state)
        {
            await state.RequestStream.WriteAsync(request.Name);

            if (!await state.ResponseStream.MoveNextAsync())
            {
                throw new InvalidOperationException("Unexpected end of stream.");
            }
            return(new HelloReply
            {
                Message = state.ResponseStream.Current
            });
        }