Removed the configuration with an App.config file from the HttpServer class

This commit is contained in:
sta 2013-07-24 17:23:48 +09:00
parent 998a296d18
commit cd7dfea62d
4 changed files with 103 additions and 140 deletions

View File

@ -49,6 +49,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AssemblyInfo.cs" /> <Compile Include="AssemblyInfo.cs" />

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Configuration;
using WebSocketSharp; using WebSocketSharp;
using WebSocketSharp.Net; using WebSocketSharp.Net;
using WebSocketSharp.Server; using WebSocketSharp.Server;
@ -15,7 +16,7 @@ namespace Example3
#if DEBUG #if DEBUG
_httpsv.Log.Level = LogLevel.TRACE; _httpsv.Log.Level = LogLevel.TRACE;
#endif #endif
//_httpsv.RootPath = "../../Public"; _httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
//_httpsv.Sweeping = false; //_httpsv.Sweeping = false;
_httpsv.AddWebSocketService<Echo> ("/Echo"); _httpsv.AddWebSocketService<Echo> ("/Echo");
_httpsv.AddWebSocketService<Chat> ("/Chat"); _httpsv.AddWebSocketService<Chat> ("/Chat");
@ -36,7 +37,7 @@ namespace Example3
Console.WriteLine (" {0}", path); Console.WriteLine (" {0}", path);
Console.WriteLine (); Console.WriteLine ();
Console.WriteLine("Press enter key to stop server..."); Console.WriteLine ("Press enter key to stop the server...");
Console.ReadLine (); Console.ReadLine ();
_httpsv.Stop (); _httpsv.Stop ();

View File

@ -28,38 +28,21 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using WebSocketSharp.Net; using WebSocketSharp.Net;
namespace WebSocketSharp.Server { namespace WebSocketSharp.Server
{
/// <summary> /// <summary>
/// Provides a simple HTTP server that allows to accept the WebSocket connection requests. /// Provides a simple HTTP server that allows to accept the WebSocket connection requests.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>
/// The HttpServer instance can provide the multi WebSocket services. /// The HttpServer instance can provide the multi WebSocket services.
/// </para>
/// <para>
/// <para>
/// The HttpServer instance can set the document root path of server using
/// the application configuration file or <see cref="RootPath"/> property.
/// </para>
/// <code lang="xml">
/// &lt;?xml version="1.0" encoding="utf-8"?&gt;
/// &lt;configuration&gt;
/// &lt;appSettings&gt;
/// &lt;add key="RootPath" value="./Public" /&gt;
/// &lt;/appSettings&gt;
/// &lt;/configuration&gt;
/// </code>
/// </para>
/// </remarks> /// </remarks>
public class HttpServer { public class HttpServer
{
#region Private Fields #region Private Fields
private HttpListener _listener; private HttpListener _listener;
@ -118,7 +101,7 @@ namespace WebSocketSharp.Server {
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The default logging level is the <see cref="LogLevel.ERROR"/>. /// The default logging level is the <see cref="LogLevel.ERROR"/>.
/// If you wanted to change the current logging level, you would set the <c>Log.Level</c> property /// If you want to change the current logging level, you set the <c>Log.Level</c> property
/// to one of the <see cref="LogLevel"/> values which you want. /// to one of the <see cref="LogLevel"/> values which you want.
/// </remarks> /// </remarks>
/// <value> /// <value>
@ -146,9 +129,8 @@ namespace WebSocketSharp.Server {
/// Gets or sets the document root path of server. /// Gets or sets the document root path of server.
/// </summary> /// </summary>
/// <value> /// <value>
/// An <see cref="string"/> that contains the document root path of server. /// A <see cref="string"/> that contains the document root path of server.
/// The default value is set from the application configuration file if is available; /// The default value is <c>./Public</c>.
/// otherwise, <c>./Public</c>.
/// </value> /// </value>
public string RootPath { public string RootPath {
get { get {
@ -259,7 +241,7 @@ namespace WebSocketSharp.Server {
_listener = new HttpListener (); _listener = new HttpListener ();
_listening = false; _listening = false;
_logger = new Logger (); _logger = new Logger ();
_rootPath = getRootPath(); _rootPath = "./Public";
_svcHosts = new ServiceHostManager (); _svcHosts = new ServiceHostManager ();
_windows = false; _windows = false;
@ -267,25 +249,10 @@ namespace WebSocketSharp.Server {
if (os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX) if (os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX)
_windows = true; _windows = true;
var prefix = String.Format( var prefix = String.Format ("http{0}://*:{1}/", _port == 443 ? "s" : "", _port);
"http{0}://*:{1}/", _port == 443 ? "s" : String.Empty, _port);
_listener.Prefixes.Add (prefix); _listener.Prefixes.Add (prefix);
} }
private static string getRootPath()
{
string rootPath = null;
try {
rootPath = ConfigurationManager.AppSettings["RootPath"];
}
catch {
}
return rootPath.IsNullOrEmpty()
? "./Public"
: rootPath;
}
private void processHttpRequest (HttpListenerContext context) private void processHttpRequest (HttpListenerContext context)
{ {
var eventArgs = new HttpRequestEventArgs (context); var eventArgs = new HttpRequestEventArgs (context);
@ -367,10 +334,9 @@ namespace WebSocketSharp.Server {
private void processRequestAsync (HttpListenerContext context) private void processRequestAsync (HttpListenerContext context)
{ {
WaitCallback callback = (state) => WaitCallback callback = state =>
{
try
{ {
try {
if (context.Request.IsUpgradeTo ("websocket")) if (context.Request.IsUpgradeTo ("websocket"))
{ {
if (processWebSocketRequest (context)) if (processWebSocketRequest (context))
@ -383,8 +349,7 @@ namespace WebSocketSharp.Server {
context.Response.Close (); context.Response.Close ();
} }
catch (Exception ex) catch (Exception ex) {
{
_logger.Fatal (ex.Message); _logger.Fatal (ex.Message);
error ("An exception has occured."); error ("An exception has occured.");
} }
@ -397,17 +362,14 @@ namespace WebSocketSharp.Server {
{ {
while (true) while (true)
{ {
try try {
{
processRequestAsync (_listener.GetContext ()); processRequestAsync (_listener.GetContext ());
} }
catch (HttpListenerException) catch (HttpListenerException) {
{ _logger.Info ("HttpListener has been stopped.");
// HttpListener has been closed.
break; break;
} }
catch (Exception ex) catch (Exception ex) {
{
_logger.Fatal (ex.Message); _logger.Fatal (ex.Message);
error ("An exception has occured."); error ("An exception has occured.");
@ -428,7 +390,7 @@ namespace WebSocketSharp.Server {
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Adds the specified type WebSocket service. /// Adds the specified typed WebSocket service.
/// </summary> /// </summary>
/// <param name="absPath"> /// <param name="absPath">
/// A <see cref="string"/> that contains an absolute path associated with the WebSocket service. /// A <see cref="string"/> that contains an absolute path associated with the WebSocket service.

View File

@ -59,7 +59,6 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.ServiceModel" /> <Reference Include="System.ServiceModel" />
<Reference Include="System.Configuration" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AssemblyInfo.cs" /> <Compile Include="AssemblyInfo.cs" />