Esempio n. 1
0
 private void InitRoutes()
 {
     if (null == Routes)
     {
         Routes = new List <Route>
         {
             new Route {
                 Name     = "wfPlayer command",
                 UrlRegex = @"^/wfplayer/cmd/.*",
                 Method   = "GET",
                 Callable = (HttpRequest request) => {
                     var match  = mRegex.Match(request.Url);
                     var result = false;
                     var cmd    = "unknown";
                     if (match.Success)
                     {
                         cmd    = match.Groups["cmd"].Value;
                         result = mInvoker?.Invoke(cmd) ?? false;
                     }
                     return(new HttpResponse()
                     {
                         ContentAsUTF8 = $"{{'cmd'='{cmd}','result'='{result}'}}",
                         ReasonPhrase = "OK",
                         StatusCode = "200"
                     });
                 }
             },
         };
     }
 }
Esempio n. 2
0
        private async Task Run()
        {
            while (mListener.IsListening)
            {
                try
                {
                    var ctx = await mListener.GetContextAsync();

                    var req = ctx.Request;
                    var uri = req.RawUrl;
                    if (uri.ToString() == "/wfplayer/")
                    {
                        using (var res = ctx.Response)
                            using (var stream = new StreamReader(@"Resources\controller.html"))
                            {
                                var html = await stream.ReadToEndAsync();

                                var buff = Encoding.UTF8.GetBytes(html);
                                await res.OutputStream.WriteAsync(buff, 0, buff.Length);
                            }
                    }
                    else
                    {
                        using (var res = ctx.Response)
                        {
                            var match = mRegex.Match(uri.ToString());
                            if (match.Success)
                            {
                                var  cmd    = match.Groups["cmd"].Value;
                                bool result = mInvoker?.Invoke(cmd) ?? false;
                                var  buff   = Encoding.UTF8.GetBytes($"{{'cmd'='{cmd}','result'='{result}'}}");
                                await res.OutputStream.WriteAsync(buff, 0, buff.Length);
                            }
                            else
                            {
                                var buff = Encoding.UTF8.GetBytes("{'result'='false'}");
                                await res.OutputStream.WriteAsync(buff, 0, buff.Length);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"HttpListener error: {e.ToString()}");
                    break;
                }
            }
            mListener.Close();
            mListener = null;
        }