public void Start(int port, [CanBeNull] string filename) { #if DEBUG var log = this; #else var log = new NullLog(); #endif _server = new WebServer($"http://+:{port}/", log, RoutingStrategy.Wildcard); // HTTP part: index webpage, static files nearby (if any) var pages = new PagesProvider(filename); _server.RegisterModule(new WebApiModule()); _server.Module <WebApiModule>().RegisterController(() => new IndexPageController(pages)); if (Directory.Exists(pages.StaticDirectory)) { _server.RegisterModule(new StaticFilesModule(new Dictionary <string, string> { [@"/"] = pages.StaticDirectory }) { UseRamCache = false, UseGzip = false }); } // Websockets part var module = new WebSocketsModule(); _server.RegisterModule(module); _sharedServer = new PublishDataSocketsServer(@"Current Race Shared Memory Server"); module.RegisterWebSocketsServer(@"/api/ws/shared", _sharedServer); _statsServer = new PublishDataSocketsServer(@"Current Race Stats Server"); module.RegisterWebSocketsServer(@"/api/ws/stats", _statsServer); // Starting try { _server.RunAsync(); } catch (HttpListenerException e) { NonfatalError.NotifyBackground("Can’t start web server", $"Don’t forget to allow port’s usage with something like “netsh http add urlacl url=\"http://+:{port}/\" user=everyone”.", e, new[] { new NonfatalErrorSolution($"Use “netsh” to allow usage of port {port}", null, token => { Process.Start(new ProcessStartInfo { FileName = "cmd", Arguments = $"/C netsh http add urlacl url=\"http://+:{port}/\" user=everyone & pause", Verb = "runas" }); return(Task.Delay(1000, token)); }) }); } catch (Exception e) { Logging.Error(e); } }
public void Start(int port) { _server = new WebServer($"http://+:{port}/", new NullLog(), RoutingStrategy.Wildcard); _server.RegisterModule(new WebApiModule()); _server.Module<WebApiModule>().RegisterController<PlayerStatsController>(); var module = new WebSocketsModule(); _server.RegisterModule(module); _currentServer = new PublishDataSocketsServer(@"Current Race Stats Server"); module.RegisterWebSocketsServer("/api/current", _currentServer); try { _server.RunAsync(); } catch (HttpListenerException e) { Logging.Warning(e.Message + $"\nDon’t forget to reserve url using something like “netsh http add urlacl url=\"http://+:{port}/\" user=everyone”."); } catch (Exception e) { Logging.Error(e); } }
public void Start(int port, [CanBeNull] string filename) { #if DEBUG var log = this; #else var log = new NullLog(); #endif _server = new WebServer($"http://+:{port}/", log, RoutingStrategy.Wildcard); // HTTP part: index webpage, static files nearby (if any) var pages = new PagesProvider(filename); _server.RegisterModule(new WebApiModule()); _server.Module <WebApiModule>().RegisterController(() => new IndexPageController(pages)); if (Directory.Exists(pages.StaticDirectory)) { _server.RegisterModule(new StaticFilesModule(new Dictionary <string, string> { [@"/"] = pages.StaticDirectory }) { UseRamCache = false, UseGzip = false }); } // Websockets part var module = new WebSocketsModule(); _server.RegisterModule(module); _sharedServer = new PublishDataSocketsServer(@"Current Race Shared Memory Server"); module.RegisterWebSocketsServer(@"/api/ws/shared", _sharedServer); _statsServer = new PublishDataSocketsServer(@"Current Race Stats Server"); module.RegisterWebSocketsServer(@"/api/ws/stats", _statsServer); // Starting try { _server.RunAsync(); } catch (HttpListenerException e) when(e.ToString().Contains("0x80004005")) { NonfatalError.NotifyBackground("Can’t start web server", $"Don’t forget to allow port’s usage with something like “netsh http add urlacl url=\"http://+:{port}/\" user=everyone”.", e, new[] { new NonfatalErrorSolution($"Use “netsh” to allow usage of port {port}", null, async token => { try { var command = $"netsh http add urlacl url=\"http://+:{port}/\" user=everyone"; var proc = ProcessExtension.Start("cmd", new[] { "/C", $"echo {command} & {command} & @pause" }, new ProcessStartInfo { Verb = "runas" }); await proc.WaitForExitAsync(token).ConfigureAwait(false); Logging.Debug("Done: " + proc.ExitCode); } catch (Win32Exception ex) when(ex.ErrorCode != -1) { Logging.Debug(ex.ErrorCode); throw new InformativeException("Access denied", "Unfortunately, HTTP.sys driver doesn’t allow to assign ports without administrator privileges."); } }) }); } catch (Exception e) { Logging.Error(e); } }