diff --git a/Example3/Program.cs b/Example3/Program.cs index f8142d4a..1da01968 100644 --- a/Example3/Program.cs +++ b/Example3/Program.cs @@ -12,6 +12,7 @@ namespace Example3 public static void Main(string[] args) { _httpsv = new HttpServer(4649); + //_httpsv.RootPath = "../../Public"; //_httpsv.Sweeping = false; _httpsv.AddWebSocketService("/Echo"); _httpsv.AddWebSocketService("/Chat"); diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index d10e8850..9966f0dc 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -41,11 +41,12 @@ namespace WebSocketSharp.Server { /// /// /// - /// The HttpServer class provides the multi WebSocket service. + /// The HttpServer instance can provide the multi WebSocket services. /// /// /// - /// The HttpServer class needs the application configuration file to configure the server root path. + /// The HttpServer instance can set the document root path of server using + /// the application configuration file or property. /// /// /// <?xml version="1.0" encoding="utf-8"?> @@ -105,7 +106,27 @@ namespace WebSocketSharp.Server { /// An that contains a port number. /// public int Port { - get { return _port; } + get { + return _port; + } + } + + /// + /// Gets or sets the document root path of server. + /// + /// + /// An that contains the document root path of server. + /// The default value is set from the application configuration file if is available; + /// otherwise, ./Public. + /// + public string RootPath { + get { + return _rootPath; + } + + set { + _rootPath = value; + } } /// @@ -196,17 +217,12 @@ namespace WebSocketSharp.Server { #region Private Methods - private void configureFromConfigFile() - { - _rootPath = ConfigurationManager.AppSettings["RootPath"]; - } - private void init() { - _isWindows = false; - _listener = new HttpListener(); - _svcHosts = new ServiceHostManager(); + _listener = new HttpListener(); + _svcHosts = new ServiceHostManager(); + _isWindows = false; var os = Environment.OSVersion; if (os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX) _isWindows = true; @@ -215,7 +231,21 @@ namespace WebSocketSharp.Server { "http{0}://*:{1}/", _port == 443 ? "s" : String.Empty, _port); _listener.Prefixes.Add(prefix); - configureFromConfigFile(); + _rootPath = getRootPath(); + } + + private static string getRootPath() + { + string rootPath = null; + try { + rootPath = ConfigurationManager.AppSettings["RootPath"]; + } + catch { + } + + return rootPath.IsNullOrEmpty() + ? "./Public" + : rootPath; } private void onError(string message) @@ -399,10 +429,11 @@ namespace WebSocketSharp.Server { } /// - /// Gets the contents of the specified file. + /// Gets the contents of the file with the specified . /// /// - /// An array of that contains the contents of the file. + /// An array of that contains the contents of the file if exists; + /// otherwise, . /// /// /// A that contains a virtual path to the file to get.