Renamed the WebSocketService class to the WebSocketBehavior class

This commit is contained in:
sta 2014-09-06 17:50:10 +09:00
parent 73bfd1dbab
commit 5109d88ebb
11 changed files with 194 additions and 154 deletions

View File

@ -5,7 +5,7 @@ using WebSocketSharp.Server;
namespace Example2 namespace Example2
{ {
public class Chat : WebSocketService public class Chat : WebSocketBehavior
{ {
private static int _num = 0; private static int _num = 0;

View File

@ -4,7 +4,7 @@ using WebSocketSharp.Server;
namespace Example2 namespace Example2
{ {
public class Echo : WebSocketService public class Echo : WebSocketBehavior
{ {
protected override void OnMessage (MessageEventArgs e) protected override void OnMessage (MessageEventArgs e)
{ {

View File

@ -5,7 +5,7 @@ using WebSocketSharp.Server;
namespace Example3 namespace Example3
{ {
public class Chat : WebSocketService public class Chat : WebSocketBehavior
{ {
private static int _num = 0; private static int _num = 0;

View File

@ -4,7 +4,7 @@ using WebSocketSharp.Server;
namespace Example3 namespace Example3
{ {
public class Echo : WebSocketService public class Echo : WebSocketBehavior
{ {
protected override void OnMessage (MessageEventArgs e) protected override void OnMessage (MessageEventArgs e)
{ {

View File

@ -243,7 +243,7 @@ using WebSocketSharp.Server;
namespace Example namespace Example
{ {
public class Laputa : WebSocketService public class Laputa : WebSocketBehavior
{ {
protected override void OnMessage (MessageEventArgs e) protected override void OnMessage (MessageEventArgs e)
{ {
@ -277,11 +277,11 @@ Required namespace.
using WebSocketSharp.Server; using WebSocketSharp.Server;
``` ```
The `WebSocketServer` and `WebSocketService` classes exist in the `WebSocketSharp.Server` namespace. The `WebSocketBehavior` and `WebSocketServer` classes exist in the `WebSocketSharp.Server` namespace.
#### Step 2 #### #### Step 2 ####
Creating the class that inherits the `WebSocketService` class. Creating the class that inherits the `WebSocketBehavior` class.
For example, if you would like to provide an echo service, For example, if you would like to provide an echo service,
@ -290,7 +290,7 @@ using System;
using WebSocketSharp; using WebSocketSharp;
using WebSocketSharp.Server; using WebSocketSharp.Server;
public class Echo : WebSocketService public class Echo : WebSocketBehavior
{ {
protected override void OnMessage (MessageEventArgs e) protected override void OnMessage (MessageEventArgs e)
{ {
@ -306,7 +306,7 @@ using System;
using WebSocketSharp; using WebSocketSharp;
using WebSocketSharp.Server; using WebSocketSharp.Server;
public class Chat : WebSocketService public class Chat : WebSocketBehavior
{ {
private string _suffix; private string _suffix;
@ -327,15 +327,17 @@ public class Chat : WebSocketService
} }
``` ```
If you override the `WebSocketService.OnMessage (MessageEventArgs)` method, it's called when the `OnMessage` event of the `WebSocket` used in the current session in the WebSocket service occurs. You can define the behavior of any WebSocket service by creating the class that inherits the `WebSocketBehavior` class.
And if you override the `WebSocketService.OnOpen ()`, `WebSocketService.OnError (ErrorEventArgs)`, and `WebSocketService.OnClose (CloseEventArgs)` methods, each of them is called when each event of the `WebSocket` (the `OnOpen`, `OnError`, and `OnClose` events) occurs. If you override the `WebSocketBehavior.OnMessage (MessageEventArgs)` method, it's called when the `WebSocket` used in the current session in the service receives a message.
The `WebSocketService.Send` method sends a data to the client on the current session in the WebSocket service. And if you override the `WebSocketBehavior.OnOpen ()`, `WebSocketBehavior.OnError (ErrorEventArgs)`, and `WebSocketBehavior.OnClose (CloseEventArgs)` methods, each of them is called when each event of the `WebSocket` (the `OnOpen`, `OnError`, and `OnClose` events) occurs.
If you would like to access the sessions in the WebSocket service, you should use the `WebSocketService.Sessions` property (returns a `WebSocketSharp.Server.WebSocketSessionManager`). The `WebSocketBehavior.Send` method sends a data to the client on the current session in the service.
The `WebSocketService.Sessions.Broadcast` method broadcasts a data to every client in the WebSocket service. If you would like to access the sessions in the service, you should use the `WebSocketBehavior.Sessions` property (returns a `WebSocketSharp.Server.WebSocketSessionManager`).
The `WebSocketBehavior.Sessions.Broadcast` method broadcasts a data to every client in the service.
#### Step 3 #### #### Step 3 ####
@ -348,13 +350,13 @@ wssv.AddWebSocketService<Chat> ("/Chat");
wssv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!")); wssv.AddWebSocketService<Chat> ("/ChatWithNyan", () => new Chat (" Nyan!"));
``` ```
You can add any WebSocket service to your `WebSocketServer` with the specified path to the service, using the `WebSocketServer.AddWebSocketService<TWithNew> (string)` or `WebSocketServer.AddWebSocketService<T> (string, Func<T>)` method. You can add any WebSocket service to your `WebSocketServer` with the specified behavior and path to the service, using the `WebSocketServer.AddWebSocketService<TBehaviorWithNew> (string)` or `WebSocketServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>)` method.
The type of `TWithNew` must inherit the `WebSocketService` class and must have a public parameterless constructor. The type of `TBehaviorWithNew` must inherit the `WebSocketBehavior` class, and must have a public parameterless constructor.
And also the type of `T` must inherit the `WebSocketService` class. And also the type of `TBehavior` must inherit the `WebSocketBehavior` class.
So you can use the classes created in **Step 2** to add the WebSocket service. So you can use the classes created in **Step 2** to add the service.
If you create an instance of the `WebSocketServer` class without a port number, the `WebSocketServer` set the port number to **80** automatically. So it's necessary to run with root permission. If you create an instance of the `WebSocketServer` class without a port number, the `WebSocketServer` set the port number to **80** automatically. So it's necessary to run with root permission.
@ -386,7 +388,7 @@ I modified the `System.Net.HttpListener`, `System.Net.HttpListenerContext`, and
So websocket-sharp provides the `WebSocketSharp.Server.HttpServer` class. So websocket-sharp provides the `WebSocketSharp.Server.HttpServer` class.
You can add any WebSocket service to your `HttpServer` with the specified path to the service, using the `HttpServer.AddWebSocketService<TWithNew> (string)` or `HttpServer.AddWebSocketService<T> (string, Func<T>)` method. You can add any WebSocket service to your `HttpServer` with the specified behavior and path to the service, using the `HttpServer.AddWebSocketService<TBehaviorWithNew> (string)` or `HttpServer.AddWebSocketService<TBehavior> (string, Func<TBehavior>)` method.
```cs ```cs
var httpsv = new HttpServer (4649); var httpsv = new HttpServer (4649);
@ -438,7 +440,7 @@ ws.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyE
}; };
``` ```
If you set this property to nothing, the validation does nothing with the server certificate and returns `true`. If you set this property to nothing, the validation does nothing with the server certificate, and returns `true`.
As a **WebSocket Server**, you should create an instance of the `WebSocketServer` or `HttpServer` class with some settings for the secure connection, like the following. As a **WebSocket Server**, you should create an instance of the `WebSocketServer` or `HttpServer` class with some settings for the secure connection, like the following.
@ -502,10 +504,10 @@ And if you would like to send the **Cookies** with the WebSocket connection requ
ws.SetCookie (new Cookie ("name", "nobita")); ws.SetCookie (new Cookie ("name", "nobita"));
``` ```
As a **WebSocket Server**, if you would like to get the **Query String** included in each WebSocket connection request, you should access the `WebSocketService.Context.QueryString` property, like the following. As a **WebSocket Server**, if you would like to get the **Query String** included in each WebSocket connection request, you should access the `WebSocketBehavior.Context.QueryString` property, like the following.
```cs ```cs
public class Chat : WebSocketService public class Chat : WebSocketBehavior
{ {
private string _name; private string _name;
... ...
@ -519,7 +521,7 @@ public class Chat : WebSocketService
} }
``` ```
And if you would like to check the **Origin header and Cookies** included in each WebSocket connection request, you should set each validation for the Origin header and Cookies in your `WebSocketService`, for example, using the `AddWebSocketService<T> (string, Func<T>)` method with initializing, like the following. And if you would like to check the **Origin header and Cookies** included in each WebSocket connection request, you should set each validation for the Origin header and Cookies in your `WebSocketBehavior`, for example, using the `AddWebSocketService<TBehavior> (string, Func<TBehavior>)` method with initializing, like the following.
```cs ```cs
wssv.AddWebSocketService<Chat> ( wssv.AddWebSocketService<Chat> (
@ -544,7 +546,7 @@ wssv.AddWebSocketService<Chat> (
}); });
``` ```
Also, if you would like to get each value of the Origin header and cookies, you should access each of the `WebSocketService.Context.Origin` and `WebSocketService.Context.CookieCollection` properties. Also, if you would like to get each value of the Origin header and cookies, you should access each of the `WebSocketBehavior.Context.Origin` and `WebSocketBehavior.Context.CookieCollection` properties.
### Connecting through the HTTP Proxy server ### ### Connecting through the HTTP Proxy server ###

View File

@ -558,53 +558,54 @@ namespace WebSocketSharp.Server
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Adds the specified typed WebSocket service with the specified <paramref name="path"/>. /// Adds a WebSocket service with the specified behavior and <paramref name="path"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method converts <paramref name="path"/> to URL-decoded string and removes <c>'/'</c> /// This method converts <paramref name="path"/> to URL-decoded string,
/// from tail end of <paramref name="path"/>. /// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </remarks> /// </remarks>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to add. /// A <see cref="string"/> that represents the absolute path to the service to add.
/// </param> /// </param>
/// <typeparam name="TWithNew"> /// <typeparam name="TBehaviorWithNew">
/// The type of the WebSocket service. The TWithNew must inherit /// The type of the behavior of the service to add. The TBehaviorWithNew must inherit
/// the <see cref="WebSocketService"/> class and must have a public parameterless constructor. /// the <see cref="WebSocketBehavior"/> class, and must have a public parameterless
/// constructor.
/// </typeparam> /// </typeparam>
public void AddWebSocketService<TWithNew> (string path) public void AddWebSocketService<TBehaviorWithNew> (string path)
where TWithNew : WebSocketService, new () where TBehaviorWithNew : WebSocketBehavior, new ()
{ {
AddWebSocketService<TWithNew> (path, () => new TWithNew ()); AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
} }
/// <summary> /// <summary>
/// Adds the specified typed WebSocket service with the specified <paramref name="path"/> and /// Adds the WebSocket service with the specified behavior, <paramref name="path"/>,
/// <paramref name="initializer"/>. /// and <paramref name="initializer"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// This method converts <paramref name="path"/> to URL-decoded string and /// This method converts <paramref name="path"/> to URL-decoded string,
/// removes <c>'/'</c> from tail end of <paramref name="path"/>. /// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </para> /// </para>
/// <para> /// <para>
/// <paramref name="initializer"/> returns an initialized specified typed /// <paramref name="initializer"/> returns an initialized specified typed
/// <see cref="WebSocketService"/> instance. /// <see cref="WebSocketBehavior"/> instance.
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to add. /// A <see cref="string"/> that represents the absolute path to the service to add.
/// </param> /// </param>
/// <param name="initializer"> /// <param name="initializer">
/// A Func&lt;T&gt; delegate that references the method used to initialize a new specified /// A Func&lt;T&gt; delegate that references the method used to initialize a new specified
/// typed <see cref="WebSocketService"/> instance (a new <see cref="IWebSocketSession"/> /// typed <see cref="WebSocketBehavior"/> instance (a new <see cref="IWebSocketSession"/>
/// instance). /// instance).
/// </param> /// </param>
/// <typeparam name="T"> /// <typeparam name="TBehavior">
/// The type of the WebSocket service. The T must inherit the <see cref="WebSocketService"/> /// The type of the behavior of the service to add. The TBehavior must inherit
/// class. /// the <see cref="WebSocketBehavior"/> class.
/// </typeparam> /// </typeparam>
public void AddWebSocketService<T> (string path, Func<T> initializer) public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
where T : WebSocketService where TBehavior : WebSocketBehavior
{ {
var msg = path.CheckIfValidServicePath () ?? var msg = path.CheckIfValidServicePath () ??
(initializer == null ? "'initializer' is null." : null); (initializer == null ? "'initializer' is null." : null);
@ -614,7 +615,7 @@ namespace WebSocketSharp.Server
return; return;
} }
var host = new WebSocketServiceHost<T> (path, initializer, _logger); var host = new WebSocketServiceHost<TBehavior> (path, initializer, _logger);
if (!KeepClean) if (!KeepClean)
host.KeepClean = false; host.KeepClean = false;
@ -646,15 +647,14 @@ namespace WebSocketSharp.Server
/// Removes the WebSocket service with the specified <paramref name="path"/>. /// Removes the WebSocket service with the specified <paramref name="path"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method converts <paramref name="path"/> to URL-decoded string and /// This method converts <paramref name="path"/> to URL-decoded string,
/// removes <c>'/'</c> from tail end of <paramref name="path"/>. /// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// <c>true</c> if the WebSocket service is successfully found and removed; /// <c>true</c> if the service is successfully found and removed; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the service to find.
/// </param> /// </param>
public bool RemoveWebSocketService (string path) public bool RemoveWebSocketService (string path)
{ {

View File

@ -74,8 +74,8 @@ namespace WebSocketSharp.Server
/// Gets the state of the <see cref="WebSocket"/> used in the session. /// Gets the state of the <see cref="WebSocket"/> used in the session.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of the /// One of the <see cref="WebSocketState"/> enum values, indicates the state of
/// <see cref="WebSocket"/> used in the session. /// the <see cref="WebSocket"/> used in the session.
/// </value> /// </value>
WebSocketState State { get; } WebSocketState State { get; }

View File

@ -1,6 +1,6 @@
#region License #region License
/* /*
* WebSocketService.cs * WebSocketBehavior.cs
* *
* The MIT License * The MIT License
* *
@ -34,18 +34,19 @@ using WebSocketSharp.Net.WebSockets;
namespace WebSocketSharp.Server namespace WebSocketSharp.Server
{ {
/// <summary> /// <summary>
/// Exposes a set of the methods and properties for a WebSocket service provided by the /// Exposes the methods and properties used to define the behavior of a WebSocket service
/// <see cref="HttpServer"/> or <see cref="WebSocketServer"/>. /// provided by the <see cref="HttpServer"/> or <see cref="WebSocketServer"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// The WebSocketService class is an abstract class. /// The WebSocketBehavior class is an abstract class.
/// </remarks> /// </remarks>
public abstract class WebSocketService : IWebSocketSession public abstract class WebSocketBehavior : IWebSocketSession
{ {
#region Private Fields #region Private Fields
private WebSocketContext _context; private WebSocketContext _context;
private Func<CookieCollection, CookieCollection, bool> _cookiesValidator; private Func<CookieCollection, CookieCollection, bool> _cookiesValidator;
private string _id;
private Func<string, bool> _originValidator; private Func<string, bool> _originValidator;
private string _protocol; private string _protocol;
private WebSocketSessionManager _sessions; private WebSocketSessionManager _sessions;
@ -57,9 +58,9 @@ namespace WebSocketSharp.Server
#region Protected Constructors #region Protected Constructors
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebSocketService"/> class. /// Initializes a new instance of the <see cref="WebSocketBehavior"/> class.
/// </summary> /// </summary>
protected WebSocketService () protected WebSocketBehavior ()
{ {
_start = DateTime.MaxValue; _start = DateTime.MaxValue;
} }
@ -71,11 +72,9 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Gets the logging functions. /// Gets the logging functions.
/// </summary> /// </summary>
/// <remarks>
/// This property is available after the WebSocket connection has been established.
/// </remarks>
/// <value> /// <value>
/// A <see cref="Logger"/> that provides the logging functions. /// A <see cref="Logger"/> that provides the logging functions, or <see langword="null"/>
/// if the WebSocket connection isn't established.
/// </value> /// </value>
protected Logger Log { protected Logger Log {
get { get {
@ -88,11 +87,9 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Gets the access to the sessions in the WebSocket service. /// Gets the access to the sessions in the WebSocket service.
/// </summary> /// </summary>
/// <remarks>
/// This property is available after the WebSocket connection has been established.
/// </remarks>
/// <value> /// <value>
/// A <see cref="WebSocketSessionManager"/> that provides the access to the sessions. /// A <see cref="WebSocketSessionManager"/> that provides the access to the sessions,
/// or <see langword="null"/> if the WebSocket connection isn't established.
/// </value> /// </value>
protected WebSocketSessionManager Sessions { protected WebSocketSessionManager Sessions {
get { get {
@ -108,7 +105,8 @@ namespace WebSocketSharp.Server
/// Gets the information in the current connection request to the WebSocket service. /// Gets the information in the current connection request to the WebSocket service.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketContext"/> that provides the access to the current connection request. /// A <see cref="WebSocketContext"/> that provides the access to the current connection request,
/// or <see langword="null"/> if the WebSocket connection isn't established.
/// </value> /// </value>
public WebSocketContext Context { public WebSocketContext Context {
get { get {
@ -128,11 +126,11 @@ namespace WebSocketSharp.Server
/// <para> /// <para>
/// A <c>Func&lt;CookieCollection, CookieCollection, bool&gt;</c> delegate that references /// A <c>Func&lt;CookieCollection, CookieCollection, bool&gt;</c> delegate that references
/// the method(s) used to validate the cookies. 1st <see cref="CookieCollection"/> passed to /// the method(s) used to validate the cookies. 1st <see cref="CookieCollection"/> passed to
/// this delegate contains the cookies to validate, if any. 2nd <see cref="CookieCollection"/> /// this delegate contains the cookies to validate if any. 2nd <see cref="CookieCollection"/>
/// passed to this delegate receives the cookies to send to the client. /// passed to this delegate receives the cookies to send to the client.
/// </para> /// </para>
/// <para> /// <para>
/// This delegate should return <c>true</c> if the cookies are valid; otherwise, <c>false</c>. /// This delegate should return <c>true</c> if the cookies are valid.
/// </para> /// </para>
/// <para> /// <para>
/// The default value is <see langword="null"/>, and it does nothing to validate. /// The default value is <see langword="null"/>, and it does nothing to validate.
@ -152,10 +150,13 @@ namespace WebSocketSharp.Server
/// Gets the unique ID of the current session. /// Gets the unique ID of the current session.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="string"/> that represents the unique ID of the current session. /// A <see cref="string"/> that represents the unique ID of the current session,
/// or <see langword="null"/> if the WebSocket connection isn't established.
/// </value> /// </value>
public string ID { public string ID {
get; private set; get {
return _id;
}
} }
/// <summary> /// <summary>
@ -170,11 +171,10 @@ namespace WebSocketSharp.Server
/// <para> /// <para>
/// A <c>Func&lt;string, bool&gt;</c> delegate that references the method(s) used to validate /// A <c>Func&lt;string, bool&gt;</c> delegate that references the method(s) used to validate
/// the origin header. A <see cref="string"/> passed to this delegate represents the value of /// the origin header. A <see cref="string"/> passed to this delegate represents the value of
/// the origin header to validate, if any. /// the origin header to validate if any.
/// </para> /// </para>
/// <para> /// <para>
/// This delegate should return <c>true</c> if the origin header is valid; otherwise, /// This delegate should return <c>true</c> if the origin header is valid.
/// <c>false</c>.
/// </para> /// </para>
/// <para> /// <para>
/// The default value is <see langword="null"/>, and it does nothing to validate. /// The default value is <see langword="null"/>, and it does nothing to validate.
@ -199,8 +199,8 @@ namespace WebSocketSharp.Server
/// </remarks> /// </remarks>
/// <value> /// <value>
/// <para> /// <para>
/// A <see cref="string"/> that represents the subprotocol if any. The default value is /// A <see cref="string"/> that represents the subprotocol if any.
/// <see cref="String.Empty"/>. /// The default value is <see cref="String.Empty"/>.
/// </para> /// </para>
/// <para> /// <para>
/// The value to set must be a token defined in /// The value to set must be a token defined in
@ -229,7 +229,8 @@ namespace WebSocketSharp.Server
/// Gets the time that the current session has started. /// Gets the time that the current session has started.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="DateTime"/> that represents the time that the current session has started. /// A <see cref="DateTime"/> that represents the time that the current session has started,
/// or <see cref="DateTime.MaxValue"/> if the WebSocket connection isn't established.
/// </value> /// </value>
public DateTime StartTime { public DateTime StartTime {
get { get {
@ -241,8 +242,8 @@ namespace WebSocketSharp.Server
/// Gets the state of the <see cref="WebSocket"/> used in the current session. /// Gets the state of the <see cref="WebSocket"/> used in the current session.
/// </summary> /// </summary>
/// <value> /// <value>
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of the /// One of the <see cref="WebSocketState"/> enum values, indicates the state of
/// <see cref="WebSocket"/> used in the current session. /// the <see cref="WebSocket"/> used in the current session.
/// </value> /// </value>
public WebSocketState State { public WebSocketState State {
get { get {
@ -268,10 +269,10 @@ namespace WebSocketSharp.Server
private void onClose (object sender, CloseEventArgs e) private void onClose (object sender, CloseEventArgs e)
{ {
if (ID == null) if (_id == null)
return; return;
_sessions.Remove (ID); _sessions.Remove (_id);
OnClose (e); OnClose (e);
} }
@ -287,8 +288,8 @@ namespace WebSocketSharp.Server
private void onOpen (object sender, EventArgs e) private void onOpen (object sender, EventArgs e)
{ {
ID = _sessions.Add (this); _id = _sessions.Add (this);
if (ID == null) { if (_id == null) {
_websocket.Close (CloseStatusCode.Away); _websocket.Close (CloseStatusCode.Away);
return; return;
} }
@ -330,22 +331,30 @@ namespace WebSocketSharp.Server
#region Protected Methods #region Protected Methods
/// <summary> /// <summary>
/// Calls the <see cref="OnError"/> method with the specified <paramref name="message"/>. /// Calls the <see cref="OnError"/> method with the specified <paramref name="message"/> and
/// <paramref name="exception"/>.
/// </summary> /// </summary>
/// <remarks>
/// This method doesn't call the <see cref="OnError"/> method if <paramref name="message"/> is
/// <see langword="null"/> or empty.
/// </remarks>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that represents the error message. /// A <see cref="string"/> that represents the error message.
/// </param> /// </param>
protected void Error (string message) /// <param name="exception">
/// An <see cref="Exception"/> instance that represents the cause of the error if any.
/// </param>
protected void Error (string message, Exception exception)
{ {
if (message != null && message.Length > 0) if (message != null && message.Length > 0)
OnError (new ErrorEventArgs (message)); OnError (new ErrorEventArgs (message, exception));
} }
/// <summary> /// <summary>
/// Called when the WebSocket connection used in the current session has been closed. /// Called when the WebSocket connection used in the current session has been closed.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// A <see cref="CloseEventArgs"/> that represents the event data received by /// A <see cref="CloseEventArgs"/> that represents the event data passed to
/// a <see cref="WebSocket.OnClose"/> event. /// a <see cref="WebSocket.OnClose"/> event.
/// </param> /// </param>
protected virtual void OnClose (CloseEventArgs e) protected virtual void OnClose (CloseEventArgs e)
@ -353,10 +362,10 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Called when the current session gets an error. /// Called when the <see cref="WebSocket"/> used in the current session gets an error.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// A <see cref="ErrorEventArgs"/> that represents the event data received by /// A <see cref="ErrorEventArgs"/> that represents the event data passed to
/// a <see cref="WebSocket.OnError"/> event. /// a <see cref="WebSocket.OnError"/> event.
/// </param> /// </param>
protected virtual void OnError (ErrorEventArgs e) protected virtual void OnError (ErrorEventArgs e)
@ -364,10 +373,10 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Called when the current session receives a message. /// Called when the <see cref="WebSocket"/> used in the current session receives a message.
/// </summary> /// </summary>
/// <param name="e"> /// <param name="e">
/// A <see cref="MessageEventArgs"/> that represents the event data received by /// A <see cref="MessageEventArgs"/> that represents the event data passed to
/// a <see cref="WebSocket.OnMessage"/> event. /// a <see cref="WebSocket.OnMessage"/> event.
/// </param> /// </param>
protected virtual void OnMessage (MessageEventArgs e) protected virtual void OnMessage (MessageEventArgs e)
@ -384,6 +393,9 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends a binary <paramref name="data"/> to the client on the current session. /// Sends a binary <paramref name="data"/> to the client on the current session.
/// </summary> /// </summary>
/// <remarks>
/// This method is available after the WebSocket connection has been established.
/// </remarks>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
@ -394,9 +406,12 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends the specified <paramref name="file"/> as a binary data to the client on the current /// Sends the specified <paramref name="file"/> as a binary data to the client
/// session. /// on the current session.
/// </summary> /// </summary>
/// <remarks>
/// This method is available after the WebSocket connection has been established.
/// </remarks>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> that represents the file to send. /// A <see cref="FileInfo"/> that represents the file to send.
/// </param> /// </param>
@ -409,6 +424,9 @@ namespace WebSocketSharp.Server
/// <summary> /// <summary>
/// Sends a text <paramref name="data"/> to the client on the current session. /// Sends a text <paramref name="data"/> to the client on the current session.
/// </summary> /// </summary>
/// <remarks>
/// This method is available after the WebSocket connection has been established.
/// </remarks>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
/// </param> /// </param>
@ -422,15 +440,20 @@ namespace WebSocketSharp.Server
/// Sends a binary <paramref name="data"/> asynchronously to the client on the current session. /// Sends a binary <paramref name="data"/> asynchronously to the client on the current session.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>
/// This method is available after the WebSocket connection has been established.
/// </para>
/// <para>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </para>
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// An array of <see cref="byte"/> that represents the binary data to send. /// An array of <see cref="byte"/> that represents the binary data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when the send is /// An <c>Action&lt;bool&gt;</c> delegate that references the method(s) called when the send is
/// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>. /// complete successfully.
/// </param> /// </param>
protected void SendAsync (byte[] data, Action<bool> completed) protected void SendAsync (byte[] data, Action<bool> completed)
{ {
@ -443,15 +466,20 @@ namespace WebSocketSharp.Server
/// on the current session. /// on the current session.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>
/// This method is available after the WebSocket connection has been established.
/// </para>
/// <para>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </para>
/// </remarks> /// </remarks>
/// <param name="file"> /// <param name="file">
/// A <see cref="FileInfo"/> that represents the file to send. /// A <see cref="FileInfo"/> that represents the file to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when the send is /// An <c>Action&lt;bool&gt;</c> delegate that references the method(s) called when the send is
/// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>. /// complete successfully.
/// </param> /// </param>
protected void SendAsync (FileInfo file, Action<bool> completed) protected void SendAsync (FileInfo file, Action<bool> completed)
{ {
@ -463,15 +491,20 @@ namespace WebSocketSharp.Server
/// Sends a text <paramref name="data"/> asynchronously to the client on the current session. /// Sends a text <paramref name="data"/> asynchronously to the client on the current session.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>
/// This method is available after the WebSocket connection has been established.
/// </para>
/// <para>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </para>
/// </remarks> /// </remarks>
/// <param name="data"> /// <param name="data">
/// A <see cref="string"/> that represents the text data to send. /// A <see cref="string"/> that represents the text data to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when the send is /// An <c>Action&lt;bool&gt;</c> delegate that references the method(s) called when the send is
/// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>. /// complete successfully.
/// </param> /// </param>
protected void SendAsync (string data, Action<bool> completed) protected void SendAsync (string data, Action<bool> completed)
{ {
@ -480,11 +513,16 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously to the client on /// Sends a binary data from the specified <see cref="Stream"/> asynchronously to the client
/// the current session. /// on the current session.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>
/// This method is available after the WebSocket connection has been established.
/// </para>
/// <para>
/// This method doesn't wait for the send to be complete. /// This method doesn't wait for the send to be complete.
/// </para>
/// </remarks> /// </remarks>
/// <param name="stream"> /// <param name="stream">
/// A <see cref="Stream"/> from which contains the binary data to send. /// A <see cref="Stream"/> from which contains the binary data to send.
@ -493,9 +531,9 @@ namespace WebSocketSharp.Server
/// An <see cref="int"/> that represents the number of bytes to send. /// An <see cref="int"/> that represents the number of bytes to send.
/// </param> /// </param>
/// <param name="completed"> /// <param name="completed">
/// An Action&lt;bool&gt; delegate that references the method(s) called when the send is /// An <c>Action&lt;bool&gt;</c> delegate that references the method(s) called when the send is
/// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is /// complete. A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>. /// complete successfully.
/// </param> /// </param>
protected void SendAsync (Stream stream, int length, Action<bool> completed) protected void SendAsync (Stream stream, int length, Action<bool> completed)
{ {

View File

@ -675,53 +675,54 @@ namespace WebSocketSharp.Server
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Adds the specified typed WebSocket service with the specified <paramref name="path"/>. /// Adds a WebSocket service with the specified behavior and <paramref name="path"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method converts <paramref name="path"/> to URL-decoded string and removes <c>'/'</c> /// This method converts <paramref name="path"/> to URL-decoded string,
/// from tail end of <paramref name="path"/>. /// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </remarks> /// </remarks>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to add. /// A <see cref="string"/> that represents the absolute path to the service to add.
/// </param> /// </param>
/// <typeparam name="TWithNew"> /// <typeparam name="TBehaviorWithNew">
/// The type of the WebSocket service. The TWithNew must inherit /// The type of the behavior of the service to add. The TBehaviorWithNew must inherit
/// the <see cref="WebSocketService"/> class and must have a public parameterless constructor. /// the <see cref="WebSocketBehavior"/> class, and must have a public parameterless
/// constructor.
/// </typeparam> /// </typeparam>
public void AddWebSocketService<TWithNew> (string path) public void AddWebSocketService<TBehaviorWithNew> (string path)
where TWithNew : WebSocketService, new () where TBehaviorWithNew : WebSocketBehavior, new ()
{ {
AddWebSocketService<TWithNew> (path, () => new TWithNew ()); AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
} }
/// <summary> /// <summary>
/// Adds the specified typed WebSocket service with the specified <paramref name="path"/> and /// Adds a WebSocket service with the specified behavior, <paramref name="path"/>,
/// <paramref name="initializer"/>. /// and <paramref name="initializer"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para> /// <para>
/// This method converts <paramref name="path"/> to URL-decoded string and /// This method converts <paramref name="path"/> to URL-decoded string,
/// removes <c>'/'</c> from tail end of <paramref name="path"/>. /// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </para> /// </para>
/// <para> /// <para>
/// <paramref name="initializer"/> returns an initialized specified typed /// <paramref name="initializer"/> returns an initialized specified typed
/// <see cref="WebSocketService"/> instance. /// <see cref="WebSocketBehavior"/> instance.
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to add. /// A <see cref="string"/> that represents the absolute path to the service to add.
/// </param> /// </param>
/// <param name="initializer"> /// <param name="initializer">
/// A Func&lt;T&gt; delegate that references the method used to initialize a new specified /// A Func&lt;T&gt; delegate that references the method used to initialize a new specified
/// typed <see cref="WebSocketService"/> instance (a new <see cref="IWebSocketSession"/> /// typed <see cref="WebSocketBehavior"/> instance (a new <see cref="IWebSocketSession"/>
/// instance). /// instance).
/// </param> /// </param>
/// <typeparam name="T"> /// <typeparam name="TBehavior">
/// The type of the WebSocket service. The T must inherit the <see cref="WebSocketService"/> /// The type of the behavior of the service to add. The TBehavior must inherit
/// class. /// the <see cref="WebSocketBehavior"/> class.
/// </typeparam> /// </typeparam>
public void AddWebSocketService<T> (string path, Func<T> initializer) public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
where T : WebSocketService where TBehavior : WebSocketBehavior
{ {
var msg = path.CheckIfValidServicePath () ?? var msg = path.CheckIfValidServicePath () ??
(initializer == null ? "'initializer' is null." : null); (initializer == null ? "'initializer' is null." : null);
@ -731,7 +732,7 @@ namespace WebSocketSharp.Server
return; return;
} }
var host = new WebSocketServiceHost<T> (path, initializer, _logger); var host = new WebSocketServiceHost<TBehavior> (path, initializer, _logger);
if (!KeepClean) if (!KeepClean)
host.KeepClean = false; host.KeepClean = false;
@ -742,15 +743,14 @@ namespace WebSocketSharp.Server
/// Removes the WebSocket service with the specified <paramref name="path"/>. /// Removes the WebSocket service with the specified <paramref name="path"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method converts <paramref name="path"/> to URL-decoded string and /// This method converts <paramref name="path"/> to URL-decoded string,
/// removes <c>'/'</c> from tail end of <paramref name="path"/>. /// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
/// </remarks> /// </remarks>
/// <returns> /// <returns>
/// <c>true</c> if the WebSocket service is successfully found and removed; /// <c>true</c> if the service is successfully found and removed; otherwise, <c>false</c>.
/// otherwise, <c>false</c>.
/// </returns> /// </returns>
/// <param name="path"> /// <param name="path">
/// A <see cref="string"/> that represents the absolute path to the WebSocket service to find. /// A <see cref="string"/> that represents the absolute path to the service to find.
/// </param> /// </param>
public bool RemoveWebSocketService (string path) public bool RemoveWebSocketService (string path)
{ {

View File

@ -66,7 +66,7 @@ namespace WebSocketSharp.Server
/// sessions periodically. /// sessions periodically.
/// </summary> /// </summary>
/// <value> /// <value>
/// <c>true</c> if the WebSocket service cleans up the inactive sessions periodically; /// <c>true</c> if the service cleans up the inactive sessions periodically;
/// otherwise, <c>false</c>. /// otherwise, <c>false</c>.
/// </value> /// </value>
public abstract bool KeepClean { get; set; } public abstract bool KeepClean { get; set; }
@ -75,7 +75,7 @@ namespace WebSocketSharp.Server
/// Gets the path to the WebSocket service. /// Gets the path to the WebSocket service.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="string"/> that represents the absolute path to the WebSocket service. /// A <see cref="string"/> that represents the absolute path to the service.
/// </value> /// </value>
public abstract string Path { get; } public abstract string Path { get; }
@ -83,15 +83,15 @@ namespace WebSocketSharp.Server
/// Gets the access to the sessions in the WebSocket service. /// Gets the access to the sessions in the WebSocket service.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="WebSocketSessionManager"/> that manages the sessions. /// A <see cref="WebSocketSessionManager"/> that manages the sessions in the service.
/// </value> /// </value>
public abstract WebSocketSessionManager Sessions { get; } public abstract WebSocketSessionManager Sessions { get; }
/// <summary> /// <summary>
/// Gets the type of the WebSocket service. /// Gets the <see cref="System.Type"/> of the behavior of the WebSocket service.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="System.Type"/> that represents the type of the WebSocket service. /// A <see cref="System.Type"/> that represents the type of the behavior of the service.
/// </value> /// </value>
public abstract Type Type { get; } public abstract Type Type { get; }
@ -112,19 +112,19 @@ namespace WebSocketSharp.Server
/// Creates a new session in the WebSocket service. /// Creates a new session in the WebSocket service.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// A <see cref="WebSocketService"/> instance that represents a new session. /// A <see cref="WebSocketBehavior"/> instance that represents a new session.
/// </returns> /// </returns>
protected abstract WebSocketService CreateSession (); protected abstract WebSocketBehavior CreateSession ();
#endregion #endregion
} }
internal class WebSocketServiceHost<T> : WebSocketServiceHost internal class WebSocketServiceHost<TBehavior> : WebSocketServiceHost
where T : WebSocketService where TBehavior : WebSocketBehavior
{ {
#region Private Fields #region Private Fields
private Func<T> _initializer; private Func<TBehavior> _initializer;
private string _path; private string _path;
private WebSocketSessionManager _sessions; private WebSocketSessionManager _sessions;
@ -132,7 +132,7 @@ namespace WebSocketSharp.Server
#region Internal Constructors #region Internal Constructors
internal WebSocketServiceHost (string path, Func<T> initializer, Logger logger) internal WebSocketServiceHost (string path, Func<TBehavior> initializer, Logger logger)
{ {
_path = HttpUtility.UrlDecode (path).TrimEndSlash (); _path = HttpUtility.UrlDecode (path).TrimEndSlash ();
_initializer = initializer; _initializer = initializer;
@ -167,7 +167,7 @@ namespace WebSocketSharp.Server
public override Type Type { public override Type Type {
get { get {
return typeof (T); return typeof (TBehavior);
} }
} }
@ -175,7 +175,7 @@ namespace WebSocketSharp.Server
#region Protected Methods #region Protected Methods
protected override WebSocketService CreateSession () protected override WebSocketBehavior CreateSession ()
{ {
return _initializer (); return _initializer ();
} }

View File

@ -69,7 +69,6 @@
<Compile Include="ErrorEventArgs.cs" /> <Compile Include="ErrorEventArgs.cs" />
<Compile Include="WebSocket.cs" /> <Compile Include="WebSocket.cs" />
<Compile Include="Server\WebSocketServer.cs" /> <Compile Include="Server\WebSocketServer.cs" />
<Compile Include="Server\WebSocketService.cs" />
<Compile Include="Net\AuthenticationSchemes.cs" /> <Compile Include="Net\AuthenticationSchemes.cs" />
<Compile Include="Net\ChunkStream.cs" /> <Compile Include="Net\ChunkStream.cs" />
<Compile Include="Net\Cookie.cs" /> <Compile Include="Net\Cookie.cs" />
@ -134,6 +133,7 @@
<Compile Include="HttpBase.cs" /> <Compile Include="HttpBase.cs" />
<Compile Include="HttpRequest.cs" /> <Compile Include="HttpRequest.cs" />
<Compile Include="HttpResponse.cs" /> <Compile Include="HttpResponse.cs" />
<Compile Include="Server\WebSocketBehavior.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>