示例#1
0
        private static async Task ChatAsync(ChatClient from, ChatClient to)
        {
            await from.WriteLineAsync("Found one say hi");
            await to.WriteLineAsync("Found one say hi");

            var fromTask = from.Stream.CopyToAsync(to.Stream);
            var toTask = to.Stream.CopyToAsync(from.Stream);

            var result = await Task.WhenAny(fromTask, toTask);

            if (fromTask.IsCompleted && toTask.IsCompleted)
            {
                // Noop
                from.Dispose();
                to.Dispose();
            }
            else if (result == fromTask)
            {
                from.Dispose();
                await to.WriteLineAsync("Your partner left!");
                Match(to);
            }
            else if (result == toTask)
            {
                to.Dispose();
                await from.WriteLineAsync("Your partner left!");
                Match(from);
            }
        }
示例#2
0
        // Fire and forget
        private static async void Match(ChatClient connection)
        {
            try
            {
                await connection.WriteLineAsync("Waiting for a partner...");

                ChatClient partner;
                if (_queue.TryDequeue(out partner) && partner.Socket.Connected)
                {
                    await ChatAsync(partner, connection);
                }
                else
                {
                    _queue.Enqueue(connection);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }