Modified HttpServer.cs (added RootPath property)

This commit is contained in:
sta 2013-07-04 15:48:01 +09:00
parent 0619a43caa
commit b9c5c222ca
2 changed files with 46 additions and 14 deletions

View File

@ -12,6 +12,7 @@ namespace Example3
public static void Main(string[] args) public static void Main(string[] args)
{ {
_httpsv = new HttpServer(4649); _httpsv = new HttpServer(4649);
//_httpsv.RootPath = "../../Public";
//_httpsv.Sweeping = false; //_httpsv.Sweeping = false;
_httpsv.AddWebSocketService<Echo>("/Echo"); _httpsv.AddWebSocketService<Echo>("/Echo");
_httpsv.AddWebSocketService<Chat>("/Chat"); _httpsv.AddWebSocketService<Chat>("/Chat");

View File

@ -41,11 +41,12 @@ namespace WebSocketSharp.Server {
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// The HttpServer class provides the multi WebSocket service. /// The HttpServer instance can provide the multi WebSocket services.
/// </para> /// </para>
/// <para> /// <para>
/// <para> /// <para>
/// 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 <see cref="RootPath"/> property.
/// </para> /// </para>
/// <code lang="xml"> /// <code lang="xml">
/// &lt;?xml version="1.0" encoding="utf-8"?&gt; /// &lt;?xml version="1.0" encoding="utf-8"?&gt;
@ -105,7 +106,27 @@ namespace WebSocketSharp.Server {
/// An <see cref="int"/> that contains a port number. /// An <see cref="int"/> that contains a port number.
/// </value> /// </value>
public int Port { public int Port {
get { return _port; } get {
return _port;
}
}
/// <summary>
/// Gets or sets the document root path of server.
/// </summary>
/// <value>
/// An <see cref="string"/> that contains the document root path of server.
/// The default value is set from the application configuration file if is available;
/// otherwise, <c>./Public</c>.
/// </value>
public string RootPath {
get {
return _rootPath;
}
set {
_rootPath = value;
}
} }
/// <summary> /// <summary>
@ -196,17 +217,12 @@ namespace WebSocketSharp.Server {
#region Private Methods #region Private Methods
private void configureFromConfigFile()
{
_rootPath = ConfigurationManager.AppSettings["RootPath"];
}
private void init() private void init()
{ {
_isWindows = false;
_listener = new HttpListener(); _listener = new HttpListener();
_svcHosts = new ServiceHostManager(); _svcHosts = new ServiceHostManager();
_isWindows = false;
var os = Environment.OSVersion; var os = Environment.OSVersion;
if (os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX) if (os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX)
_isWindows = true; _isWindows = true;
@ -215,7 +231,21 @@ namespace WebSocketSharp.Server {
"http{0}://*:{1}/", _port == 443 ? "s" : String.Empty, _port); "http{0}://*:{1}/", _port == 443 ? "s" : String.Empty, _port);
_listener.Prefixes.Add(prefix); _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) private void onError(string message)
@ -399,10 +429,11 @@ namespace WebSocketSharp.Server {
} }
/// <summary> /// <summary>
/// Gets the contents of the specified file. /// Gets the contents of the file with the specified <paramref name="path"/>.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// An array of <see cref="byte"/> that contains the contents of the file. /// An array of <see cref="byte"/> that contains the contents of the file if exists;
/// otherwise, <see langword="null"/>.
/// </returns> /// </returns>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that contains a virtual path to the file to get. /// A <see cref="string"/> that contains a virtual path to the file to get.