private void InitializeServer() { if (_server != null) { _server.Stop(); _server = null; } LogMessage("Initializing Server..."); List <String> bindings = new List <String>(); int currentBinding = 1; String currentBindingKey = String.Format(WebServerHostKey, currentBinding); while (Configuration.HasSetting(currentBindingKey)) { bindings.Add(Configuration.GetString(currentBindingKey)); currentBinding++; currentBindingKey = String.Format(WebServerHostKey, currentBinding); } // If there are no bindings in the configuration file, we'll need to initialize those values. if (bindings.Count == 0) { const String defaultBinding = "http://localhost:8080/"; bindings.Add(defaultBinding); // If there aren't any bindings, the value of currentBindingKey will never have made it past 1. // As a result, we can just use that. Configuration.SetString(currentBindingKey, defaultBinding); Configuration.SaveSettings(); } // The endpoint used internally should always be the first binding in the configuration. // There's no need to use multiple bindings for internal references, we only need a single one. _endpoint = bindings.First(); WebServer ws = new WebServer(HandleRequest, bindings.ToArray()); _server = ws; _server.Run(); LogMessage("Server Initialized."); _requestHandlers = new List <IRequestHandler>(); try { RegisterHandlers(); } catch (Exception ex) { UnityEngine.Debug.LogException(ex); } }
private void InitializeServer() { if (_server != null) { _server.Stop(); _server.LogMessage -= ServerOnLogMessage; _server = null; } LogMessage("Initializing Server..."); // I'm not sure how I feel about making the port registration configurable. // Honestly, it sort of defeats the purpose, since other mods could potentially expect it to exist on a specific port. int port = 8080; if (Configuration.HasSetting(WebServerPortKey)) { port = Configuration.GetInt(WebServerPortKey); } else { Configuration.SetInt(WebServerPortKey, port); Configuration.SaveSettings(); } String endpoint = String.Format("http://localhost:{0}/", port); Endpoint = endpoint; WebServer ws = new WebServer(HandleRequest, endpoint); _server = ws; _server.LogMessage += ServerOnLogMessage; _server.Run(); LogMessage("Server Initialized."); }