Added some functions (e.g. CloseSession method) to WebSocketServiceHost<T> and other classes
This commit is contained in:
parent
c464cf8f52
commit
d98aad51e6
@ -164,6 +164,59 @@ namespace WebSocketSharp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidCloseData (this byte [] data)
|
||||||
|
{
|
||||||
|
return data.Length > 125
|
||||||
|
? "The payload length of a Close frame must be 125 bytes or less."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidCloseStatusCode (this ushort code)
|
||||||
|
{
|
||||||
|
return !code.IsCloseStatusCode ()
|
||||||
|
? "Invalid close status code."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidPingMessage (this string message)
|
||||||
|
{
|
||||||
|
return message != null && message.Length > 0 && Encoding.UTF8.GetBytes (message).Length > 125
|
||||||
|
? "The payload length of a Ping frame must be 125 bytes or less."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidSendData (this byte [] data)
|
||||||
|
{
|
||||||
|
return data == null
|
||||||
|
? "'data' must not be null."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidSendData (this string data)
|
||||||
|
{
|
||||||
|
return data == null
|
||||||
|
? "'data' must not be null."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidServicePath (this string servicePath)
|
||||||
|
{
|
||||||
|
return servicePath == null || servicePath.Length == 0
|
||||||
|
? "'servicePath' must not be null or empty."
|
||||||
|
: servicePath [0] != '/'
|
||||||
|
? "'servicePath' not absolute path."
|
||||||
|
: servicePath.IndexOfAny (new [] {'?', '#'}) != -1
|
||||||
|
? "'servicePath' must not contain either or both query and fragment components."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string CheckIfValidSessionID (this string id)
|
||||||
|
{
|
||||||
|
return id == null || id.Length == 0
|
||||||
|
? "'id' must not be null or empty."
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
internal static byte [] Compress (this byte [] value, CompressionMethod method)
|
internal static byte [] Compress (this byte [] value, CompressionMethod method)
|
||||||
{
|
{
|
||||||
return method == CompressionMethod.DEFLATE
|
return method == CompressionMethod.DEFLATE
|
||||||
@ -230,26 +283,26 @@ namespace WebSocketSharp
|
|||||||
return value == method.ToCompressionExtension ();
|
return value == method.ToCompressionExtension ();
|
||||||
}
|
}
|
||||||
|
|
||||||
// <summary>
|
/// <summary>
|
||||||
// Determines whether the specified <see cref="int"/> equals the specified <see cref="char"/>,
|
/// Determines whether the specified <see cref="int"/> equals the specified <see cref="char"/>,
|
||||||
// and invokes the specified Action<int> delegate at the same time.
|
/// and invokes the specified Action<int> delegate at the same time.
|
||||||
// </summary>
|
/// </summary>
|
||||||
// <returns>
|
/// <returns>
|
||||||
// <c>true</c> if <paramref name="value"/> equals <paramref name="c"/>; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="value"/> equals <paramref name="c"/>; otherwise, <c>false</c>.
|
||||||
// </returns>
|
/// </returns>
|
||||||
// <param name="value">
|
/// <param name="value">
|
||||||
// An <see cref="int"/> to compare.
|
/// An <see cref="int"/> to compare.
|
||||||
// </param>
|
/// </param>
|
||||||
// <param name="c">
|
/// <param name="c">
|
||||||
// A <see cref="char"/> to compare.
|
/// A <see cref="char"/> to compare.
|
||||||
// </param>
|
/// </param>
|
||||||
// <param name="action">
|
/// <param name="action">
|
||||||
// An Action<int> delegate that references the method(s) called at the same time as when comparing.
|
/// An Action<int> delegate that references the method(s) called at the same time as when comparing.
|
||||||
// An <see cref="int"/> parameter to pass to the method(s) is <paramref name="value"/>.
|
/// An <see cref="int"/> parameter to pass to the method(s) is <paramref name="value"/>.
|
||||||
// </param>
|
/// </param>
|
||||||
// <exception cref="ArgumentOutOfRangeException">
|
/// <exception cref="ArgumentOutOfRangeException">
|
||||||
// <paramref name="value"/> is less than 0, or greater than 255.
|
/// <paramref name="value"/> is less than 0, or greater than 255.
|
||||||
// </exception>
|
/// </exception>
|
||||||
internal static bool EqualsWith (this int value, char c, Action<int> action)
|
internal static bool EqualsWith (this int value, char c, Action<int> action)
|
||||||
{
|
{
|
||||||
if (value < 0 || value > 255)
|
if (value < 0 || value > 255)
|
||||||
@ -259,6 +312,31 @@ namespace WebSocketSharp
|
|||||||
return value == c - 0;
|
return value == c - 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the absolute path from the specified <see cref="Uri"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A <see cref="string"/> that contains the absolute path if it is successfully found;
|
||||||
|
/// otherwise, <see langword="null"/>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="uri">
|
||||||
|
/// A <see cref="Uri"/> that contains a URI to get the absolute path from.
|
||||||
|
/// </param>
|
||||||
|
internal static string GetAbsolutePath (this Uri uri)
|
||||||
|
{
|
||||||
|
if (uri.IsAbsoluteUri)
|
||||||
|
return uri.AbsolutePath;
|
||||||
|
|
||||||
|
var original = uri.OriginalString;
|
||||||
|
if (original [0] != '/')
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var i = original.IndexOfAny (new [] {'?', '#'});
|
||||||
|
return i > 0
|
||||||
|
? original.Substring (0, i)
|
||||||
|
: original;
|
||||||
|
}
|
||||||
|
|
||||||
internal static string GetNameInternal (this string nameAndValue, string separator)
|
internal static string GetNameInternal (this string nameAndValue, string separator)
|
||||||
{
|
{
|
||||||
int i = nameAndValue.IndexOf (separator);
|
int i = nameAndValue.IndexOf (separator);
|
||||||
@ -498,6 +576,14 @@ namespace WebSocketSharp
|
|||||||
return CompressionMethod.NONE;
|
return CompressionMethod.NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static string TrimEndSlash (this string value)
|
||||||
|
{
|
||||||
|
value = value.TrimEnd ('/');
|
||||||
|
return value.Length > 0
|
||||||
|
? value
|
||||||
|
: "/";
|
||||||
|
}
|
||||||
|
|
||||||
internal static string Unquote (this string value)
|
internal static string Unquote (this string value)
|
||||||
{
|
{
|
||||||
var start = value.IndexOf ('\"');
|
var start = value.IndexOf ('\"');
|
||||||
@ -536,7 +622,7 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public static bool Contains (this string value, params char [] chars)
|
public static bool Contains (this string value, params char [] chars)
|
||||||
{
|
{
|
||||||
return chars.Length == 0
|
return chars == null || chars.Length == 0
|
||||||
? true
|
? true
|
||||||
: value == null || value.Length == 0
|
: value == null || value.Length == 0
|
||||||
? false
|
? false
|
||||||
@ -639,35 +725,6 @@ namespace WebSocketSharp
|
|||||||
eventHandler (sender, e);
|
eventHandler (sender, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the absolute path from the specified <see cref="Uri"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// A <see cref="string"/> that contains the absolute path if gets successfully;
|
|
||||||
/// otherwise, <see langword="null"/>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="uri">
|
|
||||||
/// A <see cref="Uri"/> that contains a URI to get the absolute path from.
|
|
||||||
/// </param>
|
|
||||||
public static string GetAbsolutePath (this Uri uri)
|
|
||||||
{
|
|
||||||
if (uri == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
if (uri.IsAbsoluteUri)
|
|
||||||
return uri.AbsolutePath;
|
|
||||||
|
|
||||||
var uriString = uri.OriginalString;
|
|
||||||
var i = uriString.IndexOf ('/');
|
|
||||||
if (i != 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
i = uriString.IndexOfAny (new [] {'?', '#'});
|
|
||||||
return i > 0
|
|
||||||
? uriString.Substring (0, i)
|
|
||||||
: uriString;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the collection of cookies from the specified <see cref="NameValueCollection"/>.
|
/// Gets the collection of cookies from the specified <see cref="NameValueCollection"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -851,25 +908,23 @@ namespace WebSocketSharp
|
|||||||
/// </list>
|
/// </list>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="code"/> is in the allowable range of the WebSocket close status code; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="value"/> is in the allowable range of the WebSocket close status code;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="code">
|
/// <param name="value">
|
||||||
/// A <see cref="ushort"/> to test.
|
/// A <see cref="ushort"/> to test.
|
||||||
/// </param>
|
/// </param>
|
||||||
public static bool IsCloseStatusCode (this ushort code)
|
public static bool IsCloseStatusCode (this ushort value)
|
||||||
{
|
{
|
||||||
return code < 1000
|
return value > 999 && value < 5000;
|
||||||
? false
|
|
||||||
: code > 4999
|
|
||||||
? false
|
|
||||||
: true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the specified <see cref="string"/> is enclosed in the specified <see cref="char"/>.
|
/// Determines whether the specified <see cref="string"/> is enclosed in the specified <see cref="char"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="value"/> is enclosed in <paramref name="c"/>; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="value"/> is enclosed in <paramref name="c"/>;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="value">
|
/// <param name="value">
|
||||||
/// A <see cref="string"/> to test.
|
/// A <see cref="string"/> to test.
|
||||||
@ -879,9 +934,10 @@ namespace WebSocketSharp
|
|||||||
/// </param>
|
/// </param>
|
||||||
public static bool IsEnclosedIn (this string value, char c)
|
public static bool IsEnclosedIn (this string value, char c)
|
||||||
{
|
{
|
||||||
return value == null || value.Length == 0
|
return value != null &&
|
||||||
? false
|
value.Length > 1 &&
|
||||||
: value [0] == c && value [value.Length - 1] == c;
|
value [0] == c &&
|
||||||
|
value [value.Length - 1] == c;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -1021,44 +1077,6 @@ namespace WebSocketSharp
|
|||||||
request.Headers.Contains ("Connection", "Upgrade");
|
request.Headers.Contains ("Connection", "Upgrade");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines whether the specified <see cref="string"/> is valid absolute path.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// <c>true</c> if <paramref name="absPath"/> is valid absolute path; otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="absPath">
|
|
||||||
/// A <see cref="string"/> to test.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="message">
|
|
||||||
/// A <see cref="string"/> that receives a message if <paramref name="absPath"/> is invalid.
|
|
||||||
/// </param>
|
|
||||||
public static bool IsValidAbsolutePath (this string absPath, out string message)
|
|
||||||
{
|
|
||||||
if (absPath == null || absPath.Length == 0)
|
|
||||||
{
|
|
||||||
message = "Must not be null or empty.";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var i = absPath.IndexOf ('/');
|
|
||||||
if (i != 0)
|
|
||||||
{
|
|
||||||
message = "Not absolute path: " + absPath;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
i = absPath.IndexOfAny (new [] {'?', '#'});
|
|
||||||
if (i != -1)
|
|
||||||
{
|
|
||||||
message = "Must not contain either or both query and fragment components: " + absPath;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
message = String.Empty;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether the specified <see cref="string"/> is a URI string.
|
/// Determines whether the specified <see cref="string"/> is a URI string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -134,7 +134,8 @@ namespace WebSocketSharp.Server
|
|||||||
set {
|
set {
|
||||||
if (_listening)
|
if (_listening)
|
||||||
{
|
{
|
||||||
_logger.Error ("The value of Certificate property cannot be changed because the server has already been started.");
|
_logger.Error (
|
||||||
|
"The value of Certificate property cannot be changed because the server has already been started.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,7 +234,8 @@ namespace WebSocketSharp.Server
|
|||||||
set {
|
set {
|
||||||
if (_listening)
|
if (_listening)
|
||||||
{
|
{
|
||||||
_logger.Error ("The value of RootPath property cannot be changed because the server has already been started.");
|
_logger.Error (
|
||||||
|
"The value of RootPath property cannot be changed because the server has already been started.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,10 +388,9 @@ namespace WebSocketSharp.Server
|
|||||||
private bool processWebSocketRequest (HttpListenerContext context)
|
private bool processWebSocketRequest (HttpListenerContext context)
|
||||||
{
|
{
|
||||||
var wsContext = context.AcceptWebSocket ();
|
var wsContext = context.AcceptWebSocket ();
|
||||||
var path = wsContext.Path.UrlDecode ();
|
|
||||||
|
|
||||||
IWebSocketServiceHost host;
|
IWebSocketServiceHost host;
|
||||||
if (!_serviceHosts.TryGetServiceHost (path, out host))
|
if (!_serviceHosts.TryGetServiceHost (wsContext.Path, out host))
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = (int) HttpStatusCode.NotImplemented;
|
context.Response.StatusCode = (int) HttpStatusCode.NotImplemented;
|
||||||
return false;
|
return false;
|
||||||
@ -451,26 +452,19 @@ namespace WebSocketSharp.Server
|
|||||||
_receiveRequestThread.Start ();
|
_receiveRequestThread.Start ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stop (ushort code, string reason, bool ignoreArgs)
|
private void stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
if (!ignoreArgs)
|
var data = code.Append (reason);
|
||||||
|
var msg = data.CheckIfValidCloseData ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
var data = code.Append (reason);
|
_logger.Error (String.Format ("{0}\ncode: {1}\nreason: {2}", msg, code, reason));
|
||||||
if (data.Length > 125)
|
return;
|
||||||
{
|
|
||||||
_logger.Error (
|
|
||||||
String.Format ("The payload length of a Close frame must be 125 bytes or less.\ncode: {0}\nreason: {1}", code, reason));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_listener.Close ();
|
_listener.Close ();
|
||||||
_receiveRequestThread.Join (5 * 1000);
|
_receiveRequestThread.Join (5 * 1000);
|
||||||
if (ignoreArgs)
|
_serviceHosts.Stop (code, reason);
|
||||||
_serviceHosts.Stop ();
|
|
||||||
else
|
|
||||||
_serviceHosts.Stop (code, reason);
|
|
||||||
|
|
||||||
_listening = false;
|
_listening = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -481,6 +475,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified typed WebSocket service with the specified <paramref name="servicePath"/>.
|
/// Adds the specified typed WebSocket service with the specified <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method converts <paramref name="servicePath"/> to URL-decoded string and
|
||||||
|
/// removes <c>'/'</c> from tail end of <paramref name="servicePath"/>.
|
||||||
|
/// </remarks>
|
||||||
/// <param name="servicePath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path to the WebSocket service.
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service.
|
||||||
/// </param>
|
/// </param>
|
||||||
@ -490,10 +488,10 @@ namespace WebSocketSharp.Server
|
|||||||
public void AddWebSocketService<T> (string servicePath)
|
public void AddWebSocketService<T> (string servicePath)
|
||||||
where T : WebSocketService, new ()
|
where T : WebSocketService, new ()
|
||||||
{
|
{
|
||||||
string msg;
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
if (!servicePath.IsValidAbsolutePath (out msg))
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error (msg);
|
_logger.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -529,6 +527,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the WebSocket service with the specified <paramref name="servicePath"/>.
|
/// Removes the WebSocket service with the specified <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method converts <paramref name="servicePath"/> to URL-decoded string and
|
||||||
|
/// removes <c>'/'</c> from tail end of <paramref name="servicePath"/>.
|
||||||
|
/// </remarks>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if the WebSocket service is successfully found and removed; otherwise, <c>false</c>.
|
/// <c>true</c> if the WebSocket service is successfully found and removed; otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
@ -537,9 +539,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public bool RemoveWebSocketService (string servicePath)
|
public bool RemoveWebSocketService (string servicePath)
|
||||||
{
|
{
|
||||||
if (servicePath.IsNullOrEmpty ())
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error ("'servicePath' must not be null or empty.");
|
_logger.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -576,7 +579,10 @@ namespace WebSocketSharp.Server
|
|||||||
if (!_listening)
|
if (!_listening)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
stop (0, null, true);
|
_listener.Close ();
|
||||||
|
_receiveRequestThread.Join (5 * 1000);
|
||||||
|
_serviceHosts.Stop ();
|
||||||
|
_listening = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -594,13 +600,14 @@ namespace WebSocketSharp.Server
|
|||||||
if (!_listening)
|
if (!_listening)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!code.IsCloseStatusCode ())
|
var msg = code.CheckIfValidCloseStatusCode ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error ("Invalid status code for stop.\ncode: " + code);
|
_logger.Error (String.Format ("{0}\ncode: {1}", msg, code));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
stop (code, reason, false);
|
stop (code, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -618,7 +625,7 @@ namespace WebSocketSharp.Server
|
|||||||
if (!_listening)
|
if (!_listening)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
stop ((ushort) code, reason, false);
|
stop ((ushort) code, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -79,22 +79,81 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
void Broadcast (string data);
|
void Broadcast (string data);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends Pings to all clients of the WebSocket service host.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
|
/// indicating whether the WebSocket service host received a Pong from each client in a time.
|
||||||
|
/// </returns>
|
||||||
|
Dictionary<string, bool> Broadping ();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends Pings with the specified <paramref name="message"/> to all clients of
|
/// Sends Pings with the specified <paramref name="message"/> to all clients of
|
||||||
/// the WebSocket service host.
|
/// the WebSocket service host.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
/// indicating whether the WebSocket service host received the Pong from each client in a time.
|
/// indicating whether the WebSocket service host received a Pong from each client in a time.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
Dictionary<string, bool> Broadping (string message);
|
Dictionary<string, bool> Broadping (string message);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
void CloseSession (string id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>
|
||||||
|
/// and <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
void CloseSession (ushort code, string reason, string id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>
|
||||||
|
/// and <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
void CloseSession (CloseStatusCode code, string reason, string id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
|
/// </param>
|
||||||
|
bool PingTo (string id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
||||||
/// the specified ID.
|
/// the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
|
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
|
||||||
@ -104,12 +163,12 @@ namespace WebSocketSharp.Server
|
|||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
/// </param>
|
/// </param>
|
||||||
bool PingTo (string message, string id);
|
bool PingTo (string message, string id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a binary data to the client associated with the specified ID.
|
/// Sends a binary data to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -118,12 +177,12 @@ namespace WebSocketSharp.Server
|
|||||||
/// An array of <see cref="byte"/> that contains a binary data to send.
|
/// An array of <see cref="byte"/> that contains a binary data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
bool SendTo (byte [] data, string id);
|
bool SendTo (byte [] data, string id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a text data to the client associated with the specified ID.
|
/// Sends a text data to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -132,15 +191,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// A <see cref="string"/> that contains a text data to send.
|
/// A <see cref="string"/> that contains a text data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
bool SendTo (string data, string id);
|
bool SendTo (string data, string id);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts the WebSocket service host.
|
|
||||||
/// </summary>
|
|
||||||
void Start ();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stops the WebSocket service host.
|
/// Stops the WebSocket service host.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -199,10 +199,10 @@ namespace WebSocketSharp.Server
|
|||||||
private void stop (ushort code, string reason)
|
private void stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
var data = code.Append (reason);
|
var data = code.Append (reason);
|
||||||
if (data.Length > 125)
|
var msg = data.CheckIfValidCloseData ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format (
|
Log.Error (String.Format ("{0}\ncode: {1}\nreason: {2}", msg, code, reason));
|
||||||
"The payload length of a Close frame must be 125 bytes or less.\ncode: {0}\nreason: {1}", code, reason));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,9 +223,9 @@ namespace WebSocketSharp.Server
|
|||||||
protected override void AcceptWebSocket (TcpListenerWebSocketContext context)
|
protected override void AcceptWebSocket (TcpListenerWebSocketContext context)
|
||||||
{
|
{
|
||||||
var websocket = context.WebSocket;
|
var websocket = context.WebSocket;
|
||||||
var path = context.Path.UrlDecode ();
|
|
||||||
|
|
||||||
websocket.Log = Log;
|
websocket.Log = Log;
|
||||||
|
|
||||||
|
var path = context.Path;
|
||||||
IWebSocketServiceHost host;
|
IWebSocketServiceHost host;
|
||||||
if (!_serviceHosts.TryGetServiceHost (path, out host))
|
if (!_serviceHosts.TryGetServiceHost (path, out host))
|
||||||
{
|
{
|
||||||
@ -246,6 +246,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified typed WebSocket service with the specified <paramref name="servicePath"/>.
|
/// Adds the specified typed WebSocket service with the specified <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method converts <paramref name="servicePath"/> to URL-decoded string and
|
||||||
|
/// removes <c>'/'</c> from tail end of <paramref name="servicePath"/>.
|
||||||
|
/// </remarks>
|
||||||
/// <param name="servicePath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path to the WebSocket service.
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service.
|
||||||
/// </param>
|
/// </param>
|
||||||
@ -255,10 +259,10 @@ namespace WebSocketSharp.Server
|
|||||||
public void AddWebSocketService<T> (string servicePath)
|
public void AddWebSocketService<T> (string servicePath)
|
||||||
where T : WebSocketService, new ()
|
where T : WebSocketService, new ()
|
||||||
{
|
{
|
||||||
string msg;
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
if (!servicePath.IsValidAbsolutePath (out msg))
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (msg);
|
Log.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,6 +280,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the WebSocket service with the specified <paramref name="servicePath"/>.
|
/// Removes the WebSocket service with the specified <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method converts <paramref name="servicePath"/> to URL-decoded string and
|
||||||
|
/// removes <c>'/'</c> from tail end of <paramref name="servicePath"/>.
|
||||||
|
/// </remarks>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if the WebSocket service is successfully found and removed; otherwise, <c>false</c>.
|
/// <c>true</c> if the WebSocket service is successfully found and removed; otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
@ -284,9 +292,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public bool RemoveWebSocketService (string servicePath)
|
public bool RemoveWebSocketService (string servicePath)
|
||||||
{
|
{
|
||||||
if (servicePath.IsNullOrEmpty ())
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error ("'servicePath' must not be null or empty.");
|
Log.Error (String.Format ("{0}\nservice path: {1}", msg, servicePath ?? ""));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,9 +323,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Stop (ushort code, string reason)
|
public void Stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
if (!code.IsCloseStatusCode ())
|
var msg = code.CheckIfValidCloseStatusCode ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error ("Invalid status code for stop.\ncode: " + code);
|
Log.Error (String.Format ("{0}\ncode: {1}", msg, code));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="WebSocketServerBase"/> class
|
/// Initializes a new instance of the <see cref="WebSocketServerBase"/> class
|
||||||
/// that listens for incoming connection attempts on the specified <paramref name="address"/>,
|
/// that listens for incoming connection attempts on the specified <paramref name="address"/>,
|
||||||
/// <paramref name="port"/>, <paramref name="absPath"/> and <paramref name="secure"/>.
|
/// <paramref name="port"/>, <paramref name="servicePath"/> and <paramref name="secure"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">
|
/// <param name="address">
|
||||||
/// A <see cref="IPAddress"/> that contains a local IP address.
|
/// A <see cref="IPAddress"/> that contains a local IP address.
|
||||||
@ -125,7 +125,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="absPath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path.
|
/// A <see cref="string"/> that contains an absolute path.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="secure">
|
/// <param name="secure">
|
||||||
@ -133,14 +133,14 @@ namespace WebSocketSharp.Server
|
|||||||
/// (<c>true</c> indicates providing a secure connection.)
|
/// (<c>true</c> indicates providing a secure connection.)
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <exception cref="ArgumentNullException">
|
/// <exception cref="ArgumentNullException">
|
||||||
/// Either <paramref name="address"/> or <paramref name="absPath"/> is <see langword="null"/>.
|
/// Either <paramref name="address"/> or <paramref name="servicePath"/> is <see langword="null"/>.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
/// <exception cref="ArgumentOutOfRangeException">
|
/// <exception cref="ArgumentOutOfRangeException">
|
||||||
/// <paramref name="port"/> is 0 or less, or 65536 or greater.
|
/// <paramref name="port"/> is 0 or less, or 65536 or greater.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
/// <exception cref="ArgumentException">
|
/// <exception cref="ArgumentException">
|
||||||
/// <para>
|
/// <para>
|
||||||
/// <paramref name="absPath"/> is invalid.
|
/// <paramref name="servicePath"/> is invalid.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// -or-
|
/// -or-
|
||||||
@ -149,20 +149,20 @@ namespace WebSocketSharp.Server
|
|||||||
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
|
/// Pair of <paramref name="port"/> and <paramref name="secure"/> is invalid.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </exception>
|
/// </exception>
|
||||||
protected WebSocketServerBase (IPAddress address, int port, string absPath, bool secure)
|
protected WebSocketServerBase (IPAddress address, int port, string servicePath, bool secure)
|
||||||
{
|
{
|
||||||
if (address == null)
|
if (address == null)
|
||||||
throw new ArgumentNullException ("address");
|
throw new ArgumentNullException ("address");
|
||||||
|
|
||||||
if (absPath == null)
|
if (servicePath == null)
|
||||||
throw new ArgumentNullException ("absPath");
|
throw new ArgumentNullException ("servicePath");
|
||||||
|
|
||||||
if (!port.IsPortNumber ())
|
if (!port.IsPortNumber ())
|
||||||
throw new ArgumentOutOfRangeException ("port", "Invalid port number: " + port);
|
throw new ArgumentOutOfRangeException ("port", "Invalid port number: " + port);
|
||||||
|
|
||||||
string msg;
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
if (!absPath.IsValidAbsolutePath (out msg))
|
if (msg != null)
|
||||||
throw new ArgumentException (msg, "absPath");
|
throw new ArgumentException (msg, "servicePath");
|
||||||
|
|
||||||
if ((port == 80 && secure) || (port == 443 && !secure))
|
if ((port == 80 && secure) || (port == 443 && !secure))
|
||||||
throw new ArgumentException (String.Format (
|
throw new ArgumentException (String.Format (
|
||||||
@ -170,7 +170,7 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
_address = address;
|
_address = address;
|
||||||
_port = port;
|
_port = port;
|
||||||
_uri = absPath.ToUri ();
|
_uri = servicePath.ToUri ();
|
||||||
_secure = secure;
|
_secure = secure;
|
||||||
|
|
||||||
init ();
|
init ();
|
||||||
|
@ -37,7 +37,7 @@ using WebSocketSharp.Net.WebSockets;
|
|||||||
namespace WebSocketSharp.Server
|
namespace WebSocketSharp.Server
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides the basic functions of the WebSocket service.
|
/// Provides the basic functions of the WebSocket service managed by the WebSocket service host.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// The WebSocketService class is an abstract class.
|
/// The WebSocketService class is an abstract class.
|
||||||
@ -46,9 +46,9 @@ namespace WebSocketSharp.Server
|
|||||||
{
|
{
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
|
private WebSocket _websocket;
|
||||||
private WebSocketContext _context;
|
private WebSocketContext _context;
|
||||||
private WebSocketServiceManager _sessions;
|
private WebSocketServiceManager _sessions;
|
||||||
private WebSocket _websocket;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -115,11 +115,10 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the sessions to the <see cref="WebSocketService"/> instances.
|
/// Gets the collection of the WebSocket sessions managed by the WebSocket service host.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// A <see cref="WebSocketServiceManager"/> that contains the sessions to
|
/// A <see cref="WebSocketServiceManager"/> that contains a collection of the WebSocket sessions.
|
||||||
/// the <see cref="WebSocketService"/> instances.
|
|
||||||
/// </value>
|
/// </value>
|
||||||
protected WebSocketServiceManager Sessions {
|
protected WebSocketServiceManager Sessions {
|
||||||
get {
|
get {
|
||||||
@ -134,10 +133,10 @@ namespace WebSocketSharp.Server
|
|||||||
#region Public Properties
|
#region Public Properties
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the ID of the current <see cref="WebSocketService"/> instance.
|
/// Gets the session ID of the current <see cref="WebSocketService"/> instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// A <see cref="string"/> that contains an ID.
|
/// A <see cref="string"/> that contains a session ID.
|
||||||
/// </value>
|
/// </value>
|
||||||
public string ID {
|
public string ID {
|
||||||
get; private set;
|
get; private set;
|
||||||
@ -216,6 +215,97 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
#region Protected Methods
|
#region Protected Methods
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Broadcasts the specified array of <see cref="byte"/> to the clients of every <see cref="WebSocketService"/>
|
||||||
|
/// instances in the <see cref="WebSocketService.Sessions"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data">
|
||||||
|
/// An array of <see cref="byte"/> to broadcast.
|
||||||
|
/// </param>
|
||||||
|
protected virtual void Broadcast (byte [] data)
|
||||||
|
{
|
||||||
|
if (!IsBound)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var msg = data.CheckIfValidSendData ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
Error (msg);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_sessions.Broadcast (data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Broadcasts the specified <see cref="string"/> to the clients of every <see cref="WebSocketService"/>
|
||||||
|
/// instances in the <see cref="WebSocketService.Sessions"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data">
|
||||||
|
/// A <see cref="string"/> to broadcast.
|
||||||
|
/// </param>
|
||||||
|
protected virtual void Broadcast (string data)
|
||||||
|
{
|
||||||
|
if (!IsBound)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var msg = data.CheckIfValidSendData ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
Error (msg);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_sessions.Broadcast (data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends Pings to the clients of every <see cref="WebSocketService"/> instances
|
||||||
|
/// in the <see cref="WebSocketService.Sessions"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
|
/// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
|
||||||
|
/// </returns>
|
||||||
|
protected virtual Dictionary<string, bool> Broadping ()
|
||||||
|
{
|
||||||
|
return IsBound
|
||||||
|
? _sessions.Broadping ()
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends Pings with the specified <paramref name="message"/> to the clients of every <see cref="WebSocketService"/>
|
||||||
|
/// instances in the <see cref="WebSocketService.Sessions"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
|
/// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="message">
|
||||||
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
|
/// </param>
|
||||||
|
protected virtual Dictionary<string, bool> Broadping (string message)
|
||||||
|
{
|
||||||
|
if (!IsBound)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var msg = message.CheckIfValidPingMessage ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
Error (msg);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _sessions.Broadping (message);
|
||||||
|
}
|
||||||
|
|
||||||
/// <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"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -269,6 +359,125 @@ namespace WebSocketSharp.Server
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping to the client of the <see cref="WebSocketService"/> instance
|
||||||
|
/// with the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
|
||||||
|
/// a Pong in a time; otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
|
/// </param>
|
||||||
|
protected virtual bool PingTo (string id)
|
||||||
|
{
|
||||||
|
if (!IsBound)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
Error (msg);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _sessions.PingTo (id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping with the specified <paramref name="message"/> to the client of
|
||||||
|
/// the <see cref="WebSocketService"/> instance with the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
|
||||||
|
/// a Pong in a time; otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="message">
|
||||||
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
|
/// </param>
|
||||||
|
protected virtual bool PingTo (string message, string id)
|
||||||
|
{
|
||||||
|
if (!IsBound)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
Error (msg);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _sessions.PingTo (message, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a binary data to the client of the <see cref="WebSocketService"/> instance
|
||||||
|
/// with the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="data">
|
||||||
|
/// An array of <see cref="byte"/> that contains a binary data to send.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
|
/// </param>
|
||||||
|
protected virtual bool SendTo (byte [] data, string id)
|
||||||
|
{
|
||||||
|
if (!IsBound)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
Error (msg);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _sessions.SendTo (data, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a text data to the client of the <see cref="WebSocketService"/> instance
|
||||||
|
/// with the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="data">
|
||||||
|
/// A <see cref="string"/> that contains a text data to send.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
|
/// </param>
|
||||||
|
protected virtual bool SendTo (string data, string id)
|
||||||
|
{
|
||||||
|
if (!IsBound)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
Error (msg);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _sessions.SendTo (data, id);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validates the cookies used in the WebSocket connection request.
|
/// Validates the cookies used in the WebSocket connection request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -296,101 +505,6 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
#region Public Methods
|
#region Public Methods
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Broadcasts the specified array of <see cref="byte"/> to the clients of every <see cref="WebSocketService"/>
|
|
||||||
/// instances in the <see cref="WebSocketService.Sessions"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="data">
|
|
||||||
/// An array of <see cref="byte"/> to broadcast.
|
|
||||||
/// </param>
|
|
||||||
public virtual void Broadcast (byte [] data)
|
|
||||||
{
|
|
||||||
if (!IsBound)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (data == null)
|
|
||||||
{
|
|
||||||
var msg = "'data' must not be null.";
|
|
||||||
Log.Error (msg);
|
|
||||||
Error (msg);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_sessions.Broadcast (data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Broadcasts the specified <see cref="string"/> to the clients of every <see cref="WebSocketService"/>
|
|
||||||
/// instances in the <see cref="WebSocketService.Sessions"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="data">
|
|
||||||
/// A <see cref="string"/> to broadcast.
|
|
||||||
/// </param>
|
|
||||||
public virtual void Broadcast (string data)
|
|
||||||
{
|
|
||||||
if (!IsBound)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (data == null)
|
|
||||||
{
|
|
||||||
var msg = "'data' must not be null.";
|
|
||||||
Log.Error (msg);
|
|
||||||
Error (msg);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_sessions.Broadcast (data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends Pings to the clients of every <see cref="WebSocketService"/> instances
|
|
||||||
/// in the <see cref="WebSocketService.Sessions"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
|
||||||
/// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
|
|
||||||
/// </returns>
|
|
||||||
public virtual Dictionary<string, bool> Broadping ()
|
|
||||||
{
|
|
||||||
return IsBound
|
|
||||||
? _sessions.Broadping (String.Empty)
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends Pings with the specified <paramref name="message"/> to the clients of every <see cref="WebSocketService"/>
|
|
||||||
/// instances in the <see cref="WebSocketService.Sessions"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
|
||||||
/// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="message">
|
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
|
||||||
/// </param>
|
|
||||||
public virtual Dictionary<string, bool> Broadping (string message)
|
|
||||||
{
|
|
||||||
if (!IsBound)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
if (message.IsNullOrEmpty ())
|
|
||||||
return _sessions.Broadping (String.Empty);
|
|
||||||
|
|
||||||
var len = Encoding.UTF8.GetBytes (message).Length;
|
|
||||||
if (len > 125)
|
|
||||||
{
|
|
||||||
var msg = "The payload length of a Ping frame must be 125 bytes or less.";
|
|
||||||
Log.Error (msg);
|
|
||||||
Error (msg);
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _sessions.Broadping (message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a Ping to the client of the current <see cref="WebSocketService"/> instance.
|
/// Sends a Ping to the client of the current <see cref="WebSocketService"/> instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -423,53 +537,6 @@ namespace WebSocketSharp.Server
|
|||||||
: false;
|
: false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends a Ping to the client of the <see cref="WebSocketService"/> instance
|
|
||||||
/// with the specified ID.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
|
|
||||||
/// a Pong in a time; otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="id">
|
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
|
|
||||||
/// </param>
|
|
||||||
public virtual bool PingTo (string id)
|
|
||||||
{
|
|
||||||
return PingTo (String.Empty, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends a Ping with the specified <paramref name="message"/> to the client of
|
|
||||||
/// the <see cref="WebSocketService"/> instance with the specified ID.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
|
|
||||||
/// a Pong in a time; otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="message">
|
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="id">
|
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
|
|
||||||
/// </param>
|
|
||||||
public virtual bool PingTo (string message, string id)
|
|
||||||
{
|
|
||||||
if (!IsBound)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (id.IsNullOrEmpty ())
|
|
||||||
{
|
|
||||||
var msg = "'id' must not be null or empty.";
|
|
||||||
Log.Error (msg);
|
|
||||||
Error (msg);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _sessions.PingTo (message, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a binary data to the client of the current <see cref="WebSocketService"/> instance.
|
/// Sends a binary data to the client of the current <see cref="WebSocketService"/> instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -494,76 +561,6 @@ namespace WebSocketSharp.Server
|
|||||||
_websocket.Send (data);
|
_websocket.Send (data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends a binary data to the client of the <see cref="WebSocketService"/> instance
|
|
||||||
/// with the specified ID.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="data">
|
|
||||||
/// An array of <see cref="byte"/> that contains a binary data to send.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="id">
|
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
|
||||||
/// </param>
|
|
||||||
public virtual bool SendTo (byte [] data, string id)
|
|
||||||
{
|
|
||||||
if (!IsBound)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var msg = data == null
|
|
||||||
? "'data' must not be null."
|
|
||||||
: id.IsNullOrEmpty ()
|
|
||||||
? "'id' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
|
||||||
{
|
|
||||||
Log.Error (msg);
|
|
||||||
Error (msg);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _sessions.SendTo (data, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sends a text data to the client of the <see cref="WebSocketService"/> instance
|
|
||||||
/// with the specified ID.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>
|
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
|
||||||
/// </returns>
|
|
||||||
/// <param name="data">
|
|
||||||
/// A <see cref="string"/> that contains a text data to send.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="id">
|
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
|
||||||
/// </param>
|
|
||||||
public virtual bool SendTo (string data, string id)
|
|
||||||
{
|
|
||||||
if (!IsBound)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var msg = data == null
|
|
||||||
? "'data' must not be null."
|
|
||||||
: id.IsNullOrEmpty ()
|
|
||||||
? "'id' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
|
||||||
{
|
|
||||||
Log.Error (msg);
|
|
||||||
Error (msg);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _sessions.SendTo (data, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Starts the current <see cref="WebSocketService"/> instance.
|
/// Starts the current <see cref="WebSocketService"/> instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -52,6 +52,7 @@ namespace WebSocketSharp.Server
|
|||||||
{
|
{
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
|
private string _servicePath;
|
||||||
private WebSocketServiceManager _sessions;
|
private WebSocketServiceManager _sessions;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -111,43 +112,43 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
||||||
/// incoming connection attempts on the specified <paramref name="port"/> and <paramref name="absPath"/>.
|
/// incoming connection attempts on the specified <paramref name="port"/> and <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="absPath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path.
|
/// A <see cref="string"/> that contains an absolute path.
|
||||||
/// </param>
|
/// </param>
|
||||||
public WebSocketServiceHost (int port, string absPath)
|
public WebSocketServiceHost (int port, string servicePath)
|
||||||
: this (System.Net.IPAddress.Any, port, absPath)
|
: this (System.Net.IPAddress.Any, port, servicePath)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
||||||
/// incoming connection attempts on the specified <paramref name="port"/>, <paramref name="absPath"/>
|
/// incoming connection attempts on the specified <paramref name="port"/>, <paramref name="servicePath"/>
|
||||||
/// and <paramref name="secure"/>.
|
/// and <paramref name="secure"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="absPath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path.
|
/// A <see cref="string"/> that contains an absolute path.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="secure">
|
/// <param name="secure">
|
||||||
/// A <see cref="bool"/> that indicates providing a secure connection or not.
|
/// A <see cref="bool"/> that indicates providing a secure connection or not.
|
||||||
/// (<c>true</c> indicates providing a secure connection.)
|
/// (<c>true</c> indicates providing a secure connection.)
|
||||||
/// </param>
|
/// </param>
|
||||||
public WebSocketServiceHost (int port, string absPath, bool secure)
|
public WebSocketServiceHost (int port, string servicePath, bool secure)
|
||||||
: this (System.Net.IPAddress.Any, port, absPath, secure)
|
: this (System.Net.IPAddress.Any, port, servicePath, secure)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
||||||
/// incoming connection attempts on the specified <paramref name="address"/>, <paramref name="port"/>
|
/// incoming connection attempts on the specified <paramref name="address"/>, <paramref name="port"/>
|
||||||
/// and <paramref name="absPath"/>.
|
/// and <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">
|
/// <param name="address">
|
||||||
/// A <see cref="System.Net.IPAddress"/> that contains a local IP address.
|
/// A <see cref="System.Net.IPAddress"/> that contains a local IP address.
|
||||||
@ -155,18 +156,18 @@ namespace WebSocketSharp.Server
|
|||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="absPath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path.
|
/// A <see cref="string"/> that contains an absolute path.
|
||||||
/// </param>
|
/// </param>
|
||||||
public WebSocketServiceHost (System.Net.IPAddress address, int port, string absPath)
|
public WebSocketServiceHost (System.Net.IPAddress address, int port, string servicePath)
|
||||||
: this (address, port, absPath, port == 443 ? true : false)
|
: this (address, port, servicePath, port == 443 ? true : false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
/// Initializes a new instance of the WebSocketServiceHost<T> class that listens for
|
||||||
/// incoming connection attempts on the specified <paramref name="address"/>, <paramref name="port"/>,
|
/// incoming connection attempts on the specified <paramref name="address"/>, <paramref name="port"/>,
|
||||||
/// <paramref name="absPath"/> and <paramref name="secure"/>.
|
/// <paramref name="servicePath"/> and <paramref name="secure"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">
|
/// <param name="address">
|
||||||
/// A <see cref="System.Net.IPAddress"/> that contains a local IP address.
|
/// A <see cref="System.Net.IPAddress"/> that contains a local IP address.
|
||||||
@ -174,15 +175,15 @@ namespace WebSocketSharp.Server
|
|||||||
/// <param name="port">
|
/// <param name="port">
|
||||||
/// An <see cref="int"/> that contains a port number.
|
/// An <see cref="int"/> that contains a port number.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="absPath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path.
|
/// A <see cref="string"/> that contains an absolute path.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="secure">
|
/// <param name="secure">
|
||||||
/// A <see cref="bool"/> that indicates providing a secure connection or not.
|
/// A <see cref="bool"/> that indicates providing a secure connection or not.
|
||||||
/// (<c>true</c> indicates providing a secure connection.)
|
/// (<c>true</c> indicates providing a secure connection.)
|
||||||
/// </param>
|
/// </param>
|
||||||
public WebSocketServiceHost (System.Net.IPAddress address, int port, string absPath, bool secure)
|
public WebSocketServiceHost (System.Net.IPAddress address, int port, string servicePath, bool secure)
|
||||||
: base (address, port, absPath, secure)
|
: base (address, port, servicePath, secure)
|
||||||
{
|
{
|
||||||
_sessions = new WebSocketServiceManager (Log);
|
_sessions = new WebSocketServiceManager (Log);
|
||||||
}
|
}
|
||||||
@ -208,8 +209,8 @@ namespace WebSocketSharp.Server
|
|||||||
/// the inactive <see cref="WebSocketService"/> instances periodically.
|
/// the inactive <see cref="WebSocketService"/> instances periodically.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// <c>true</c> if the WebSocket service host cleans up the inactive WebSocket service instances every 60 seconds;
|
/// <c>true</c> if the WebSocket service host cleans up the inactive WebSocket service instances
|
||||||
/// otherwise, <c>false</c>. The default value is <c>true</c>.
|
/// every 60 seconds; otherwise, <c>false</c>. The default value is <c>true</c>.
|
||||||
/// </value>
|
/// </value>
|
||||||
public bool KeepClean {
|
public bool KeepClean {
|
||||||
get {
|
get {
|
||||||
@ -221,6 +222,33 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the collection of the WebSocket sessions managed by the WebSocket service host.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// A <see cref="WebSocketServiceManager"/> that contains a collection of the WebSocket sessions.
|
||||||
|
/// </value>
|
||||||
|
public WebSocketServiceManager Sessions {
|
||||||
|
get {
|
||||||
|
return _sessions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the path to the WebSocket service that the WebSocket service host provides.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service.
|
||||||
|
/// </value>
|
||||||
|
public string ServicePath {
|
||||||
|
get {
|
||||||
|
if (_servicePath == null)
|
||||||
|
_servicePath = HttpUtility.UrlDecode (BaseUri.GetAbsolutePath ()).TrimEndSlash ();
|
||||||
|
|
||||||
|
return _servicePath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the WebSocket URL on which to listen for incoming connection attempts.
|
/// Gets the WebSocket URL on which to listen for incoming connection attempts.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -244,10 +272,10 @@ namespace WebSocketSharp.Server
|
|||||||
private void stop (ushort code, string reason)
|
private void stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
var data = code.Append (reason);
|
var data = code.Append (reason);
|
||||||
if (data.Length > 125)
|
var msg = data.CheckIfValidCloseData ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (String.Format (
|
Log.Error (String.Format ("{0}\ncode: {1}\nreason: {2}", msg, code, reason));
|
||||||
"The payload length of a Close frame must be 125 bytes or less.\ncode: {0}\nreason: {1}", code, reason));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,17 +296,17 @@ namespace WebSocketSharp.Server
|
|||||||
protected override void AcceptWebSocket (TcpListenerWebSocketContext context)
|
protected override void AcceptWebSocket (TcpListenerWebSocketContext context)
|
||||||
{
|
{
|
||||||
var websocket = context.WebSocket;
|
var websocket = context.WebSocket;
|
||||||
var path = context.Path.UrlDecode ();
|
|
||||||
|
|
||||||
websocket.Log = Log;
|
websocket.Log = Log;
|
||||||
if (path != Uri.GetAbsolutePath ().UrlDecode ())
|
|
||||||
|
var path = HttpUtility.UrlDecode (context.Path).TrimEndSlash ();
|
||||||
|
if (path != ServicePath)
|
||||||
{
|
{
|
||||||
websocket.Close (HttpStatusCode.NotImplemented);
|
websocket.Close (HttpStatusCode.NotImplemented);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Uri.IsAbsoluteUri)
|
if (BaseUri.IsAbsoluteUri)
|
||||||
websocket.Url = Uri;
|
websocket.Url = BaseUri;
|
||||||
|
|
||||||
((IWebSocketServiceHost) this).BindWebSocket (context);
|
((IWebSocketServiceHost) this).BindWebSocket (context);
|
||||||
}
|
}
|
||||||
@ -295,9 +323,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Broadcast (byte [] data)
|
public void Broadcast (byte [] data)
|
||||||
{
|
{
|
||||||
if (data == null)
|
var msg = data.CheckIfValidSendData ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error ("'data' must not be null.");
|
Log.Error (msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,34 +341,44 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Broadcast (string data)
|
public void Broadcast (string data)
|
||||||
{
|
{
|
||||||
if (data == null)
|
var msg = data.CheckIfValidSendData ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error ("'data' must not be null.");
|
Log.Error (msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_sessions.Broadcast (data);
|
_sessions.Broadcast (data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends Pings to all clients.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
|
/// indicating whether the WebSocket service host received a Pong from each client in a time.
|
||||||
|
/// </returns>
|
||||||
|
public Dictionary<string, bool> Broadping ()
|
||||||
|
{
|
||||||
|
return _sessions.Broadping ();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends Pings with the specified <paramref name="message"/> to all clients.
|
/// Sends Pings with the specified <paramref name="message"/> to all clients.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
/// indicating whether the service host received the Pong from each client in a time.
|
/// indicating whether the WebSocket service host received a Pong from each client in a time.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
public Dictionary<string, bool> Broadping (string message)
|
public Dictionary<string, bool> Broadping (string message)
|
||||||
{
|
{
|
||||||
if (message.IsNullOrEmpty ())
|
var msg = message.CheckIfValidPingMessage ();
|
||||||
return _sessions.Broadping (String.Empty);
|
if (msg != null)
|
||||||
|
|
||||||
var len = Encoding.UTF8.GetBytes (message).Length;
|
|
||||||
if (len > 125)
|
|
||||||
{
|
{
|
||||||
Log.Error ("The payload length of a Ping frame must be 125 bytes or less.");
|
Log.Error (msg);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,23 +386,115 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
/// Close the WebSocket session with the specified <paramref name="id"/>.
|
||||||
/// the specified ID.
|
/// </summary>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
public void CloseSession (string id)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_sessions.StopServiceInstance (id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>
|
||||||
|
/// and <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
public void CloseSession (ushort code, string reason, string id)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_sessions.StopServiceInstance (code, reason, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>
|
||||||
|
/// and <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
public void CloseSession (CloseStatusCode code, string reason, string id)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_sessions.StopServiceInstance (code, reason, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if the service host receives a Pong from the client in a time; otherwise, <c>false</c>.
|
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
|
/// </param>
|
||||||
|
public bool PingTo (string id)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
Log.Error (msg);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _sessions.PingTo (id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
||||||
|
/// the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
/// </param>
|
/// </param>
|
||||||
public bool PingTo (string message, string id)
|
public bool PingTo (string message, string id)
|
||||||
{
|
{
|
||||||
if (id.IsNullOrEmpty ())
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error ("'id' must not be null or empty.");
|
Log.Error (msg);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -371,7 +502,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a binary data to the client associated with the specified ID.
|
/// Sends a binary data to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -380,16 +511,11 @@ namespace WebSocketSharp.Server
|
|||||||
/// An array of <see cref="byte"/> that contains a binary data to send.
|
/// An array of <see cref="byte"/> that contains a binary data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
public bool SendTo (byte [] data, string id)
|
public bool SendTo (byte [] data, string id)
|
||||||
{
|
{
|
||||||
var msg = data == null
|
var msg = id.CheckIfValidSessionID ();
|
||||||
? "'data' must not be null."
|
|
||||||
: id.IsNullOrEmpty ()
|
|
||||||
? "'id' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (msg);
|
Log.Error (msg);
|
||||||
@ -400,7 +526,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a text data to the client associated with the specified ID.
|
/// Sends a text data to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -409,16 +535,11 @@ namespace WebSocketSharp.Server
|
|||||||
/// A <see cref="string"/> that contains a text data to send.
|
/// A <see cref="string"/> that contains a text data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
public bool SendTo (string data, string id)
|
public bool SendTo (string data, string id)
|
||||||
{
|
{
|
||||||
var msg = data == null
|
var msg = id.CheckIfValidSessionID ();
|
||||||
? "'data' must not be null."
|
|
||||||
: id.IsNullOrEmpty ()
|
|
||||||
? "'id' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error (msg);
|
Log.Error (msg);
|
||||||
@ -449,9 +570,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public void Stop (ushort code, string reason)
|
public void Stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
if (!code.IsCloseStatusCode ())
|
var msg = code.CheckIfValidCloseStatusCode ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
Log.Error ("Invalid status code for stop.\ncode: " + code);
|
Log.Error (String.Format ("{0}\ncode: {1}", msg, code));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -528,17 +650,80 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
/// Close the WebSocket session with the specified <paramref name="id"/>.
|
||||||
/// the specified ID.
|
/// </summary>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
void IWebSocketServiceHost.CloseSession (string id)
|
||||||
|
{
|
||||||
|
_sessions.StopServiceInstance (id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>
|
||||||
|
/// and <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
void IWebSocketServiceHost.CloseSession (ushort code, string reason, string id)
|
||||||
|
{
|
||||||
|
_sessions.StopServiceInstance (code, reason, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>
|
||||||
|
/// and <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
void IWebSocketServiceHost.CloseSession (CloseStatusCode code, string reason, string id)
|
||||||
|
{
|
||||||
|
_sessions.StopServiceInstance (code, reason, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if the service host receives a Pong from the client in a time; otherwise, <c>false</c>.
|
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
|
/// </param>
|
||||||
|
bool IWebSocketServiceHost.PingTo (string id)
|
||||||
|
{
|
||||||
|
return _sessions.PingTo (id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
||||||
|
/// the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the WebSocket service host receives a Pong from the client in a time;
|
||||||
|
/// otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
/// </param>
|
/// </param>
|
||||||
bool IWebSocketServiceHost.PingTo (string message, string id)
|
bool IWebSocketServiceHost.PingTo (string message, string id)
|
||||||
{
|
{
|
||||||
@ -546,7 +731,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a binary data to the client associated with the specified ID.
|
/// Sends a binary data to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -555,7 +740,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// An array of <see cref="byte"/> that contains a binary data to send.
|
/// An array of <see cref="byte"/> that contains a binary data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
bool IWebSocketServiceHost.SendTo (byte [] data, string id)
|
bool IWebSocketServiceHost.SendTo (byte [] data, string id)
|
||||||
{
|
{
|
||||||
@ -563,7 +748,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a text data to the client associated with the specified ID.
|
/// Sends a text data to the client associated with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -572,7 +757,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// A <see cref="string"/> that contains a text data to send.
|
/// A <see cref="string"/> that contains a text data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
bool IWebSocketServiceHost.SendTo (string data, string id)
|
bool IWebSocketServiceHost.SendTo (string data, string id)
|
||||||
{
|
{
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using WebSocketSharp.Net;
|
||||||
|
|
||||||
namespace WebSocketSharp.Server
|
namespace WebSocketSharp.Server
|
||||||
{
|
{
|
||||||
@ -63,56 +64,6 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Public Properties
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the connection count to the WebSocket services managed by the <see cref="WebSocketServiceHostManager"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// An <see cref="int"/> that contains the connection count.
|
|
||||||
/// </value>
|
|
||||||
public int ConnectionCount {
|
|
||||||
get {
|
|
||||||
var count = 0;
|
|
||||||
foreach (var host in ServiceHosts)
|
|
||||||
count += host.ConnectionCount;
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the number of the WebSocket services managed by the <see cref="WebSocketServiceHostManager"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// An <see cref="int"/> that contains the number of the WebSocket services.
|
|
||||||
/// </value>
|
|
||||||
public int ServiceCount {
|
|
||||||
get {
|
|
||||||
lock (_sync)
|
|
||||||
{
|
|
||||||
return _serviceHosts.Count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the collection of paths to the WebSocket services managed by the <see cref="WebSocketServiceHostManager"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// An IEnumerable<string> that contains the collection of paths.
|
|
||||||
/// </value>
|
|
||||||
public IEnumerable<string> ServicePaths {
|
|
||||||
get {
|
|
||||||
lock (_sync)
|
|
||||||
{
|
|
||||||
return _serviceHosts.Keys;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Internal Properties
|
#region Internal Properties
|
||||||
|
|
||||||
internal bool KeepClean {
|
internal bool KeepClean {
|
||||||
@ -144,6 +95,56 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Public Properties
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the connection count to the WebSocket services managed by the <see cref="WebSocketServiceHostManager"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// An <see cref="int"/> that contains the connection count.
|
||||||
|
/// </value>
|
||||||
|
public int ConnectionCount {
|
||||||
|
get {
|
||||||
|
var count = 0;
|
||||||
|
foreach (var host in ServiceHosts)
|
||||||
|
count += host.ConnectionCount;
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the number of the WebSocket services managed by the <see cref="WebSocketServiceHostManager"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// An <see cref="int"/> that contains the number of the WebSocket services.
|
||||||
|
/// </value>
|
||||||
|
public int Count {
|
||||||
|
get {
|
||||||
|
lock (_sync)
|
||||||
|
{
|
||||||
|
return _serviceHosts.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the collection of paths to the WebSocket services managed by the <see cref="WebSocketServiceHostManager"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// An IEnumerable<string> that contains the collection of paths.
|
||||||
|
/// </value>
|
||||||
|
public IEnumerable<string> ServicePaths {
|
||||||
|
get {
|
||||||
|
lock (_sync)
|
||||||
|
{
|
||||||
|
return _serviceHosts.Keys;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Private Methods
|
#region Private Methods
|
||||||
|
|
||||||
private Dictionary<string, IWebSocketServiceHost> copy ()
|
private Dictionary<string, IWebSocketServiceHost> copy ()
|
||||||
@ -160,6 +161,7 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
internal void Add (string servicePath, IWebSocketServiceHost serviceHost)
|
internal void Add (string servicePath, IWebSocketServiceHost serviceHost)
|
||||||
{
|
{
|
||||||
|
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash ();
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
IWebSocketServiceHost host;
|
IWebSocketServiceHost host;
|
||||||
@ -170,12 +172,14 @@ namespace WebSocketSharp.Server
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_serviceHosts.Add (servicePath.UrlDecode (), serviceHost);
|
_serviceHosts.Add (servicePath, serviceHost);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool Remove (string servicePath)
|
internal bool Remove (string servicePath)
|
||||||
{
|
{
|
||||||
|
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash ();
|
||||||
|
|
||||||
IWebSocketServiceHost host;
|
IWebSocketServiceHost host;
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
@ -217,6 +221,7 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
internal bool TryGetServiceHost (string servicePath, out IWebSocketServiceHost serviceHost)
|
internal bool TryGetServiceHost (string servicePath, out IWebSocketServiceHost serviceHost)
|
||||||
{
|
{
|
||||||
|
servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash ();
|
||||||
lock (_sync)
|
lock (_sync)
|
||||||
{
|
{
|
||||||
return _serviceHosts.TryGetValue (servicePath, out serviceHost);
|
return _serviceHosts.TryGetValue (servicePath, out serviceHost);
|
||||||
@ -228,16 +233,18 @@ namespace WebSocketSharp.Server
|
|||||||
#region Public Methods
|
#region Public Methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Broadcasts the specified array of <see cref="byte"/> to all clients of the WebSocket services.
|
/// Broadcasts the specified array of <see cref="byte"/> to all clients of the WebSocket services
|
||||||
|
/// managed by the <see cref="WebSocketServiceHostManager"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">
|
/// <param name="data">
|
||||||
/// An array of <see cref="byte"/> to broadcast.
|
/// An array of <see cref="byte"/> to broadcast.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void Broadcast (byte [] data)
|
public void Broadcast (byte [] data)
|
||||||
{
|
{
|
||||||
if (data == null)
|
var msg = data.CheckIfValidSendData ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error ("'data' must not be null.");
|
_logger.Error (msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,16 +253,18 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Broadcasts the specified <see cref="string"/> to all clients of the WebSocket services.
|
/// Broadcasts the specified <see cref="string"/> to all clients of the WebSocket services
|
||||||
|
/// managed by the <see cref="WebSocketServiceHostManager"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">
|
/// <param name="data">
|
||||||
/// A <see cref="string"/> to broadcast.
|
/// A <see cref="string"/> to broadcast.
|
||||||
/// </param>
|
/// </param>
|
||||||
public void Broadcast (string data)
|
public void Broadcast (string data)
|
||||||
{
|
{
|
||||||
if (data == null)
|
var msg = data.CheckIfValidSendData ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error ("'data' must not be null.");
|
_logger.Error (msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,12 +287,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public bool BroadcastTo (byte [] data, string servicePath)
|
public bool BroadcastTo (byte [] data, string servicePath)
|
||||||
{
|
{
|
||||||
var msg = data == null
|
var msg = data.CheckIfValidSendData () ?? servicePath.CheckIfValidServicePath ();
|
||||||
? "'data' must not be null."
|
|
||||||
: servicePath.IsNullOrEmpty ()
|
|
||||||
? "'servicePath' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
@ -316,12 +320,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public bool BroadcastTo (string data, string servicePath)
|
public bool BroadcastTo (string data, string servicePath)
|
||||||
{
|
{
|
||||||
var msg = data == null
|
var msg = data.CheckIfValidSendData () ?? servicePath.CheckIfValidServicePath ();
|
||||||
? "'data' must not be null."
|
|
||||||
: servicePath.IsNullOrEmpty ()
|
|
||||||
? "'servicePath' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
@ -340,26 +339,41 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends Pings with the specified <paramref name="message"/> to all clients of the WebSocket services.
|
/// Sends Pings to all clients of the WebSocket services managed by the <see cref="WebSocketServiceHostManager"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A Dictionary<string, Dictionary<string, bool>> that contains the collection of
|
/// A Dictionary<string, Dictionary<string, bool>> that contains the collection of
|
||||||
/// service paths and pairs of session ID and value indicating whether each WebSocket service
|
/// service paths and pairs of session ID and value indicating whether each WebSocket service
|
||||||
/// received the Pong from each client in a time.
|
/// received a Pong from each client in a time.
|
||||||
|
/// </returns>
|
||||||
|
public Dictionary<string, Dictionary<string, bool>> Broadping ()
|
||||||
|
{
|
||||||
|
var result = new Dictionary<string, Dictionary<string, bool>> ();
|
||||||
|
foreach (var service in copy ())
|
||||||
|
result.Add (service.Key, service.Value.Broadping ());
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends Pings with the specified <paramref name="message"/> to all clients of the WebSocket services
|
||||||
|
/// managed by the <see cref="WebSocketServiceHostManager"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A Dictionary<string, Dictionary<string, bool>> that contains the collection of
|
||||||
|
/// service paths and pairs of session ID and value indicating whether each WebSocket service
|
||||||
|
/// received a Pong from each client in a time.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
public Dictionary<string, Dictionary<string, bool>> Broadping (string message)
|
public Dictionary<string, Dictionary<string, bool>> Broadping (string message)
|
||||||
{
|
{
|
||||||
if (!message.IsNullOrEmpty ())
|
var msg = message.CheckIfValidPingMessage ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
var len = Encoding.UTF8.GetBytes (message).Length;
|
_logger.Error (msg);
|
||||||
if (len > 125)
|
return null;
|
||||||
{
|
|
||||||
_logger.Error ("The payload length of a Ping frame must be 125 bytes or less.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = new Dictionary<string, Dictionary<string, bool>> ();
|
var result = new Dictionary<string, Dictionary<string, bool>> ();
|
||||||
@ -369,13 +383,43 @@ namespace WebSocketSharp.Server
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends Pings to all clients of the WebSocket service with the specified <paramref name="servicePath"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
|
/// indicating whether the WebSocket service received a Pong from each client in a time.
|
||||||
|
/// If the WebSocket service is not found, returns <see langword="null"/>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="servicePath">
|
||||||
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
|
/// </param>
|
||||||
|
public Dictionary<string, bool> BroadpingTo (string servicePath)
|
||||||
|
{
|
||||||
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
_logger.Error (msg);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWebSocketServiceHost host;
|
||||||
|
if (!TryGetServiceHost (servicePath, out host))
|
||||||
|
{
|
||||||
|
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return host.Broadping ();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends Pings with the specified <paramref name="message"/> to all clients of the WebSocket service
|
/// Sends Pings with the specified <paramref name="message"/> to all clients of the WebSocket service
|
||||||
/// with the specified <paramref name="servicePath"/>.
|
/// with the specified <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
||||||
/// indicating whether the WebSocket service received the Pong from each client in a time.
|
/// indicating whether the WebSocket service received a Pong from each client in a time.
|
||||||
/// If the WebSocket service is not found, returns <see langword="null"/>.
|
/// If the WebSocket service is not found, returns <see langword="null"/>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
@ -386,15 +430,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public Dictionary<string, bool> BroadpingTo (string message, string servicePath)
|
public Dictionary<string, bool> BroadpingTo (string message, string servicePath)
|
||||||
{
|
{
|
||||||
if (message == null)
|
var msg = message.CheckIfValidPingMessage () ?? servicePath.CheckIfValidServicePath ();
|
||||||
message = String.Empty;
|
|
||||||
|
|
||||||
var msg = Encoding.UTF8.GetBytes (message).Length > 125
|
|
||||||
? "The payload length of a Ping frame must be 125 bytes or less."
|
|
||||||
: servicePath.IsNullOrEmpty ()
|
|
||||||
? "'servicePath' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error (msg);
|
_logger.Error (msg);
|
||||||
@ -411,21 +447,121 @@ namespace WebSocketSharp.Server
|
|||||||
return host.Broadping (message);
|
return host.Broadping (message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="id"/> and
|
||||||
|
/// <paramref name="servicePath"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="servicePath">
|
||||||
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
|
/// </param>
|
||||||
|
public void CloseSession (string id, string servicePath)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID () ?? servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
_logger.Error (msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWebSocketServiceHost host;
|
||||||
|
if (!TryGetServiceHost (servicePath, out host))
|
||||||
|
{
|
||||||
|
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
host.CloseSession (id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>,
|
||||||
|
/// <paramref name="id"/> and <paramref name="servicePath"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="servicePath">
|
||||||
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
|
/// </param>
|
||||||
|
public void CloseSession (ushort code, string reason, string id, string servicePath)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID () ?? servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
_logger.Error (msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWebSocketServiceHost host;
|
||||||
|
if (!TryGetServiceHost (servicePath, out host))
|
||||||
|
{
|
||||||
|
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
host.CloseSession (code, reason, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Close the WebSocket session with the specified <paramref name="code"/>, <paramref name="reason"/>,
|
||||||
|
/// <paramref name="id"/> and <paramref name="servicePath"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">
|
||||||
|
/// A <see cref="CloseStatusCode"/> that contains a status code indicating the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="reason">
|
||||||
|
/// A <see cref="string"/> that contains the reason for closure.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID to find.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="servicePath">
|
||||||
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
|
/// </param>
|
||||||
|
public void CloseSession (CloseStatusCode code, string reason, string id, string servicePath)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID () ?? servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
_logger.Error (msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWebSocketServiceHost host;
|
||||||
|
if (!TryGetServiceHost (servicePath, out host))
|
||||||
|
{
|
||||||
|
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
host.CloseSession (code, reason, id);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the connection count to the WebSocket service with the specified <paramref name="servicePath"/>.
|
/// Gets the connection count to the WebSocket service with the specified <paramref name="servicePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// An <see cref="int"/> that contains the connection count if the WebSocket service is successfully found;
|
/// An <see cref="int"/> that contains the connection count if the WebSocket service is successfully
|
||||||
/// otherwise, <c>-1</c>.
|
/// found; otherwise, <c>-1</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="servicePath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
/// </param>
|
/// </param>
|
||||||
public int GetConnectionCount (string servicePath)
|
public int GetConnectionCount (string servicePath)
|
||||||
{
|
{
|
||||||
if (servicePath.IsNullOrEmpty ())
|
var msg = servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error ("'servicePath' must not be null or empty.");
|
_logger.Error (msg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -439,6 +575,39 @@ namespace WebSocketSharp.Server
|
|||||||
return host.ConnectionCount;
|
return host.ConnectionCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping to the client associated with the specified <paramref name="id"/> and
|
||||||
|
/// <paramref name="servicePath"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the WebSocket service with <paramref name="servicePath"/> receives a Pong
|
||||||
|
/// from the client in a time; otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="servicePath">
|
||||||
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
|
/// </param>
|
||||||
|
public bool PingTo (string id, string servicePath)
|
||||||
|
{
|
||||||
|
var msg = id.CheckIfValidSessionID () ?? servicePath.CheckIfValidServicePath ();
|
||||||
|
if (msg != null)
|
||||||
|
{
|
||||||
|
_logger.Error (msg);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
IWebSocketServiceHost host;
|
||||||
|
if (!TryGetServiceHost (servicePath, out host))
|
||||||
|
{
|
||||||
|
_logger.Error ("The WebSocket service with the specified path not found.\npath: " + servicePath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return host.PingTo (id);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
/// Sends a Ping with the specified <paramref name="message"/> to the client associated with
|
||||||
/// the specified <paramref name="id"/> and <paramref name="servicePath"/>.
|
/// the specified <paramref name="id"/> and <paramref name="servicePath"/>.
|
||||||
@ -451,18 +620,15 @@ namespace WebSocketSharp.Server
|
|||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the Ping.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="servicePath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
/// </param>
|
/// </param>
|
||||||
public bool PingTo (string message, string id, string servicePath)
|
public bool PingTo (string message, string id, string servicePath)
|
||||||
{
|
{
|
||||||
var msg = id.IsNullOrEmpty ()
|
var msg = (message.CheckIfValidPingMessage () ?? id.CheckIfValidSessionID ()) ??
|
||||||
? "'id' must not be null or empty."
|
servicePath.CheckIfValidServicePath ();
|
||||||
: servicePath.IsNullOrEmpty ()
|
|
||||||
? "'servicePath' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
@ -491,20 +657,15 @@ namespace WebSocketSharp.Server
|
|||||||
/// An array of <see cref="byte"/> that contains a binary data to send.
|
/// An array of <see cref="byte"/> that contains a binary data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="servicePath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
/// </param>
|
/// </param>
|
||||||
public bool SendTo (byte [] data, string id, string servicePath)
|
public bool SendTo (byte [] data, string id, string servicePath)
|
||||||
{
|
{
|
||||||
var msg = data == null
|
var msg = (data.CheckIfValidSendData () ?? id.CheckIfValidSessionID ()) ??
|
||||||
? "'data' must not be null."
|
servicePath.CheckIfValidServicePath ();
|
||||||
: id.IsNullOrEmpty ()
|
|
||||||
? "'id' must not be null or empty."
|
|
||||||
: servicePath.IsNullOrEmpty ()
|
|
||||||
? "'servicePath' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
@ -533,20 +694,15 @@ namespace WebSocketSharp.Server
|
|||||||
/// A <see cref="string"/> that contains a text data to send.
|
/// A <see cref="string"/> that contains a text data to send.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="id">
|
/// <param name="id">
|
||||||
/// A <see cref="string"/> that contains an ID that represents the destination for the data.
|
/// A <see cref="string"/> that contains a session ID that represents the destination for the data.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="servicePath">
|
/// <param name="servicePath">
|
||||||
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
/// A <see cref="string"/> that contains an absolute path to the WebSocket service to find.
|
||||||
/// </param>
|
/// </param>
|
||||||
public bool SendTo (string data, string id, string servicePath)
|
public bool SendTo (string data, string id, string servicePath)
|
||||||
{
|
{
|
||||||
var msg = data == null
|
var msg = (data.CheckIfValidSendData () ?? id.CheckIfValidSessionID ()) ??
|
||||||
? "'data' must not be null."
|
servicePath.CheckIfValidServicePath ();
|
||||||
: id.IsNullOrEmpty ()
|
|
||||||
? "'id' must not be null or empty."
|
|
||||||
: servicePath.IsNullOrEmpty ()
|
|
||||||
? "'servicePath' must not be null or empty."
|
|
||||||
: null;
|
|
||||||
|
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
{
|
{
|
||||||
|
@ -84,7 +84,7 @@ namespace WebSocketSharp.Server
|
|||||||
/// </value>
|
/// </value>
|
||||||
public IEnumerable<string> ActiveIDs {
|
public IEnumerable<string> ActiveIDs {
|
||||||
get {
|
get {
|
||||||
return from result in Broadping (String.Empty)
|
return from result in Broadping ()
|
||||||
where result.Value
|
where result.Value
|
||||||
select result.Key;
|
select result.Key;
|
||||||
}
|
}
|
||||||
@ -134,14 +134,14 @@ namespace WebSocketSharp.Server
|
|||||||
/// </value>
|
/// </value>
|
||||||
public IEnumerable<string> InactiveIDs {
|
public IEnumerable<string> InactiveIDs {
|
||||||
get {
|
get {
|
||||||
return from result in Broadping (String.Empty)
|
return from result in Broadping ()
|
||||||
where !result.Value
|
where !result.Value
|
||||||
select result.Key;
|
select result.Key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the <see cref="WebSocketService"/> instance with the specified ID
|
/// Gets the <see cref="WebSocketService"/> instance with the specified <paramref name="id"/>
|
||||||
/// from the <see cref="WebSocketServiceManager"/>.
|
/// from the <see cref="WebSocketServiceManager"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
@ -153,9 +153,10 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public WebSocketService this [string id] {
|
public WebSocketService this [string id] {
|
||||||
get {
|
get {
|
||||||
if (id.IsNullOrEmpty ())
|
var msg = id.CheckIfValidSessionID ();
|
||||||
|
if (msg != null)
|
||||||
{
|
{
|
||||||
_logger.Error ("'id' must not be null or empty.");
|
_logger.Error (msg);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -289,23 +290,6 @@ namespace WebSocketSharp.Server
|
|||||||
_sweepTimer.Start ();
|
_sweepTimer.Start ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stop (ushort code, string reason, bool ignoreArgs)
|
|
||||||
{
|
|
||||||
stopSweepTimer ();
|
|
||||||
lock (_sync)
|
|
||||||
{
|
|
||||||
if (_stopped)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_stopped = true;
|
|
||||||
foreach (var service in copy ().Values)
|
|
||||||
if (ignoreArgs)
|
|
||||||
service.Stop ();
|
|
||||||
else
|
|
||||||
service.Stop (code, reason);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void stopSweepTimer ()
|
private void stopSweepTimer ()
|
||||||
{
|
{
|
||||||
if (_sweepTimer.Enabled)
|
if (_sweepTimer.Enabled)
|
||||||
@ -331,8 +315,8 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Broadcasts the specified array of <see cref="byte"/> to the clients of every
|
/// Broadcasts the specified array of <see cref="byte"/> to the clients of every <see cref="WebSocketService"/>
|
||||||
/// <see cref="WebSocketService"/> instances managed by the <see cref="WebSocketServiceManager"/>.
|
/// instances managed by the <see cref="WebSocketServiceManager"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">
|
/// <param name="data">
|
||||||
/// An array of <see cref="byte"/> to broadcast.
|
/// An array of <see cref="byte"/> to broadcast.
|
||||||
@ -346,8 +330,8 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Broadcasts the specified <see cref="string"/> to the clients of every
|
/// Broadcasts the specified <see cref="string"/> to the clients of every <see cref="WebSocketService"/>
|
||||||
/// <see cref="WebSocketService"/> instances managed by the <see cref="WebSocketServiceManager"/>.
|
/// instances managed by the <see cref="WebSocketServiceManager"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">
|
/// <param name="data">
|
||||||
/// A <see cref="string"/> to broadcast.
|
/// A <see cref="string"/> to broadcast.
|
||||||
@ -361,12 +345,29 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends Pings with the specified <paramref name="message"/> to the clients of every
|
/// Sends Pings to the clients of every <see cref="WebSocketService"/> instances managed by
|
||||||
/// <see cref="WebSocketService"/> instances managed by the <see cref="WebSocketServiceManager"/>.
|
/// the <see cref="WebSocketServiceManager"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and value
|
/// A Dictionary<string, bool> that contains the collection of pairs of ID and value indicating
|
||||||
/// indicating whether the each <see cref="WebSocketService"/> instance received a Pong in a time.
|
/// whether each <see cref="WebSocketService"/> instance received a Pong from the client in a time.
|
||||||
|
/// </returns>
|
||||||
|
internal Dictionary<string, bool> Broadping ()
|
||||||
|
{
|
||||||
|
var result = new Dictionary<string, bool> ();
|
||||||
|
foreach (var session in copy ())
|
||||||
|
result.Add (session.Key, session.Value.Ping ());
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends Pings with the specified <paramref name="message"/> to the clients of every <see cref="WebSocketService"/>
|
||||||
|
/// instances managed by the <see cref="WebSocketServiceManager"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// A Dictionary<string, bool> that contains the collection of pairs of ID and value indicating
|
||||||
|
/// whether each <see cref="WebSocketService"/> instance received a Pong from the client in a time.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
@ -381,12 +382,36 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a Ping with the specified <paramref name="message"/> to the client of
|
/// Sends a Ping to the client of the <see cref="WebSocketService"/> instance
|
||||||
/// the <see cref="WebSocketService"/> instance with the specified ID.
|
/// with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/> receives
|
/// <c>true</c> if the <see cref="WebSocketService"/> instance receives a Pong from the client
|
||||||
/// a Pong in a time; otherwise, <c>false</c>.
|
/// in a time; otherwise, <c>false</c>.
|
||||||
|
/// </returns>
|
||||||
|
/// <param name="id">
|
||||||
|
/// A <see cref="string"/> that contains an ID that represents the destination for the Ping.
|
||||||
|
/// </param>
|
||||||
|
internal bool PingTo (string id)
|
||||||
|
{
|
||||||
|
WebSocketService service;
|
||||||
|
if (!TryGetServiceInstance (id, out service))
|
||||||
|
{
|
||||||
|
_logger.Error (
|
||||||
|
"The WebSocket service instance with the specified ID not found.\nID: " + id);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return service.Ping ();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a Ping with the specified <paramref name="message"/> to the client of the <see cref="WebSocketService"/>
|
||||||
|
/// instance with the specified <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <c>true</c> if the <see cref="WebSocketService"/> instance receives a Pong from the client
|
||||||
|
/// in a time; otherwise, <c>false</c>.
|
||||||
/// </returns>
|
/// </returns>
|
||||||
/// <param name="message">
|
/// <param name="message">
|
||||||
/// A <see cref="string"/> that contains a message to send.
|
/// A <see cref="string"/> that contains a message to send.
|
||||||
@ -417,7 +442,7 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a binary data to the client of the <see cref="WebSocketService"/> instance
|
/// Sends a binary data to the client of the <see cref="WebSocketService"/> instance
|
||||||
/// with the specified ID.
|
/// with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -444,7 +469,7 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a text data to the client of the <see cref="WebSocketService"/> instance
|
/// Sends a text data to the client of the <see cref="WebSocketService"/> instance
|
||||||
/// with the specified ID.
|
/// with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
/// <c>true</c> if <paramref name="data"/> is successfully sent; otherwise, <c>false</c>.
|
||||||
@ -471,12 +496,69 @@ namespace WebSocketSharp.Server
|
|||||||
|
|
||||||
internal void Stop ()
|
internal void Stop ()
|
||||||
{
|
{
|
||||||
stop (0, null, true);
|
stopSweepTimer ();
|
||||||
|
lock (_sync)
|
||||||
|
{
|
||||||
|
if (_stopped)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_stopped = true;
|
||||||
|
foreach (var service in copy ().Values)
|
||||||
|
service.Stop ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Stop (ushort code, string reason)
|
internal void Stop (ushort code, string reason)
|
||||||
{
|
{
|
||||||
stop (code, reason, false);
|
stopSweepTimer ();
|
||||||
|
lock (_sync)
|
||||||
|
{
|
||||||
|
if (_stopped)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_stopped = true;
|
||||||
|
foreach (var service in copy ().Values)
|
||||||
|
service.Stop (code, reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void StopServiceInstance (string id)
|
||||||
|
{
|
||||||
|
WebSocketService service;
|
||||||
|
if (!TryGetServiceInstance (id, out service))
|
||||||
|
{
|
||||||
|
_logger.Error (
|
||||||
|
"The WebSocket service instance with the specified ID not found.\nID: " + id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
service.Stop ();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void StopServiceInstance (ushort code, string reason, string id)
|
||||||
|
{
|
||||||
|
WebSocketService service;
|
||||||
|
if (!TryGetServiceInstance (id, out service))
|
||||||
|
{
|
||||||
|
_logger.Error (
|
||||||
|
"The WebSocket service instance with the specified ID not found.\nID: " + id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
service.Stop (code, reason);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void StopServiceInstance (CloseStatusCode code, string reason, string id)
|
||||||
|
{
|
||||||
|
WebSocketService service;
|
||||||
|
if (!TryGetServiceInstance (id, out service))
|
||||||
|
{
|
||||||
|
_logger.Error (
|
||||||
|
"The WebSocket service instance with the specified ID not found.\nID: " + id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
service.Stop (code, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -520,7 +602,7 @@ namespace WebSocketSharp.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries to get the <see cref="WebSocketService"/> instance with the specified ID.
|
/// Tries to get the <see cref="WebSocketService"/> instance with the specified <paramref name="id"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/>
|
/// <c>true</c> if the <see cref="WebSocketService"/> instance with <paramref name="id"/>
|
||||||
|
Loading…
Reference in New Issue
Block a user