Esempio n. 1
0
        private HttpListener CreateHttpListener(Uri uri)
        {
            HttpListener listener = new HttpListener();
            string       scheme   = GetHttpScheme(uri);
            UriBuilder   newUri   = new UriBuilder(uri)
            {
                Scheme = scheme
            };

            listener.Prefixes.Add(newUri.ToString());
            listener.Start();
            Task task = new Task(delegate
            {
                while (true)
                {
                    HttpListenerContext listenerContext;
                    try
                    {
                        listenerContext = listener.GetContext();
                    }
                    catch
                    {
                        break;
                    }
                    if (listenerContext.Request.IsWebSocketRequest)
                    {
                        Invoke(new MethodInvoker(delegate
                        {
                            HttpListenerClientContext clientContext =
                                new HttpListenerClientContext(listenerContext);
                            ClientForm clientForm = new ClientForm(clientContext);
                            clientForm.Show();
                        }));
                    }
                    else
                    {
                        listenerContext.Response.StatusCode = 400;
                        listenerContext.Response.Close();
                    }
                }
            });

            task.Start();
            return(listener);
        }