|
|
|
@ -104,10 +104,12 @@ namespace WebSocketSharp.Server
|
|
|
|
#region Public Properties
|
|
|
|
#region Public Properties
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the collection of every ID of the active sessions to the Websocket service.
|
|
|
|
/// Gets the collection of every ID of the active sessions to the Websocket
|
|
|
|
|
|
|
|
/// service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// An IEnumerable<string> that contains the collection of every ID of the active sessions.
|
|
|
|
/// An IEnumerable<string> that contains the collection of every ID of
|
|
|
|
|
|
|
|
/// the active sessions.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public IEnumerable<string> ActiveIDs {
|
|
|
|
public IEnumerable<string> ActiveIDs {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
@ -121,12 +123,11 @@ namespace WebSocketSharp.Server
|
|
|
|
/// Gets the number of the sessions to the Websocket service.
|
|
|
|
/// Gets the number of the sessions to the Websocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// An <see cref="int"/> that contains the number of the sessions.
|
|
|
|
/// An <see cref="int"/> that represents the number of the sessions.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public int Count {
|
|
|
|
public int Count {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
return _sessions.Count;
|
|
|
|
return _sessions.Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -136,22 +137,24 @@ namespace WebSocketSharp.Server
|
|
|
|
/// Gets the collection of every ID of the sessions to the Websocket service.
|
|
|
|
/// Gets the collection of every ID of the sessions to the Websocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// An IEnumerable<string> that contains the collection of every ID of the sessions.
|
|
|
|
/// An IEnumerable<string> that contains the collection of every ID of
|
|
|
|
|
|
|
|
/// the sessions.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public IEnumerable<string> IDs {
|
|
|
|
public IEnumerable<string> IDs {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
return _sessions.Keys.ToList ();
|
|
|
|
return _sessions.Keys.ToList ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the collection of every ID of the inactive sessions to the Websocket service.
|
|
|
|
/// Gets the collection of every ID of the inactive sessions to the Websocket
|
|
|
|
|
|
|
|
/// service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// An IEnumerable<string> that contains the collection of every ID of the inactive sessions.
|
|
|
|
/// An IEnumerable<string> that contains the collection of every ID of
|
|
|
|
|
|
|
|
/// the inactive sessions.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public IEnumerable<string> InactiveIDs {
|
|
|
|
public IEnumerable<string> InactiveIDs {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
@ -165,11 +168,12 @@ namespace WebSocketSharp.Server
|
|
|
|
/// Gets a WebSocket session information with the specified <paramref name="id"/>.
|
|
|
|
/// Gets a WebSocket session information with the specified <paramref name="id"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// A <see cref="IWebSocketSession"/> instance that contains the session information
|
|
|
|
/// A <see cref="IWebSocketSession"/> instance that represents the session if
|
|
|
|
/// if the session is successfully found; otherwise, <see langword="null"/>.
|
|
|
|
/// it's successfully found; otherwise, <see langword="null"/>.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that contains an ID of the session to find.
|
|
|
|
/// A <see cref="string"/> that represents the ID of the WebSocket session to
|
|
|
|
|
|
|
|
/// get.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public IWebSocketSession this [string id] {
|
|
|
|
public IWebSocketSession this [string id] {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
@ -181,11 +185,12 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether the manager cleans up the inactive sessions periodically.
|
|
|
|
/// Gets a value indicating whether the manager cleans up the inactive
|
|
|
|
|
|
|
|
/// sessions periodically.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// <c>true</c> if the manager cleans up the inactive sessions every 60 seconds;
|
|
|
|
/// <c>true</c> if the manager cleans up the inactive sessions every 60
|
|
|
|
/// otherwise, <c>false</c>.
|
|
|
|
/// seconds; otherwise, <c>false</c>.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public bool KeepClean {
|
|
|
|
public bool KeepClean {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
@ -206,15 +211,15 @@ namespace WebSocketSharp.Server
|
|
|
|
/// Gets the collection of the session informations to the Websocket service.
|
|
|
|
/// Gets the collection of the session informations to the Websocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <value>
|
|
|
|
/// <value>
|
|
|
|
/// An IEnumerable<IWebSocketSession> that contains the collection of the session informations.
|
|
|
|
/// An IEnumerable<IWebSocketSession> that contains the collection of
|
|
|
|
|
|
|
|
/// the session informations to the Websocket service.
|
|
|
|
/// </value>
|
|
|
|
/// </value>
|
|
|
|
public IEnumerable<IWebSocketSession> Sessions {
|
|
|
|
public IEnumerable<IWebSocketSession> Sessions {
|
|
|
|
get {
|
|
|
|
get {
|
|
|
|
if (_state == ServerState.SHUTDOWN)
|
|
|
|
if (_state == ServerState.SHUTDOWN)
|
|
|
|
return _emptySessions;
|
|
|
|
return _emptySessions;
|
|
|
|
|
|
|
|
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
return _sessions.Values.ToList ();
|
|
|
|
return _sessions.Values.ToList ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -224,6 +229,53 @@ namespace WebSocketSharp.Server
|
|
|
|
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
#region Private Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void broadcast (Opcode opcode, byte [] data, Action completed)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var cache = new Dictionary<CompressionMethod, byte []> ();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Broadcast (opcode, data, cache);
|
|
|
|
|
|
|
|
if (completed != null)
|
|
|
|
|
|
|
|
completed ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
|
|
|
_logger.Fatal (ex.ToString ());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
|
|
|
|
cache.Clear ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void broadcast (Opcode opcode, Stream stream, Action completed)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var cache = new Dictionary <CompressionMethod, Stream> ();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Broadcast (opcode, stream, cache);
|
|
|
|
|
|
|
|
if (completed != null)
|
|
|
|
|
|
|
|
completed ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
|
|
|
_logger.Fatal (ex.ToString ());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
|
|
|
|
foreach (var cached in cache.Values)
|
|
|
|
|
|
|
|
cached.Dispose ();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cache.Clear ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void broadcastAsync (Opcode opcode, byte [] data, Action completed)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ThreadPool.QueueUserWorkItem (
|
|
|
|
|
|
|
|
state => broadcast (opcode, data, completed));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void broadcastAsync (Opcode opcode, Stream stream, Action completed)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ThreadPool.QueueUserWorkItem (
|
|
|
|
|
|
|
|
state => broadcast (opcode, stream, completed));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string createID ()
|
|
|
|
private static string createID ()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Guid.NewGuid ().ToString ("N");
|
|
|
|
return Guid.NewGuid ().ToString ("N");
|
|
|
|
@ -232,22 +284,19 @@ namespace WebSocketSharp.Server
|
|
|
|
private void setSweepTimer (double interval)
|
|
|
|
private void setSweepTimer (double interval)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_sweepTimer = new System.Timers.Timer (interval);
|
|
|
|
_sweepTimer = new System.Timers.Timer (interval);
|
|
|
|
_sweepTimer.Elapsed += (sender, e) =>
|
|
|
|
_sweepTimer.Elapsed += (sender, e) => Sweep ();
|
|
|
|
{
|
|
|
|
|
|
|
|
Sweep ();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool tryGetSession (string id, out IWebSocketSession session)
|
|
|
|
private bool tryGetSession (string id, out IWebSocketSession session)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
bool result;
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
result = _sessions.TryGetValue (id, out session);
|
|
|
|
result = _sessions.TryGetValue (id, out session);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
if (!result)
|
|
|
|
_logger.Error ("A WebSocket session with the specified ID not found.\nID: " + id);
|
|
|
|
_logger.Error (
|
|
|
|
|
|
|
|
"A WebSocket session with the specified ID not found.\nID: " + id);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -258,8 +307,7 @@ namespace WebSocketSharp.Server
|
|
|
|
|
|
|
|
|
|
|
|
internal string Add (IWebSocketSession session)
|
|
|
|
internal string Add (IWebSocketSession session)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
@ -270,46 +318,10 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void Broadcast (Opcode opcode, byte [] data, Action completed)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var cache = new Dictionary<CompressionMethod, byte []> ();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Broadcast (opcode, data, cache);
|
|
|
|
|
|
|
|
if (completed != null)
|
|
|
|
|
|
|
|
completed ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
|
|
|
_logger.Fatal (ex.ToString ());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
|
|
|
|
cache.Clear ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal void Broadcast (Opcode opcode, Stream stream, Action completed)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var cache = new Dictionary <CompressionMethod, Stream> ();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Broadcast (opcode, stream, cache);
|
|
|
|
|
|
|
|
if (completed != null)
|
|
|
|
|
|
|
|
completed ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
|
|
|
_logger.Fatal (ex.ToString ());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
|
|
|
|
foreach (var cached in cache.Values)
|
|
|
|
|
|
|
|
cached.Dispose ();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cache.Clear ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal void Broadcast (
|
|
|
|
internal void Broadcast (
|
|
|
|
Opcode opcode, byte [] data, Dictionary<CompressionMethod, byte []> cache)
|
|
|
|
Opcode opcode, byte [] data, Dictionary<CompressionMethod, byte []> cache)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (var session in Sessions)
|
|
|
|
foreach (var session in Sessions) {
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
@ -320,8 +332,7 @@ namespace WebSocketSharp.Server
|
|
|
|
internal void Broadcast (
|
|
|
|
internal void Broadcast (
|
|
|
|
Opcode opcode, Stream stream, Dictionary <CompressionMethod, Stream> cache)
|
|
|
|
Opcode opcode, Stream stream, Dictionary <CompressionMethod, Stream> cache)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (var session in Sessions)
|
|
|
|
foreach (var session in Sessions) {
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
@ -329,35 +340,16 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void BroadcastAsync (Opcode opcode, byte [] data, Action completed)
|
|
|
|
internal Dictionary<string, bool> Broadping (
|
|
|
|
{
|
|
|
|
byte [] frameAsBytes, int timeOut)
|
|
|
|
WaitCallback callback = state =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Broadcast (opcode, data, completed);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ThreadPool.QueueUserWorkItem (callback);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal void BroadcastAsync (Opcode opcode, Stream stream, Action completed)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
WaitCallback callback = state =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Broadcast (opcode, stream, completed);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ThreadPool.QueueUserWorkItem (callback);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal Dictionary<string, bool> Broadping (byte [] frameAsBytes, int timeOut)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var result = new Dictionary<string, bool> ();
|
|
|
|
var result = new Dictionary<string, bool> ();
|
|
|
|
foreach (var session in Sessions)
|
|
|
|
foreach (var session in Sessions) {
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
result.Add (session.ID, session.Context.WebSocket.Ping (frameAsBytes, timeOut));
|
|
|
|
result.Add (
|
|
|
|
|
|
|
|
session.ID, session.Context.WebSocket.Ping (frameAsBytes, timeOut));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
@ -365,8 +357,7 @@ namespace WebSocketSharp.Server
|
|
|
|
|
|
|
|
|
|
|
|
internal bool Remove (string id)
|
|
|
|
internal bool Remove (string id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
return _sessions.Remove (id);
|
|
|
|
return _sessions.Remove (id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -381,17 +372,17 @@ namespace WebSocketSharp.Server
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var payload = new PayloadData (data);
|
|
|
|
var payload = new PayloadData (data);
|
|
|
|
var args = new CloseEventArgs (payload);
|
|
|
|
var args = new CloseEventArgs (payload);
|
|
|
|
var frameAsBytes = send
|
|
|
|
var frameAsBytes =
|
|
|
|
? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray ()
|
|
|
|
send
|
|
|
|
: null;
|
|
|
|
? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray ()
|
|
|
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
|
|
Stop (args, frameAsBytes);
|
|
|
|
Stop (args, frameAsBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void Stop (CloseEventArgs args, byte [] frameAsBytes)
|
|
|
|
internal void Stop (CloseEventArgs args, byte [] frameAsBytes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_state = ServerState.SHUTDOWN;
|
|
|
|
_state = ServerState.SHUTDOWN;
|
|
|
|
|
|
|
|
|
|
|
|
_sweepTimer.Enabled = false;
|
|
|
|
_sweepTimer.Enabled = false;
|
|
|
|
@ -407,177 +398,170 @@ namespace WebSocketSharp.Server
|
|
|
|
#region Public Methods
|
|
|
|
#region Public Methods
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Broadcasts a binary <paramref name="data"/> to all clients of a WebSocket service.
|
|
|
|
/// Broadcasts a binary <paramref name="data"/> to all clients of the
|
|
|
|
|
|
|
|
/// WebSocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
|
|
|
|
/// This method does not wait for the broadcast to be complete.
|
|
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// An array of <see cref="byte"/> that contains a binary data to broadcast.
|
|
|
|
/// An array of <see cref="byte"/> that contains the binary data to broadcast.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void Broadcast (byte [] data)
|
|
|
|
public void Broadcast (byte [] data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Broadcast (data, null);
|
|
|
|
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
|
|
|
|
|
|
|
|
if (msg != null) {
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (data.LongLength <= WebSocket.FragmentLength)
|
|
|
|
|
|
|
|
broadcast (Opcode.BINARY, data, null);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
broadcast (Opcode.BINARY, new MemoryStream (data), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Broadcasts a text <paramref name="data"/> to all clients of a WebSocket service.
|
|
|
|
/// Broadcasts a text <paramref name="data"/> to all clients of the WebSocket
|
|
|
|
|
|
|
|
/// service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
|
|
|
|
/// This method does not wait for the broadcast to be complete.
|
|
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// A <see cref="string"/> that contains a text data to broadcast.
|
|
|
|
/// A <see cref="string"/> that represents the text data to broadcast.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void Broadcast (string data)
|
|
|
|
public void Broadcast (string data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Broadcast (data, null);
|
|
|
|
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
|
|
|
|
|
|
|
|
if (msg != null) {
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var rawData = Encoding.UTF8.GetBytes (data);
|
|
|
|
|
|
|
|
if (rawData.LongLength <= WebSocket.FragmentLength)
|
|
|
|
|
|
|
|
broadcast (Opcode.TEXT, rawData, null);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
broadcast (Opcode.TEXT, new MemoryStream (rawData), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Broadcasts a binary <paramref name="data"/> to all clients of a WebSocket service.
|
|
|
|
/// Broadcasts a binary <paramref name="data"/> asynchronously to all clients
|
|
|
|
|
|
|
|
/// of the WebSocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <remarks>
|
|
|
|
/// This method does not wait for the broadcast to be complete.
|
|
|
|
/// This method doesn't wait for the broadcast to be complete.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// An array of <see cref="byte"/> that contains a binary data to broadcast.
|
|
|
|
/// An array of <see cref="byte"/> that contains the binary data to broadcast.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="completed">
|
|
|
|
/// <param name="completed">
|
|
|
|
/// A <see cref="Action"/> delegate that references the method(s) called when
|
|
|
|
/// A <see cref="Action"/> delegate that references the method(s) called when
|
|
|
|
/// the broadcast is complete.
|
|
|
|
/// the broadcast is complete.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void Broadcast (byte [] data, Action completed)
|
|
|
|
public void BroadcastAsync (byte [] data, Action completed)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
|
|
|
|
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
|
|
|
|
if (msg != null)
|
|
|
|
if (msg != null) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
_logger.Error (msg);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data.LongLength <= WebSocket.FragmentLength)
|
|
|
|
if (data.LongLength <= WebSocket.FragmentLength)
|
|
|
|
BroadcastAsync (Opcode.BINARY, data, completed);
|
|
|
|
broadcastAsync (Opcode.BINARY, data, completed);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
BroadcastAsync (Opcode.BINARY, new MemoryStream (data), completed);
|
|
|
|
broadcastAsync (Opcode.BINARY, new MemoryStream (data), completed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Broadcasts a text <paramref name="data"/> to all clients of a WebSocket service.
|
|
|
|
/// Broadcasts a text <paramref name="data"/> asynchronously to all clients of
|
|
|
|
|
|
|
|
/// the WebSocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <remarks>
|
|
|
|
/// This method does not wait for the broadcast to be complete.
|
|
|
|
/// This method doesn't wait for the broadcast to be complete.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// A <see cref="string"/> that contains a text data to broadcast.
|
|
|
|
/// A <see cref="string"/> that represents the text data to broadcast.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="completed">
|
|
|
|
/// <param name="completed">
|
|
|
|
/// A <see cref="Action"/> delegate that references the method(s) called when
|
|
|
|
/// A <see cref="Action"/> delegate that references the method(s) called when
|
|
|
|
/// the broadcast is complete.
|
|
|
|
/// the broadcast is complete.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void Broadcast (string data, Action completed)
|
|
|
|
public void BroadcastAsync (string data, Action completed)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
|
|
|
|
var msg = _state.CheckIfStarted () ?? data.CheckIfValidSendData ();
|
|
|
|
if (msg != null)
|
|
|
|
if (msg != null) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
_logger.Error (msg);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var rawData = Encoding.UTF8.GetBytes (data);
|
|
|
|
var rawData = Encoding.UTF8.GetBytes (data);
|
|
|
|
if (rawData.LongLength <= WebSocket.FragmentLength)
|
|
|
|
if (rawData.LongLength <= WebSocket.FragmentLength)
|
|
|
|
BroadcastAsync (Opcode.TEXT, rawData, completed);
|
|
|
|
broadcastAsync (Opcode.TEXT, rawData, completed);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
BroadcastAsync (Opcode.TEXT, new MemoryStream (rawData), completed);
|
|
|
|
broadcastAsync (Opcode.TEXT, new MemoryStream (rawData), completed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Broadcasts a binary data from the specified <see cref="Stream"/>
|
|
|
|
|
|
|
|
/// to all clients of a WebSocket service.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
|
|
/// This method does not wait for the broadcast to be complete.
|
|
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
|
|
/// <param name="stream">
|
|
|
|
|
|
|
|
/// A <see cref="Stream"/> object from which contains a binary data to broadcast.
|
|
|
|
|
|
|
|
/// </param>
|
|
|
|
|
|
|
|
/// <param name="length">
|
|
|
|
|
|
|
|
/// An <see cref="int"/> that contains the number of bytes to broadcast.
|
|
|
|
|
|
|
|
/// </param>
|
|
|
|
|
|
|
|
public void Broadcast (Stream stream, int length)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Broadcast (stream, length, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Broadcasts a binary data from the specified <see cref="Stream"/>
|
|
|
|
/// Broadcasts a binary data from the specified <see cref="Stream"/>
|
|
|
|
/// to all clients of a WebSocket service.
|
|
|
|
/// asynchronously to all clients of the WebSocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// <remarks>
|
|
|
|
/// This method does not wait for the broadcast to be complete.
|
|
|
|
/// This method doesn't wait for the broadcast to be complete.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="stream">
|
|
|
|
/// <param name="stream">
|
|
|
|
/// A <see cref="Stream"/> object from which contains a binary data to broadcast.
|
|
|
|
/// A <see cref="Stream"/> object from which contains the binary data to
|
|
|
|
|
|
|
|
/// broadcast.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="length">
|
|
|
|
/// <param name="length">
|
|
|
|
/// An <see cref="int"/> that contains the number of bytes to broadcast.
|
|
|
|
/// An <see cref="int"/> that represents the number of bytes to broadcast.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="completed">
|
|
|
|
/// <param name="completed">
|
|
|
|
/// A <see cref="Action"/> delegate that references the method(s) called when
|
|
|
|
/// A <see cref="Action"/> delegate that references the method(s) called when
|
|
|
|
/// the broadcast is complete.
|
|
|
|
/// the broadcast is complete.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void Broadcast (Stream stream, int length, Action completed)
|
|
|
|
public void BroadcastAsync (Stream stream, int length, Action completed)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var msg = _state.CheckIfStarted () ??
|
|
|
|
var msg = _state.CheckIfStarted () ??
|
|
|
|
stream.CheckIfCanRead () ??
|
|
|
|
stream.CheckIfCanRead () ??
|
|
|
|
(length < 1 ? "'length' must be greater than 0." : null);
|
|
|
|
(length < 1 ? "'length' must be greater than 0." : null);
|
|
|
|
|
|
|
|
|
|
|
|
if (msg != null)
|
|
|
|
if (msg != null) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
_logger.Error (msg);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stream.ReadBytesAsync (
|
|
|
|
stream.ReadBytesAsync (
|
|
|
|
length,
|
|
|
|
length,
|
|
|
|
data =>
|
|
|
|
data => {
|
|
|
|
{
|
|
|
|
|
|
|
|
var len = data.Length;
|
|
|
|
var len = data.Length;
|
|
|
|
if (len == 0)
|
|
|
|
if (len == 0) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Error ("A data cannot be read from 'stream'.");
|
|
|
|
_logger.Error ("A data cannot be read from 'stream'.");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (len < length)
|
|
|
|
if (len < length)
|
|
|
|
_logger.Warn (String.Format (
|
|
|
|
_logger.Warn (
|
|
|
|
"A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
|
|
|
|
String.Format (
|
|
|
|
length,
|
|
|
|
"A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
|
|
|
|
len));
|
|
|
|
length,
|
|
|
|
|
|
|
|
len));
|
|
|
|
|
|
|
|
|
|
|
|
if (len <= WebSocket.FragmentLength)
|
|
|
|
if (len <= WebSocket.FragmentLength)
|
|
|
|
Broadcast (Opcode.BINARY, data, completed);
|
|
|
|
broadcast (Opcode.BINARY, data, completed);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
Broadcast (Opcode.BINARY, new MemoryStream (data), completed);
|
|
|
|
broadcast (Opcode.BINARY, new MemoryStream (data), completed);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ex =>
|
|
|
|
ex => _logger.Fatal (ex.ToString ()));
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Fatal (ex.ToString ());
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Sends Pings to all clients of a WebSocket service.
|
|
|
|
/// Sends Pings to all clients of the WebSocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// <returns>
|
|
|
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and
|
|
|
|
/// A Dictionary<string, bool> that contains the collection of pairs of
|
|
|
|
/// value indicating whether the WebSocket service received a Pong from each client in a time.
|
|
|
|
/// session ID and value indicating whether the WebSocket service received a
|
|
|
|
|
|
|
|
/// Pong from each client in a time.
|
|
|
|
/// </returns>
|
|
|
|
/// </returns>
|
|
|
|
public Dictionary<string, bool> Broadping ()
|
|
|
|
public Dictionary<string, bool> Broadping ()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var msg = _state.CheckIfStarted ();
|
|
|
|
var msg = _state.CheckIfStarted ();
|
|
|
|
if (msg != null)
|
|
|
|
if (msg != null) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
_logger.Error (msg);
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -587,14 +571,15 @@ namespace WebSocketSharp.Server
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Sends Pings with the specified <paramref name="message"/> to all clients
|
|
|
|
/// Sends Pings with the specified <paramref name="message"/> to all clients
|
|
|
|
/// of a WebSocket service.
|
|
|
|
/// of the WebSocket service.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// <returns>
|
|
|
|
/// A Dictionary<string, bool> that contains the collection of pairs of session ID and
|
|
|
|
/// A Dictionary<string, bool> that contains the collection of pairs of
|
|
|
|
/// value indicating whether the WebSocket service received a Pong from each client in a time.
|
|
|
|
/// session ID and value indicating whether the 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 represents the message to send.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public Dictionary<string, bool> Broadping (string message)
|
|
|
|
public Dictionary<string, bool> Broadping (string message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -605,20 +590,20 @@ namespace WebSocketSharp.Server
|
|
|
|
var msg = _state.CheckIfStarted () ??
|
|
|
|
var msg = _state.CheckIfStarted () ??
|
|
|
|
(data = Encoding.UTF8.GetBytes (message)).CheckIfValidPingData ();
|
|
|
|
(data = Encoding.UTF8.GetBytes (message)).CheckIfValidPingData ();
|
|
|
|
|
|
|
|
|
|
|
|
if (msg != null)
|
|
|
|
if (msg != null) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
_logger.Error (msg);
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Broadping (WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000);
|
|
|
|
return Broadping (
|
|
|
|
|
|
|
|
WsFrame.CreatePingFrame (Mask.UNMASK, data).ToByteArray (), 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Closes the session with the specified <paramref name="id"/>.
|
|
|
|
/// Closes the session with the specified <paramref name="id"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that contains a session ID to find.
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to close.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void CloseSession (string id)
|
|
|
|
public void CloseSession (string id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -628,17 +613,17 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Closes the session with the specified <paramref name="id"/>, <paramref name="code"/>
|
|
|
|
/// Closes the session with the specified <paramref name="id"/>,
|
|
|
|
/// and <paramref name="reason"/>.
|
|
|
|
/// <paramref name="code"/> and <paramref name="reason"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that contains a session ID to find.
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to close.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="code">
|
|
|
|
/// <param name="code">
|
|
|
|
/// A <see cref="ushort"/> that contains a status code indicating the reason for closure.
|
|
|
|
/// A <see cref="ushort"/> that represents the status code for closure.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="reason">
|
|
|
|
/// <param name="reason">
|
|
|
|
/// A <see cref="string"/> that contains the reason for closure.
|
|
|
|
/// A <see cref="string"/> that represents the reason for closure.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void CloseSession (string id, ushort code, string reason)
|
|
|
|
public void CloseSession (string id, ushort code, string reason)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -648,17 +633,18 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Closes the session with the specified <paramref name="id"/>, <paramref name="code"/>
|
|
|
|
/// Closes the session with the specified <paramref name="id"/>,
|
|
|
|
/// and <paramref name="reason"/>.
|
|
|
|
/// <paramref name="code"/> and <paramref name="reason"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that contains a session ID to find.
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to close.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="code">
|
|
|
|
/// <param name="code">
|
|
|
|
/// One of the <see cref="CloseStatusCode"/> values that indicate the status codes for closure.
|
|
|
|
/// One of the <see cref="CloseStatusCode"/> values that indicate the status
|
|
|
|
|
|
|
|
/// codes for closure.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="reason">
|
|
|
|
/// <param name="reason">
|
|
|
|
/// A <see cref="string"/> that contains the reason for closure.
|
|
|
|
/// A <see cref="string"/> that represents the reason for closure.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public void CloseSession (string id, CloseStatusCode code, string reason)
|
|
|
|
public void CloseSession (string id, CloseStatusCode code, string reason)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -668,20 +654,22 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Sends a Ping to the client associated with the specified <paramref name="id"/>.
|
|
|
|
/// Sends a Ping to the client associated with the specified
|
|
|
|
|
|
|
|
/// <paramref name="id"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// <returns>
|
|
|
|
/// <c>true</c> if the WebSocket service receives a Pong from the client in a time;
|
|
|
|
/// <c>true</c> if the WebSocket service receives a Pong from the client in a
|
|
|
|
/// otherwise, <c>false</c>.
|
|
|
|
/// time; otherwise, <c>false</c>.
|
|
|
|
/// </returns>
|
|
|
|
/// </returns>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that contains a session ID that represents the destination
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to send the
|
|
|
|
/// for the Ping.
|
|
|
|
/// Ping to.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public bool PingTo (string id)
|
|
|
|
public bool PingTo (string id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
IWebSocketSession session;
|
|
|
|
IWebSocketSession session;
|
|
|
|
return TryGetSession (id, out session) && session.Context.WebSocket.Ping ();
|
|
|
|
return TryGetSession (id, out session) &&
|
|
|
|
|
|
|
|
session.Context.WebSocket.Ping ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
@ -689,20 +677,21 @@ namespace WebSocketSharp.Server
|
|
|
|
/// associated with the specified <paramref name="id"/>.
|
|
|
|
/// associated with the specified <paramref name="id"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// <returns>
|
|
|
|
/// <c>true</c> if the WebSocket service receives a Pong from the client in a time;
|
|
|
|
/// <c>true</c> if the WebSocket service receives a Pong from the client in a
|
|
|
|
/// otherwise, <c>false</c>.
|
|
|
|
/// time; otherwise, <c>false</c>.
|
|
|
|
/// </returns>
|
|
|
|
/// </returns>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that contains a session ID that represents the destination
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to send the
|
|
|
|
/// for the Ping.
|
|
|
|
/// Ping to.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="message">
|
|
|
|
/// <param name="message">
|
|
|
|
/// A <see cref="string"/> that contains a message to send.
|
|
|
|
/// A <see cref="string"/> that represents the message to send.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public bool PingTo (string id, string message)
|
|
|
|
public bool PingTo (string id, string message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
IWebSocketSession session;
|
|
|
|
IWebSocketSession session;
|
|
|
|
return TryGetSession (id, out session) && session.Context.WebSocket.Ping (message);
|
|
|
|
return TryGetSession (id, out session) &&
|
|
|
|
|
|
|
|
session.Context.WebSocket.Ping (message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
@ -710,8 +699,8 @@ namespace WebSocketSharp.Server
|
|
|
|
/// specified <paramref name="id"/>.
|
|
|
|
/// specified <paramref name="id"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that represents the session ID that represents the
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to send the
|
|
|
|
/// destination for the data.
|
|
|
|
/// data to.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// An array of <see cref="byte"/> that contains the binary data to send.
|
|
|
|
/// An array of <see cref="byte"/> that contains the binary data to send.
|
|
|
|
@ -728,8 +717,8 @@ namespace WebSocketSharp.Server
|
|
|
|
/// specified <paramref name="id"/>.
|
|
|
|
/// specified <paramref name="id"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that represents the session ID that represents the
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to send the
|
|
|
|
/// destination for the data.
|
|
|
|
/// data to.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// A <see cref="string"/> that represents the text data to send.
|
|
|
|
/// A <see cref="string"/> that represents the text data to send.
|
|
|
|
@ -749,8 +738,8 @@ namespace WebSocketSharp.Server
|
|
|
|
/// This method doesn't wait for the send to be complete.
|
|
|
|
/// This method doesn't wait for the send to be complete.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that represents the session ID that represents the
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to send the
|
|
|
|
/// destination for the data.
|
|
|
|
/// data to.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// An array of <see cref="byte"/> that contains the binary data to send.
|
|
|
|
/// An array of <see cref="byte"/> that contains the binary data to send.
|
|
|
|
@ -776,8 +765,8 @@ namespace WebSocketSharp.Server
|
|
|
|
/// This method doesn't wait for the send to be complete.
|
|
|
|
/// This method doesn't wait for the send to be complete.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that represents the session ID that represents the
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to send the
|
|
|
|
/// destination for the data.
|
|
|
|
/// data to.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="data">
|
|
|
|
/// <param name="data">
|
|
|
|
/// A <see cref="string"/> that represents the text data to send.
|
|
|
|
/// A <see cref="string"/> that represents the text data to send.
|
|
|
|
@ -803,8 +792,8 @@ namespace WebSocketSharp.Server
|
|
|
|
/// This method doesn't wait for the send to be complete.
|
|
|
|
/// This method doesn't wait for the send to be complete.
|
|
|
|
/// </remarks>
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that represents the session ID that represents the
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to send the
|
|
|
|
/// destination for the data.
|
|
|
|
/// data to.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="stream">
|
|
|
|
/// <param name="stream">
|
|
|
|
/// A <see cref="Stream"/> object from which contains the binary data to send.
|
|
|
|
/// A <see cref="Stream"/> object from which contains the binary data to send.
|
|
|
|
@ -834,19 +823,15 @@ namespace WebSocketSharp.Server
|
|
|
|
if (_state != ServerState.START || _sweeping || Count == 0)
|
|
|
|
if (_state != ServerState.START || _sweeping || Count == 0)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
lock (_forSweep)
|
|
|
|
lock (_forSweep) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_sweeping = true;
|
|
|
|
_sweeping = true;
|
|
|
|
foreach (var id in InactiveIDs)
|
|
|
|
foreach (var id in InactiveIDs) {
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
if (_state != ServerState.START)
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
lock (_sync)
|
|
|
|
lock (_sync) {
|
|
|
|
{
|
|
|
|
|
|
|
|
IWebSocketSession session;
|
|
|
|
IWebSocketSession session;
|
|
|
|
if (_sessions.TryGetValue (id, out session))
|
|
|
|
if (_sessions.TryGetValue (id, out session)) {
|
|
|
|
{
|
|
|
|
|
|
|
|
var state = session.State;
|
|
|
|
var state = session.State;
|
|
|
|
if (state == WebSocketState.OPEN)
|
|
|
|
if (state == WebSocketState.OPEN)
|
|
|
|
session.Context.WebSocket.Close (CloseStatusCode.ABNORMAL);
|
|
|
|
session.Context.WebSocket.Close (CloseStatusCode.ABNORMAL);
|
|
|
|
@ -863,24 +848,24 @@ namespace WebSocketSharp.Server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Tries to get a WebSocket session information with the specified <paramref name="id"/>.
|
|
|
|
/// Tries to get a WebSocket session information with the specified
|
|
|
|
|
|
|
|
/// <paramref name="id"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// <returns>
|
|
|
|
/// <c>true</c> if the session is successfully found; otherwise, <c>false</c>.
|
|
|
|
/// <c>true</c> if the session is successfully found; otherwise, <c>false</c>.
|
|
|
|
/// </returns>
|
|
|
|
/// </returns>
|
|
|
|
/// <param name="id">
|
|
|
|
/// <param name="id">
|
|
|
|
/// A <see cref="string"/> that contains an ID of the session to find.
|
|
|
|
/// A <see cref="string"/> that represents the ID of the session to get.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="session">
|
|
|
|
/// <param name="session">
|
|
|
|
/// When this method returns, a <see cref="IWebSocketSession"/> instance that contains
|
|
|
|
/// When this method returns, a <see cref="IWebSocketSession"/> instance that
|
|
|
|
/// the session information if the session is successfully found; otherwise, <see langword="null"/>.
|
|
|
|
/// represents the session if it's successfully found; otherwise,
|
|
|
|
/// This parameter is passed uninitialized.
|
|
|
|
/// <see langword="null"/>. This parameter is passed uninitialized.
|
|
|
|
/// </param>
|
|
|
|
/// </param>
|
|
|
|
public bool TryGetSession (string id, out IWebSocketSession session)
|
|
|
|
public bool TryGetSession (string id, out IWebSocketSession session)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var msg = _state.CheckIfStarted () ?? id.CheckIfValidSessionID ();
|
|
|
|
var msg = _state.CheckIfStarted () ?? id.CheckIfValidSessionID ();
|
|
|
|
if (msg != null)
|
|
|
|
if (msg != null) {
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Error (msg);
|
|
|
|
_logger.Error (msg);
|
|
|
|
session = null;
|
|
|
|
session = null;
|
|
|
|
|
|
|
|
|
|
|
|
|