Added a new AddWebSocketService method to the WebSocketServer and HttpServer classes

This commit is contained in:
sta
2013-10-02 14:39:56 +09:00
parent 2b57a58da2
commit 24e74b60b7
8 changed files with 356 additions and 64 deletions

View File

@@ -28,6 +28,13 @@
*/
#endregion
#region Thanks
/*
* Thanks:
* Juan Manuel Lallana <juan.manuel.lallana@gmail.com>
*/
#endregion
using System;
using System.Collections.Generic;
using System.Net.Sockets;
@@ -245,20 +252,53 @@ namespace WebSocketSharp.Server
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service.
/// </param>
/// <typeparam name="TWithNew">
/// The type of the WebSocket service. The TWithNew must inherit the <see cref="WebSocketService"/>
/// class and must have a public parameterless constructor.
/// </typeparam>
public void AddWebSocketService<TWithNew> (string servicePath)
where TWithNew : WebSocketService, new ()
{
AddWebSocketService<TWithNew> (servicePath, () => new TWithNew ());
}
/// <summary>
/// Adds the specified typed WebSocket service with the specified <paramref name="servicePath"/> and
/// <paramref name="serviceConstructor"/>.
/// </summary>
/// <remarks>
/// <para>
/// This method converts <paramref name="servicePath"/> to URL-decoded string and
/// removes <c>'/'</c> from tail end of <paramref name="servicePath"/>.
/// </para>
/// <para>
/// <paramref name="serviceConstructor"/> returns a initialized specified typed WebSocket service
/// instance.
/// </para>
/// </remarks>
/// <param name="servicePath">
/// A <see cref="string"/> that contains an absolute path to the WebSocket service.
/// </param>
/// <param name="serviceConstructor">
/// A Func&lt;T&gt; delegate that references the method used to initialize a new WebSocket service
/// instance (a new WebSocket session).
/// </param>
/// <typeparam name="T">
/// The type of the WebSocket service. The T must inherit the <see cref="WebSocketService"/> class.
/// </typeparam>
public void AddWebSocketService<T> (string servicePath)
where T : WebSocketService, new ()
public void AddWebSocketService<T> (string servicePath, Func<T> serviceConstructor)
where T : WebSocketService
{
var msg = servicePath.CheckIfValidServicePath ();
var msg = servicePath.CheckIfValidServicePath () ??
(serviceConstructor == null ? "'serviceConstructor' must not be null." : null);
if (msg != null)
{
Log.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
return;
}
var host = new WebSocketServiceHost<T> (Log);
var host = new WebSocketServiceHost<T> (serviceConstructor, Log);
host.Uri = BaseUri.IsAbsoluteUri
? new Uri (BaseUri, servicePath)
: servicePath.ToUri ();