Renamed the WebSocketService class to the WebSocketBehavior class
This commit is contained in:
parent
73bfd1dbab
commit
5109d88ebb
@ -5,7 +5,7 @@ using WebSocketSharp.Server;
|
||||
|
||||
namespace Example2
|
||||
{
|
||||
public class Chat : WebSocketService
|
||||
public class Chat : WebSocketBehavior
|
||||
{
|
||||
private static int _num = 0;
|
||||
|
||||
|
@ -4,7 +4,7 @@ using WebSocketSharp.Server;
|
||||
|
||||
namespace Example2
|
||||
{
|
||||
public class Echo : WebSocketService
|
||||
public class Echo : WebSocketBehavior
|
||||
{
|
||||
protected override void OnMessage (MessageEventArgs e)
|
||||
{
|
||||
|
@ -5,7 +5,7 @@ using WebSocketSharp.Server;
|
||||
|
||||
namespace Example3
|
||||
{
|
||||
public class Chat : WebSocketService
|
||||
public class Chat : WebSocketBehavior
|
||||
{
|
||||
private static int _num = 0;
|
||||
|
||||
|
@ -4,7 +4,7 @@ using WebSocketSharp.Server;
|
||||
|
||||
namespace Example3
|
||||
{
|
||||
public class Echo : WebSocketService
|
||||
public class Echo : WebSocketBehavior
|
||||
{
|
||||
protected override void OnMessage (MessageEventArgs e)
|
||||
{
|
||||
|
42
README.md
42
README.md
@ -243,7 +243,7 @@ using WebSocketSharp.Server;
|
||||
|
||||
namespace Example
|
||||
{
|
||||
public class Laputa : WebSocketService
|
||||
public class Laputa : WebSocketBehavior
|
||||
{
|
||||
protected override void OnMessage (MessageEventArgs e)
|
||||
{
|
||||
@ -277,11 +277,11 @@ Required namespace.
|
||||
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 ####
|
||||
|
||||
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,
|
||||
|
||||
@ -290,7 +290,7 @@ using System;
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Server;
|
||||
|
||||
public class Echo : WebSocketService
|
||||
public class Echo : WebSocketBehavior
|
||||
{
|
||||
protected override void OnMessage (MessageEventArgs e)
|
||||
{
|
||||
@ -306,7 +306,7 @@ using System;
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Server;
|
||||
|
||||
public class Chat : WebSocketService
|
||||
public class Chat : WebSocketBehavior
|
||||
{
|
||||
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 ####
|
||||
|
||||
@ -348,13 +350,13 @@ wssv.AddWebSocketService<Chat> ("/Chat");
|
||||
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.
|
||||
|
||||
@ -386,7 +388,7 @@ I modified the `System.Net.HttpListener`, `System.Net.HttpListenerContext`, and
|
||||
|
||||
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
|
||||
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.
|
||||
|
||||
@ -502,10 +504,10 @@ And if you would like to send the **Cookies** with the WebSocket connection requ
|
||||
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
|
||||
public class Chat : WebSocketService
|
||||
public class Chat : WebSocketBehavior
|
||||
{
|
||||
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
|
||||
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 ###
|
||||
|
||||
|
@ -558,53 +558,54 @@ namespace WebSocketSharp.Server
|
||||
#region Public Methods
|
||||
|
||||
/// <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>
|
||||
/// <remarks>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string and removes <c>'/'</c>
|
||||
/// from tail end of <paramref name="path"/>.
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </remarks>
|
||||
/// <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>
|
||||
/// <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 name="TBehaviorWithNew">
|
||||
/// The type of the behavior of the service to add. The TBehaviorWithNew must inherit
|
||||
/// the <see cref="WebSocketBehavior"/> class, and must have a public parameterless
|
||||
/// constructor.
|
||||
/// </typeparam>
|
||||
public void AddWebSocketService<TWithNew> (string path)
|
||||
where TWithNew : WebSocketService, new ()
|
||||
public void AddWebSocketService<TBehaviorWithNew> (string path)
|
||||
where TBehaviorWithNew : WebSocketBehavior, new ()
|
||||
{
|
||||
AddWebSocketService<TWithNew> (path, () => new TWithNew ());
|
||||
AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified typed WebSocket service with the specified <paramref name="path"/> and
|
||||
/// <paramref name="initializer"/>.
|
||||
/// Adds the WebSocket service with the specified behavior, <paramref name="path"/>,
|
||||
/// and <paramref name="initializer"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string and
|
||||
/// removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <paramref name="initializer"/> returns an initialized specified typed
|
||||
/// <see cref="WebSocketService"/> instance.
|
||||
/// <see cref="WebSocketBehavior"/> instance.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <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 name="initializer">
|
||||
/// A Func<T> 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).
|
||||
/// </param>
|
||||
/// <typeparam name="T">
|
||||
/// The type of the WebSocket service. The T must inherit the <see cref="WebSocketService"/>
|
||||
/// class.
|
||||
/// <typeparam name="TBehavior">
|
||||
/// The type of the behavior of the service to add. The TBehavior must inherit
|
||||
/// the <see cref="WebSocketBehavior"/> class.
|
||||
/// </typeparam>
|
||||
public void AddWebSocketService<T> (string path, Func<T> initializer)
|
||||
where T : WebSocketService
|
||||
public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
|
||||
where TBehavior : WebSocketBehavior
|
||||
{
|
||||
var msg = path.CheckIfValidServicePath () ??
|
||||
(initializer == null ? "'initializer' is null." : null);
|
||||
@ -614,7 +615,7 @@ namespace WebSocketSharp.Server
|
||||
return;
|
||||
}
|
||||
|
||||
var host = new WebSocketServiceHost<T> (path, initializer, _logger);
|
||||
var host = new WebSocketServiceHost<TBehavior> (path, initializer, _logger);
|
||||
if (!KeepClean)
|
||||
host.KeepClean = false;
|
||||
|
||||
@ -646,15 +647,14 @@ namespace WebSocketSharp.Server
|
||||
/// Removes the WebSocket service with the specified <paramref name="path"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string and
|
||||
/// removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </remarks>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the WebSocket service is successfully found and removed;
|
||||
/// otherwise, <c>false</c>.
|
||||
/// <c>true</c> if the service is successfully found and removed; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
/// <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>
|
||||
public bool RemoveWebSocketService (string path)
|
||||
{
|
||||
|
@ -74,8 +74,8 @@ namespace WebSocketSharp.Server
|
||||
/// Gets the state of the <see cref="WebSocket"/> used in the session.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of the
|
||||
/// <see cref="WebSocket"/> used in the session.
|
||||
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of
|
||||
/// the <see cref="WebSocket"/> used in the session.
|
||||
/// </value>
|
||||
WebSocketState State { get; }
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#region License
|
||||
/*
|
||||
* WebSocketService.cs
|
||||
* WebSocketBehavior.cs
|
||||
*
|
||||
* The MIT License
|
||||
*
|
||||
@ -34,18 +34,19 @@ using WebSocketSharp.Net.WebSockets;
|
||||
namespace WebSocketSharp.Server
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes a set of the methods and properties for a WebSocket service provided by the
|
||||
/// <see cref="HttpServer"/> or <see cref="WebSocketServer"/>.
|
||||
/// Exposes the methods and properties used to define the behavior of a WebSocket service
|
||||
/// provided by the <see cref="HttpServer"/> or <see cref="WebSocketServer"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The WebSocketService class is an abstract class.
|
||||
/// The WebSocketBehavior class is an abstract class.
|
||||
/// </remarks>
|
||||
public abstract class WebSocketService : IWebSocketSession
|
||||
public abstract class WebSocketBehavior : IWebSocketSession
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private WebSocketContext _context;
|
||||
private Func<CookieCollection, CookieCollection, bool> _cookiesValidator;
|
||||
private string _id;
|
||||
private Func<string, bool> _originValidator;
|
||||
private string _protocol;
|
||||
private WebSocketSessionManager _sessions;
|
||||
@ -57,9 +58,9 @@ namespace WebSocketSharp.Server
|
||||
#region Protected Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WebSocketService"/> class.
|
||||
/// Initializes a new instance of the <see cref="WebSocketBehavior"/> class.
|
||||
/// </summary>
|
||||
protected WebSocketService ()
|
||||
protected WebSocketBehavior ()
|
||||
{
|
||||
_start = DateTime.MaxValue;
|
||||
}
|
||||
@ -71,11 +72,9 @@ namespace WebSocketSharp.Server
|
||||
/// <summary>
|
||||
/// Gets the logging functions.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This property is available after the WebSocket connection has been established.
|
||||
/// </remarks>
|
||||
/// <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>
|
||||
protected Logger Log {
|
||||
get {
|
||||
@ -88,11 +87,9 @@ namespace WebSocketSharp.Server
|
||||
/// <summary>
|
||||
/// Gets the access to the sessions in the WebSocket service.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This property is available after the WebSocket connection has been established.
|
||||
/// </remarks>
|
||||
/// <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>
|
||||
protected WebSocketSessionManager Sessions {
|
||||
get {
|
||||
@ -108,7 +105,8 @@ namespace WebSocketSharp.Server
|
||||
/// Gets the information in the current connection request to the WebSocket service.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public WebSocketContext Context {
|
||||
get {
|
||||
@ -128,11 +126,11 @@ namespace WebSocketSharp.Server
|
||||
/// <para>
|
||||
/// A <c>Func<CookieCollection, CookieCollection, bool></c> delegate that references
|
||||
/// 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.
|
||||
/// </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>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public string ID {
|
||||
get; private set;
|
||||
get {
|
||||
return _id;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -170,11 +171,10 @@ namespace WebSocketSharp.Server
|
||||
/// <para>
|
||||
/// A <c>Func<string, bool></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 to validate, if any.
|
||||
/// the origin header to validate if any.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This delegate should return <c>true</c> if the origin header is valid; otherwise,
|
||||
/// <c>false</c>.
|
||||
/// This delegate should return <c>true</c> if the origin header is valid.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The default value is <see langword="null"/>, and it does nothing to validate.
|
||||
@ -199,8 +199,8 @@ namespace WebSocketSharp.Server
|
||||
/// </remarks>
|
||||
/// <value>
|
||||
/// <para>
|
||||
/// A <see cref="string"/> that represents the subprotocol if any. The default value is
|
||||
/// <see cref="String.Empty"/>.
|
||||
/// A <see cref="string"/> that represents the subprotocol if any.
|
||||
/// The default value is <see cref="String.Empty"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public DateTime StartTime {
|
||||
get {
|
||||
@ -241,8 +242,8 @@ namespace WebSocketSharp.Server
|
||||
/// Gets the state of the <see cref="WebSocket"/> used in the current session.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of the
|
||||
/// <see cref="WebSocket"/> used in the current session.
|
||||
/// One of the <see cref="WebSocketState"/> enum values, indicates the state of
|
||||
/// the <see cref="WebSocket"/> used in the current session.
|
||||
/// </value>
|
||||
public WebSocketState State {
|
||||
get {
|
||||
@ -268,10 +269,10 @@ namespace WebSocketSharp.Server
|
||||
|
||||
private void onClose (object sender, CloseEventArgs e)
|
||||
{
|
||||
if (ID == null)
|
||||
if (_id == null)
|
||||
return;
|
||||
|
||||
_sessions.Remove (ID);
|
||||
_sessions.Remove (_id);
|
||||
OnClose (e);
|
||||
}
|
||||
|
||||
@ -287,8 +288,8 @@ namespace WebSocketSharp.Server
|
||||
|
||||
private void onOpen (object sender, EventArgs e)
|
||||
{
|
||||
ID = _sessions.Add (this);
|
||||
if (ID == null) {
|
||||
_id = _sessions.Add (this);
|
||||
if (_id == null) {
|
||||
_websocket.Close (CloseStatusCode.Away);
|
||||
return;
|
||||
}
|
||||
@ -330,22 +331,30 @@ namespace WebSocketSharp.Server
|
||||
#region Protected Methods
|
||||
|
||||
/// <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>
|
||||
/// <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">
|
||||
/// A <see cref="string"/> that represents the error message.
|
||||
/// </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)
|
||||
OnError (new ErrorEventArgs (message));
|
||||
OnError (new ErrorEventArgs (message, exception));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the WebSocket connection used in the current session has been closed.
|
||||
/// </summary>
|
||||
/// <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.
|
||||
/// </param>
|
||||
protected virtual void OnClose (CloseEventArgs e)
|
||||
@ -353,10 +362,10 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the current session gets an error.
|
||||
/// Called when the <see cref="WebSocket"/> used in the current session gets an error.
|
||||
/// </summary>
|
||||
/// <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.
|
||||
/// </param>
|
||||
protected virtual void OnError (ErrorEventArgs e)
|
||||
@ -364,10 +373,10 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the current session receives a message.
|
||||
/// Called when the <see cref="WebSocket"/> used in the current session receives a message.
|
||||
/// </summary>
|
||||
/// <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.
|
||||
/// </param>
|
||||
protected virtual void OnMessage (MessageEventArgs e)
|
||||
@ -384,6 +393,9 @@ namespace WebSocketSharp.Server
|
||||
/// <summary>
|
||||
/// Sends a binary <paramref name="data"/> to the client on the current session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method is available after the WebSocket connection has been established.
|
||||
/// </remarks>
|
||||
/// <param name="data">
|
||||
/// An array of <see cref="byte"/> that represents the binary data to send.
|
||||
/// </param>
|
||||
@ -394,9 +406,12 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the specified <paramref name="file"/> as a binary data to the client on the current
|
||||
/// session.
|
||||
/// Sends the specified <paramref name="file"/> as a binary data to the client
|
||||
/// on the current session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method is available after the WebSocket connection has been established.
|
||||
/// </remarks>
|
||||
/// <param name="file">
|
||||
/// A <see cref="FileInfo"/> that represents the file to send.
|
||||
/// </param>
|
||||
@ -409,6 +424,9 @@ namespace WebSocketSharp.Server
|
||||
/// <summary>
|
||||
/// Sends a text <paramref name="data"/> to the client on the current session.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method is available after the WebSocket connection has been established.
|
||||
/// </remarks>
|
||||
/// <param name="data">
|
||||
/// A <see cref="string"/> that represents the text data to send.
|
||||
/// </param>
|
||||
@ -422,15 +440,20 @@ namespace WebSocketSharp.Server
|
||||
/// Sends a binary <paramref name="data"/> asynchronously to the client on the current session.
|
||||
/// </summary>
|
||||
/// <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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="data">
|
||||
/// An array of <see cref="byte"/> that represents the binary data to send.
|
||||
/// </param>
|
||||
/// <param name="completed">
|
||||
/// An Action<bool> delegate that references the method(s) called when the send is
|
||||
/// An <c>Action<bool></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 successfully; otherwise, <c>false</c>.
|
||||
/// complete successfully.
|
||||
/// </param>
|
||||
protected void SendAsync (byte[] data, Action<bool> completed)
|
||||
{
|
||||
@ -443,15 +466,20 @@ namespace WebSocketSharp.Server
|
||||
/// on the current session.
|
||||
/// </summary>
|
||||
/// <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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="file">
|
||||
/// A <see cref="FileInfo"/> that represents the file to send.
|
||||
/// </param>
|
||||
/// <param name="completed">
|
||||
/// An Action<bool> delegate that references the method(s) called when the send is
|
||||
/// An <c>Action<bool></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 successfully; otherwise, <c>false</c>.
|
||||
/// complete successfully.
|
||||
/// </param>
|
||||
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.
|
||||
/// </summary>
|
||||
/// <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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="data">
|
||||
/// A <see cref="string"/> that represents the text data to send.
|
||||
/// </param>
|
||||
/// <param name="completed">
|
||||
/// An Action<bool> delegate that references the method(s) called when the send is
|
||||
/// An <c>Action<bool></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 successfully; otherwise, <c>false</c>.
|
||||
/// complete successfully.
|
||||
/// </param>
|
||||
protected void SendAsync (string data, Action<bool> completed)
|
||||
{
|
||||
@ -480,11 +513,16 @@ namespace WebSocketSharp.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously to the client on
|
||||
/// the current session.
|
||||
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously to the client
|
||||
/// on the current session.
|
||||
/// </summary>
|
||||
/// <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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="stream">
|
||||
/// 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.
|
||||
/// </param>
|
||||
/// <param name="completed">
|
||||
/// An Action<bool> delegate that references the method(s) called when the send is
|
||||
/// An <c>Action<bool></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 successfully; otherwise, <c>false</c>.
|
||||
/// complete successfully.
|
||||
/// </param>
|
||||
protected void SendAsync (Stream stream, int length, Action<bool> completed)
|
||||
{
|
@ -675,53 +675,54 @@ namespace WebSocketSharp.Server
|
||||
#region Public Methods
|
||||
|
||||
/// <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>
|
||||
/// <remarks>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string and removes <c>'/'</c>
|
||||
/// from tail end of <paramref name="path"/>.
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </remarks>
|
||||
/// <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>
|
||||
/// <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 name="TBehaviorWithNew">
|
||||
/// The type of the behavior of the service to add. The TBehaviorWithNew must inherit
|
||||
/// the <see cref="WebSocketBehavior"/> class, and must have a public parameterless
|
||||
/// constructor.
|
||||
/// </typeparam>
|
||||
public void AddWebSocketService<TWithNew> (string path)
|
||||
where TWithNew : WebSocketService, new ()
|
||||
public void AddWebSocketService<TBehaviorWithNew> (string path)
|
||||
where TBehaviorWithNew : WebSocketBehavior, new ()
|
||||
{
|
||||
AddWebSocketService<TWithNew> (path, () => new TWithNew ());
|
||||
AddWebSocketService<TBehaviorWithNew> (path, () => new TBehaviorWithNew ());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified typed WebSocket service with the specified <paramref name="path"/> and
|
||||
/// <paramref name="initializer"/>.
|
||||
/// Adds a WebSocket service with the specified behavior, <paramref name="path"/>,
|
||||
/// and <paramref name="initializer"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string and
|
||||
/// removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <paramref name="initializer"/> returns an initialized specified typed
|
||||
/// <see cref="WebSocketService"/> instance.
|
||||
/// <see cref="WebSocketBehavior"/> instance.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <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 name="initializer">
|
||||
/// A Func<T> 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).
|
||||
/// </param>
|
||||
/// <typeparam name="T">
|
||||
/// The type of the WebSocket service. The T must inherit the <see cref="WebSocketService"/>
|
||||
/// class.
|
||||
/// <typeparam name="TBehavior">
|
||||
/// The type of the behavior of the service to add. The TBehavior must inherit
|
||||
/// the <see cref="WebSocketBehavior"/> class.
|
||||
/// </typeparam>
|
||||
public void AddWebSocketService<T> (string path, Func<T> initializer)
|
||||
where T : WebSocketService
|
||||
public void AddWebSocketService<TBehavior> (string path, Func<TBehavior> initializer)
|
||||
where TBehavior : WebSocketBehavior
|
||||
{
|
||||
var msg = path.CheckIfValidServicePath () ??
|
||||
(initializer == null ? "'initializer' is null." : null);
|
||||
@ -731,7 +732,7 @@ namespace WebSocketSharp.Server
|
||||
return;
|
||||
}
|
||||
|
||||
var host = new WebSocketServiceHost<T> (path, initializer, _logger);
|
||||
var host = new WebSocketServiceHost<TBehavior> (path, initializer, _logger);
|
||||
if (!KeepClean)
|
||||
host.KeepClean = false;
|
||||
|
||||
@ -742,15 +743,14 @@ namespace WebSocketSharp.Server
|
||||
/// Removes the WebSocket service with the specified <paramref name="path"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string and
|
||||
/// removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// This method converts <paramref name="path"/> to URL-decoded string,
|
||||
/// and removes <c>'/'</c> from tail end of <paramref name="path"/>.
|
||||
/// </remarks>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the WebSocket service is successfully found and removed;
|
||||
/// otherwise, <c>false</c>.
|
||||
/// <c>true</c> if the service is successfully found and removed; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
/// <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>
|
||||
public bool RemoveWebSocketService (string path)
|
||||
{
|
||||
|
@ -66,7 +66,7 @@ namespace WebSocketSharp.Server
|
||||
/// sessions periodically.
|
||||
/// </summary>
|
||||
/// <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>.
|
||||
/// </value>
|
||||
public abstract bool KeepClean { get; set; }
|
||||
@ -75,7 +75,7 @@ namespace WebSocketSharp.Server
|
||||
/// Gets the path to the WebSocket service.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public abstract string Path { get; }
|
||||
|
||||
@ -83,15 +83,15 @@ namespace WebSocketSharp.Server
|
||||
/// Gets the access to the sessions in the WebSocket service.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// A <see cref="WebSocketSessionManager"/> that manages the sessions.
|
||||
/// A <see cref="WebSocketSessionManager"/> that manages the sessions in the service.
|
||||
/// </value>
|
||||
public abstract WebSocketSessionManager Sessions { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the WebSocket service.
|
||||
/// Gets the <see cref="System.Type"/> of the behavior of the WebSocket service.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
public abstract Type Type { get; }
|
||||
|
||||
@ -112,19 +112,19 @@ namespace WebSocketSharp.Server
|
||||
/// Creates a new session in the WebSocket service.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="WebSocketService"/> instance that represents a new session.
|
||||
/// A <see cref="WebSocketBehavior"/> instance that represents a new session.
|
||||
/// </returns>
|
||||
protected abstract WebSocketService CreateSession ();
|
||||
protected abstract WebSocketBehavior CreateSession ();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
internal class WebSocketServiceHost<T> : WebSocketServiceHost
|
||||
where T : WebSocketService
|
||||
internal class WebSocketServiceHost<TBehavior> : WebSocketServiceHost
|
||||
where TBehavior : WebSocketBehavior
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private Func<T> _initializer;
|
||||
private Func<TBehavior> _initializer;
|
||||
private string _path;
|
||||
private WebSocketSessionManager _sessions;
|
||||
|
||||
@ -132,7 +132,7 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#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 ();
|
||||
_initializer = initializer;
|
||||
@ -167,7 +167,7 @@ namespace WebSocketSharp.Server
|
||||
|
||||
public override Type Type {
|
||||
get {
|
||||
return typeof (T);
|
||||
return typeof (TBehavior);
|
||||
}
|
||||
}
|
||||
|
||||
@ -175,7 +175,7 @@ namespace WebSocketSharp.Server
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
protected override WebSocketService CreateSession ()
|
||||
protected override WebSocketBehavior CreateSession ()
|
||||
{
|
||||
return _initializer ();
|
||||
}
|
||||
|
@ -69,7 +69,6 @@
|
||||
<Compile Include="ErrorEventArgs.cs" />
|
||||
<Compile Include="WebSocket.cs" />
|
||||
<Compile Include="Server\WebSocketServer.cs" />
|
||||
<Compile Include="Server\WebSocketService.cs" />
|
||||
<Compile Include="Net\AuthenticationSchemes.cs" />
|
||||
<Compile Include="Net\ChunkStream.cs" />
|
||||
<Compile Include="Net\Cookie.cs" />
|
||||
@ -134,6 +133,7 @@
|
||||
<Compile Include="HttpBase.cs" />
|
||||
<Compile Include="HttpRequest.cs" />
|
||||
<Compile Include="HttpResponse.cs" />
|
||||
<Compile Include="Server\WebSocketBehavior.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
Loading…
Reference in New Issue
Block a user