Exemplo n.º 1
0
        public IDisposable LaunchRunner()
        {
            var middleware = new TopicMiddleware(this, _container.GetInstance <IHtmlGenerator>(), _settings);
            var webSockets = new WebSocketsHandler();

            _refresher = new BrowserRefresher(webSockets);

            var port = PortFinder.FindPort(5000);


            _settings.WebsocketAddress = $"ws://localhost:{port}";


            _container.Inject <IBrowserRefresher>(_refresher);
            StartScanning();

            var host = startHost(port, webSockets, middleware);

            _topicWatcher = new TopicFileWatcher(_settings, this);


            _topicWatcher.StartWatching(_refresher);

            return(host);
        }
Exemplo n.º 2
0
        private IWebHost startHost(int port, WebSocketsHandler webSockets, TopicMiddleware middleware)
        {
            var host = new WebHostBuilder()
                       .UseKestrel()
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .UseUrls($"http://localhost:{port}")
                       .Configure(app =>
            {
                app.UseWebSockets();

                app.Use(async(http, next) =>
                {
                    if (http.WebSockets.IsWebSocketRequest)
                    {
                        await webSockets.HandleSocket(http).ConfigureAwait(false);
                    }
                    else
                    {
                        await next().ConfigureAwait(false);
                    }
                });

                app.UseStaticFiles(new StaticFileOptions
                {
                    ServeUnknownFileTypes = true,
                    FileProvider          = new PhysicalFileProvider(_settings.Root)
                });

                app.Use(async(http, next) =>
                {
                    if (http.Request.Method.EqualsIgnoreCase("POST"))
                    {
                        switch (http.Request.Path)
                        {
                        case "/refresh":
                            Console.WriteLine("Hey, I got the order to do the hard refresh!");
                            await HardRefresh().ConfigureAwait(false);

                            _refresher.RefreshPage();
                            break;

                        case "/open":

                            var url = new Uri(http.Request.Headers["referer"]);

                            Console.WriteLine("Open requested to " + url);

                            var topic = FindTopicByUrl(url.AbsolutePath.TrimStart('/'));
                            if (topic != null)
                            {
                                try {
                                    var start             = new ProcessStartInfo();
                                    start.UseShellExecute = true;
                                    start.FileName        = "open";
                                    start.Arguments       = topic.File;
                                    Process.Start(start);
                                }
                                catch (Exception ex) {
                                    Process.Start(topic.File);
                                }
                            }


                            break;
                        }
                    }
                    else
                    {
                        await next().ConfigureAwait(false);
                    }
                });

                app.Run(middleware.Invoke);
            })
                       .Build();

            host.Start();
            return(host);
        }