public IClientConnector Start(IApplication application) { var port = PortFinder.FindPort(5000); _application = application; BaseAddress = "http://localhost:" + port; var webSockets = new WebSocketsHandler(); _commands = new CommandRunner(application); Client = new ClientConnector(webSockets, _commands.HandleJson) { WebSocketsAddress = $"ws://127.0.0.1:{port}" }; startWebServer(port, webSockets); #if DEBUG _watcher = new AssetFileWatcher(Client); _watcher.Start(); #endif return Client; }
private void startWebServer(int port, WebSocketsHandler webSockets) { var baseDirectory = AppContext.BaseDirectory; var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(baseDirectory) .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); }); #if DEBUG configureStaticFiles(app); #endif app.Run(async http => { if (http.Request.Path == "/favicon.ico") { var stream = GetType() .GetTypeInfo() .Assembly.GetManifestResourceStream("StorytellerRunner.favicon.ico"); http.Response.ContentType = "image/x-icon"; await stream.CopyToAsync(http.Response.Body).ConfigureAwait(false); return; } try { string html; if (http.Request.Path.HasValue && http.Request.Path.Value == "/preview") { html = ExportWriter.BuildPage(_application); } else { html = HomeEndpoint.BuildPage(_application, _input).ToString(); } http.Response.ContentType = "text/html"; await http.Response.WriteAsync(html).ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e.ToString()); throw; } }); }); _server = host.Start(); }