WCF开发(6) UWP客户端(hololens)

继续之前的,将程序部署到hololens上,结果不能通行,几种地址都试了一下。稍微说一下,开发时是用usb连接hololens和电脑的。

弄了个360wifi,插到电脑上,hololens连接,显示hololens的ip是172.27.86.72。服务端相应的改为172.27.86.1。可以了。

测试结果

WCF开发(6) UWP客户端(hololens)_第1张图片

测试了一下时间,从结论上说,确实有点不稳定,就算数量级是100,也可能用时1-3s,这种的人是能感觉出来的,大概10多次会有一次吧,除了这种情况外倒是速度还是挺快的。

WCF开发(6) UWP客户端(hololens)_第2张图片

前台代码

    
        
            
            
        
        
            
                
                
                
                
                
            
            
                
                
                
                
                
                
            
        
        
        
    
后台代码

   /// 
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// 
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        public MyContractClient GetClient()
        {
            string ip = CbIpList.SelectedItem.ToString();
            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
            binding.MaxReceivedMessageSize = int.MaxValue;
            EndpointAddress endpointAddress = new EndpointAddress("net.tcp://" + ip + ":8001/");
            MyContractClient currentClient = new MyContractClient(binding, endpointAddress);
            return currentClient;
        }

        private void BtnGet_Click(object sender, RoutedEventArgs e)
        {
            if (CbAutoClear.IsChecked == true)
            {
                TBResult.Text = "";
            }
            string txt = CbCount.SelectedItem.ToString();
            int count = int.Parse(txt);
            TBResult.Text += string.Format("开始,{0},{1}个0\n", count, txt.Length - 1);

            if (CbCloseClient.IsChecked == true)
            {
                TestGetStringAsync(count);
            }
            else
            {
                TestGetStringAsyncNoClose(count);
            }
        }

        private async void TestGetStringAsync(int count)
        {
            for (int i = 1; i <= 9; i++)
            {
                await GetStringAsync(count * i);
            }
            TBResult.Text += "结束" + "\n";
        }

        private async void TestGetStringAsyncNoClose(int count)
        {
            MyContractClient client = GetClient();
            for (int i = 1; i <= 9; i++)
            {
                try
                {
                    DateTime start = DateTime.Now;
                    string msg = await client.GetStringAsync((int)count, "A");
                    count = msg.Length;//这句话没有的话不论数量是多少时间都是立刻,因为不调用Result,实际上是不会执行GetStringAsync的内容的
                    DateTime end = DateTime.Now;
                    string log = string.Format("GetStringAsync,Count:\t{0}, \tStartTime:\t{1}, EndTime:\t{2}, UsedTime:\t{3} ms", count, start.ToString("HH:mm:ss.fff"), end.ToString("HH:mm:ss.fff"), (int)((end - start).TotalMilliseconds));
                    TBResult.Text += log + "\n";
                }
                catch (Exception ex)
                {
                    TBResult.Text += ex + "\n";
                    await client.CloseAsync();
                    break;
                }
            }
            await client.CloseAsync();
            TBResult.Text += "结束" + "\n";
        }

        private System.Threading.Tasks.Task GetStringAsync(int count)
        {
            System.Threading.Tasks.Task task = new System.Threading.Tasks.Task(() => { });
            try
            {
                DateTime start = DateTime.Now;
                MyContractClient client = GetClient();
                System.Threading.Tasks.Task msg = client.GetStringAsync((int)count, "A");
                count = msg.Result.Length;//这句话没有的话不论数量是多少时间都是立刻,因为不调用Result,实际上是不会执行GetStringAsync的内容的
                task = client.CloseAsync();
                DateTime end = DateTime.Now;
                string log = string.Format("GetStringAsync,Count:\t{0}, \tStartTime:\t{1}, EndTime:\t{2}, UsedTime:\t{3} ms", count, start.ToString("HH:mm:ss.fff"), end.ToString("HH:mm:ss.fff"), (int)((end - start).TotalMilliseconds));
                TBResult.Text += log + "\n";
            }
            catch (Exception ex)
            {
                TBResult.Text += ex + "\n";
            }
            return task;
        }

        private void BtnGet2_Click(object sender, RoutedEventArgs e)
        {
            if (CbAutoClear.IsChecked == true)
            {
                TBResult.Text = "";
            }
            string txt = CbCount.SelectedItem.ToString();
            int count = int.Parse(txt);
            TBResult.Text += string.Format("开始,{0},{1}个0\n", count, txt.Length - 1);

            if (CbCloseClient.IsChecked == true)
            {
                TestGetStringClose(count);
            }
            else
            {
                TestGetStringNoClose(count);
            }
            TBResult.Text += "结束" + "\n";
        }

        private void TestGetStringClose(int count)
        {
            for (int i = 1; i <= 9; i++) //10000000
            {
                TestGetString(count * i);
            }
        }

        private async void TestGetStringNoClose(int count)
        {
            MyContractClient client = GetClient();
            for (int i = 1; i <= 9; i++) //10000000
            {
                int number = count * i;
                try
                {
                    DateTime start = DateTime.Now;
                     await client.GetStringAsync((int)count, "A");
                    DateTime end = DateTime.Now;
                    string log = string.Format("GetStringAsync,Count:\t{0}, \tStartTime:\t{1}, EndTime:\t{2}, UsedTime:\t{3} ms", count, start.ToString("HH:mm:ss.fff"), end.ToString("HH:mm:ss.fff"), (int)((end - start).TotalMilliseconds));
                    TBResult.Text += log + "\n";
                }
                catch (Exception ex)
                {

                    TBResult.Text += ex + "\n";
                    await client.CloseAsync();
                }
            }
            await client.CloseAsync();
        }

        private async void TestGetString(int count)
        {
            try
            {
                DateTime start = DateTime.Now;
                MyContractClient client = GetClient();
                await client.GetStringAsync((int)count, "A");
                await client.CloseAsync();
                DateTime end = DateTime.Now;
                string log = string.Format("GetStringAsync,Count:\t{0}, \tStartTime:\t{1}, EndTime:\t{2}, UsedTime:\t{3} ms", count, start.ToString("HH:mm:ss.fff"), end.ToString("HH:mm:ss.fff"), (int)((end - start).TotalMilliseconds));
                TBResult.Text += log + "\n";
            }
            catch (Exception ex)
            {

                TBResult.Text += ex + "\n";
            }
        }

        private void Page_Loading(FrameworkElement sender, object args)
        {

        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            IReadOnlyList hostList= NetworkInformation.GetHostNames();
            CbHostList.ItemsSource = hostList;
            CbHostList.DisplayMemberPath = "DisplayName";

            foreach (var item in hostList)
            {
                TBResult.Text += item.DisplayName + "\n";
            }

            CbIpList.ItemsSource = new string[] { "127.0.0.1","localhost","192.168.1.155", "172.27.86.1"};
            CbIpList.SelectedIndex = 0;

            CbCount.ItemsSource = new string[] { "100", "1000", "10000", "100000", "1000000", "10000000", "10000000" };
            CbCount.SelectedIndex = 0;
        }

        private void BtnClearText_Click(object sender, RoutedEventArgs e)
        {
            TBResult.Text = "";
        }

        private async void BtnHello_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MyContractClient client = GetClient();
                string msg = await client.MyMethodAsync();
                TBResult.Text += msg + "\n";
            }
            catch (Exception ex)
            {
                TBResult.Text += ex + "\n";
            }
        }
    }

在循环中每次都获取一个新客户端(用完就关闭)相比,所有循环都用同一个客户端(多次调用函数)快多了,也不容易出现网络不稳定的情况(突然某一次很慢)。

另外,前面有个地方理解错了。并行部分的代码中,虽然发送是并行的,接收并处理是串行的。那时接收时间差不多可能是因为是本机测试的吧。

===================================

在做了一些unity开发测试后,回来发现这个UWP客户端部署到hololens上后有连接不上了。

查看网卡设置后,发现360wifi的IP变了。

重新设置后又可以连接了。

你可能感兴趣的:(wcf,hololens,uwp)