From 42a44226d55183ed23dda48d79055f1408d3df38 Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 16 Sep 2013 14:33:26 +0900 Subject: [PATCH] Moved some methods (e.g. Broadcast method) from WebSocketService.cs and WebSocketServiceHost.cs to WebSocketSessionManager.cs --- Example2/Chat.cs | 4 +- Example3/Chat.cs | 4 +- README.md | 2 +- .../Server/IWebSocketServiceHost.cs | 146 ------- websocket-sharp/Server/WebSocketService.cs | 226 +---------- .../Server/WebSocketServiceHost.cs | 289 -------------- .../Server/WebSocketServiceHostManager.cs | 66 ++-- .../Server/WebSocketSessionManager.cs | 357 ++++++++++++++---- 8 files changed, 323 insertions(+), 771 deletions(-) diff --git a/Example2/Chat.cs b/Example2/Chat.cs index a87bc389..91f450ad 100644 --- a/Example2/Chat.cs +++ b/Example2/Chat.cs @@ -28,12 +28,12 @@ namespace Example2 protected override void OnMessage (MessageEventArgs e) { - Broadcast (String.Format ("{0}: {1}", _name, e.Data)); + Sessions.Broadcast (String.Format ("{0}: {1}", _name, e.Data)); } protected override void OnClose (CloseEventArgs e) { - Broadcast (String.Format ("{0} got logged off...", _name)); + Sessions.Broadcast (String.Format ("{0} got logged off...", _name)); } } } diff --git a/Example3/Chat.cs b/Example3/Chat.cs index b806693c..d4036f67 100644 --- a/Example3/Chat.cs +++ b/Example3/Chat.cs @@ -28,12 +28,12 @@ namespace Example3 protected override void OnMessage (MessageEventArgs e) { - Broadcast (String.Format ("{0}: {1}", _name, e.Data)); + Sessions.Broadcast (String.Format ("{0}: {1}", _name, e.Data)); } protected override void OnClose (CloseEventArgs e) { - Broadcast (String.Format ("{0} got logged off...", _name)); + Sessions.Broadcast (String.Format ("{0} got logged off...", _name)); } } } diff --git a/README.md b/README.md index fc8c4bd4..8000cefd 100644 --- a/README.md +++ b/README.md @@ -237,7 +237,7 @@ public class Chat : WebSocketService { protected override void OnMessage (MessageEventArgs e) { - Broadcast (e.Data); + Sessions.Broadcast (e.Data); } } ``` diff --git a/websocket-sharp/Server/IWebSocketServiceHost.cs b/websocket-sharp/Server/IWebSocketServiceHost.cs index 02ac649c..e6b2f883 100644 --- a/websocket-sharp/Server/IWebSocketServiceHost.cs +++ b/websocket-sharp/Server/IWebSocketServiceHost.cs @@ -70,151 +70,5 @@ namespace WebSocketSharp.Server /// A that contains the WebSocket connection request objects to bind. /// void BindWebSocket (WebSocketContext context); - - /// - /// Broadcasts the specified array of to all clients of the WebSocket service host. - /// - /// - /// An array of to broadcast. - /// - void Broadcast (byte [] data); - - /// - /// Broadcasts the specified to all clients of the WebSocket service host. - /// - /// - /// A to broadcast. - /// - void Broadcast (string data); - - /// - /// Sends Pings to all clients of the WebSocket service host. - /// - /// - /// 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. - /// - Dictionary Broadping (); - - /// - /// Sends Pings with the specified to all clients of the WebSocket service host. - /// - /// - /// 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. - /// - /// - /// An array of that contains a message data to send. - /// - Dictionary Broadping (byte [] data); - - /// - /// Close the session with the specified . - /// - /// - /// A that contains a session ID to find. - /// - void CloseSession (string id); - - /// - /// Close the session with the specified , - /// and . - /// - /// - /// A that contains a status code indicating the reason for closure. - /// - /// - /// A that contains the reason for closure. - /// - /// - /// A that contains a session ID to find. - /// - void CloseSession (ushort code, string reason, string id); - - /// - /// Close the session with the specified , - /// and . - /// - /// - /// A that contains a status code indicating the reason for closure. - /// - /// - /// A that contains the reason for closure. - /// - /// - /// A that contains a session ID to find. - /// - void CloseSession (CloseStatusCode code, string reason, string id); - - /// - /// Sends a Ping to the client associated with the specified . - /// - /// - /// true if the WebSocket service host receives a Pong from the client in a time; - /// otherwise, false. - /// - /// - /// A that contains a session ID that represents the destination for the Ping. - /// - bool PingTo (string id); - - /// - /// Sends a Ping with the specified to the client associated with - /// the specified . - /// - /// - /// true if the WebSocket service host receives a Pong from the client in a time; - /// otherwise, false. - /// - /// - /// A that contains a message to send. - /// - /// - /// A that contains a session ID that represents the destination for the Ping. - /// - bool PingTo (string message, string id); - - /// - /// Sends a binary to the client associated with the specified - /// . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// An array of that contains a binary data to send. - /// - /// - /// A that contains a session ID that represents the destination for the data. - /// - bool SendTo (byte [] data, string id); - - /// - /// Sends a text to the client associated with the specified - /// . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// A that contains a text data to send. - /// - /// - /// A that contains a session ID that represents the destination for the data. - /// - bool SendTo (string data, string id); - - /// - /// Stops the WebSocket service host. - /// - void Stop (); - - /// - /// Stops the WebSocket service host with the specified array of . - /// - /// - /// An array of that contains the reason for stop. - /// - void Stop (byte [] data); } } diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index 0d3f6ae7..04c15e77 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -95,8 +95,7 @@ namespace WebSocketSharp.Server /// Gets the manager of the sessions to the WebSocket service. /// /// - /// A that manages the sessions - /// to the WebSocket service. + /// A that manages the sessions to the WebSocket service. /// protected WebSocketSessionManager Sessions { get { @@ -226,111 +225,10 @@ namespace WebSocketSharp.Server IsBound = true; } - internal bool Ping (byte [] data) - { - return _websocket.Ping (data); - } - - internal void Stop (byte [] data) - { - _websocket.Close (data); - } - #endregion #region Protected Methods - /// - /// Broadcasts the specified array of to all clients of the WebSocket service. - /// - /// - /// An array of to broadcast. - /// - 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); - } - - /// - /// Broadcasts the specified to all clients of the WebSocket service. - /// - /// - /// A to broadcast. - /// - 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); - } - - /// - /// Sends Pings to all clients of the WebSocket service. - /// - /// - /// 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. - /// - protected virtual Dictionary Broadping () - { - return IsBound - ? _sessions.Broadping (new byte [] {}) - : null; - } - - /// - /// Sends Pings with the specified to all clients of the WebSocket service. - /// - /// - /// 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. - /// - /// - /// A that contains a message to send. - /// - protected virtual Dictionary Broadping (string message) - { - if (!IsBound) - return null; - - if (message == null || message.Length == 0) - return _sessions.Broadping (new byte [] {}); - - var data = Encoding.UTF8.GetBytes (message); - var msg = data.CheckIfValidPingData (); - if (msg != null) - { - Log.Error (msg); - Error (msg); - - return null; - } - - return _sessions.Broadping (data); - } - /// /// Calls the method with the specified . /// @@ -384,124 +282,6 @@ namespace WebSocketSharp.Server { } - /// - /// Sends a Ping to the client associated with the specified . - /// - /// - /// true if the WebSocket service receives a Pong from the client in a time; - /// otherwise, false. - /// - /// - /// A that contains a session ID that represents the destination for the Ping. - /// - 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); - } - - /// - /// Sends a Ping with the specified to the client associated with - /// the specified . - /// - /// - /// true if the WebSocket service receives a Pong from the client in a time; - /// otherwise, false. - /// - /// - /// A that contains a message to send. - /// - /// - /// A that contains a session ID that represents the destination for the Ping. - /// - 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); - } - - /// - /// Sends a binary to the client associated with the specified - /// . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// An array of that contains a binary data to send. - /// - /// - /// A that contains a session ID that represents the destination for the data. - /// - 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); - } - - /// - /// Sends a text to the client associated with the specified - /// . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// A that contains a text data to send. - /// - /// - /// A that contains a session ID that represents the destination for the data. - /// - 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); - } - /// /// Validates the cookies used in the WebSocket connection request. /// @@ -536,7 +316,7 @@ namespace WebSocketSharp.Server /// true if the current instance receives a Pong /// from the client in a time; otherwise, false. /// - public virtual bool Ping () + public bool Ping () { return IsBound ? _websocket.Ping () @@ -554,7 +334,7 @@ namespace WebSocketSharp.Server /// /// A that contains a message to send. /// - public virtual bool Ping (string message) + public bool Ping (string message) { return IsBound ? _websocket.Ping (message) diff --git a/websocket-sharp/Server/WebSocketServiceHost.cs b/websocket-sharp/Server/WebSocketServiceHost.cs index f74855e5..65c8dbab 100644 --- a/websocket-sharp/Server/WebSocketServiceHost.cs +++ b/websocket-sharp/Server/WebSocketServiceHost.cs @@ -315,246 +315,6 @@ namespace WebSocketSharp.Server #region Public Methods - /// - /// Broadcasts the specified array of to all clients. - /// - /// - /// An array of to broadcast. - /// - public void Broadcast (byte [] data) - { - var msg = data.CheckIfValidSendData (); - if (msg != null) - { - Log.Error (msg); - return; - } - - _sessions.Broadcast (data); - } - - /// - /// Broadcasts the specified to all clients. - /// - /// - /// A to broadcast. - /// - public void Broadcast (string data) - { - var msg = data.CheckIfValidSendData (); - if (msg != null) - { - Log.Error (msg); - return; - } - - _sessions.Broadcast (data); - } - - /// - /// Sends Pings to all clients. - /// - /// - /// 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. - /// - public Dictionary Broadping () - { - return _sessions.Broadping (new byte [] {}); - } - - /// - /// Sends Pings with the specified to all clients. - /// - /// - /// 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. - /// - /// - /// A that contains a message to send. - /// - public Dictionary Broadping (string message) - { - if (message == null || message.Length == 0) - return _sessions.Broadping (new byte [] {}); - - var data = Encoding.UTF8.GetBytes (message); - var msg = data.CheckIfValidPingData (); - if (msg != null) - { - Log.Error (msg); - return null; - } - - return _sessions.Broadping (data); - } - - /// - /// Close the session with the specified . - /// - /// - /// A that contains a session ID to find. - /// - public void CloseSession (string id) - { - var msg = id.CheckIfValidSessionID (); - if (msg != null) - { - Log.Error (msg); - return; - } - - _sessions.StopServiceInstance (id); - } - - /// - /// Close the session with the specified , - /// and . - /// - /// - /// A that contains a status code indicating the reason for closure. - /// - /// - /// A that contains the reason for closure. - /// - /// - /// A that contains a session ID to find. - /// - 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); - } - - /// - /// Close the session with the specified , - /// and . - /// - /// - /// A that contains a status code indicating the reason for closure. - /// - /// - /// A that contains the reason for closure. - /// - /// - /// A that contains a session ID to find. - /// - 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); - } - - /// - /// Sends a Ping to the client associated with the specified . - /// - /// - /// true if the WebSocket service host receives a Pong from the client in a time; - /// otherwise, false. - /// - /// - /// A that contains a session ID that represents the destination for the Ping. - /// - public bool PingTo (string id) - { - var msg = id.CheckIfValidSessionID (); - if (msg != null) - { - Log.Error (msg); - return false; - } - - return _sessions.PingTo (id); - } - - /// - /// Sends a Ping with the specified to the client associated with - /// the specified . - /// - /// - /// true if the WebSocket service host receives a Pong from the client in a time; - /// otherwise, false. - /// - /// - /// A that contains a message to send. - /// - /// - /// A that contains a session ID that represents the destination for the Ping. - /// - public bool PingTo (string message, string id) - { - var msg = id.CheckIfValidSessionID (); - if (msg != null) - { - Log.Error (msg); - return false; - } - - return _sessions.PingTo (message, id); - } - - /// - /// Sends a binary to the client associated with the specified - /// . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// An array of that contains a binary data to send. - /// - /// - /// A that contains a session ID that represents the destination for the data. - /// - public bool SendTo (byte [] data, string id) - { - var msg = id.CheckIfValidSessionID (); - if (msg != null) - { - Log.Error (msg); - return false; - } - - return _sessions.SendTo (data, id); - } - - /// - /// Sends a text to the client associated with the specified - /// . - /// - /// - /// true if is successfully sent; otherwise, false. - /// - /// - /// A that contains a text data to send. - /// - /// - /// A that contains a session ID that represents the destination for the data. - /// - public bool SendTo (string data, string id) - { - var msg = id.CheckIfValidSessionID (); - if (msg != null) - { - Log.Error (msg); - return false; - } - - return _sessions.SendTo (data, id); - } - /// /// Stops receiving the WebSocket connection requests. /// @@ -618,55 +378,6 @@ namespace WebSocketSharp.Server service.Start (); } - /// - /// Broadcasts the specified array of to all clients. - /// - /// - /// An array of to broadcast. - /// - void IWebSocketServiceHost.Broadcast (byte [] data) - { - _sessions.Broadcast (data); - } - - /// - /// Broadcasts the specified to all clients. - /// - /// - /// A to broadcast. - /// - void IWebSocketServiceHost.Broadcast (string data) - { - _sessions.Broadcast (data); - } - - /// - /// Sends Pings with the specified to all clients. - /// - /// - /// 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. - /// - /// - /// An array of that contains a message data to send. - /// - Dictionary IWebSocketServiceHost.Broadping (byte [] data) - { - return _sessions.Broadping (data); - } - - /// - /// Stops receiving the WebSocket connection requests with the specified array of . - /// - /// - /// An array of that contains the reason for stop. - /// - void IWebSocketServiceHost.Stop (byte [] data) - { - base.Stop (); - _sessions.Stop (data); - } - #endregion } } diff --git a/websocket-sharp/Server/WebSocketServiceHostManager.cs b/websocket-sharp/Server/WebSocketServiceHostManager.cs index adbeb296..dad24bcc 100644 --- a/websocket-sharp/Server/WebSocketServiceHostManager.cs +++ b/websocket-sharp/Server/WebSocketServiceHostManager.cs @@ -131,10 +131,10 @@ namespace WebSocketSharp.Server } /// - /// Gets the collection of each path to the WebSocket services provided by the WebSocket server. + /// Gets the collection of every path to the WebSocket services provided by the WebSocket server. /// /// - /// An IEnumerable<string> that contains the collection of each path to the WebSocket services. + /// An IEnumerable<string> that contains the collection of every path to the WebSocket services. /// public IEnumerable ServicePaths { get { @@ -149,6 +149,15 @@ namespace WebSocketSharp.Server #region Private Methods + private Dictionary> broadping (byte [] data) + { + var result = new Dictionary> (); + foreach (var service in copy ()) + result.Add (service.Key, service.Value.Sessions.BroadpingInternally (data)); + + return result; + } + private Dictionary copy () { lock (_sync) @@ -178,15 +187,6 @@ namespace WebSocketSharp.Server } } - internal Dictionary> Broadping (byte [] data) - { - var result = new Dictionary> (); - foreach (var service in copy ()) - result.Add (service.Key, service.Value.Broadping (data)); - - return result; - } - internal bool Remove (string servicePath) { servicePath = HttpUtility.UrlDecode (servicePath).TrimEndSlash (); @@ -204,7 +204,7 @@ namespace WebSocketSharp.Server _serviceHosts.Remove (servicePath); } - host.Stop (((ushort) CloseStatusCode.AWAY).ToByteArray (ByteOrder.BIG)); + host.Sessions.Stop (((ushort) CloseStatusCode.AWAY).ToByteArray (ByteOrder.BIG)); return true; } @@ -213,7 +213,7 @@ namespace WebSocketSharp.Server lock (_sync) { foreach (var host in _serviceHosts.Values) - host.Stop (); + host.Sessions.Stop (); _serviceHosts.Clear (); } @@ -224,7 +224,7 @@ namespace WebSocketSharp.Server lock (_sync) { foreach (var host in _serviceHosts.Values) - host.Stop (data); + host.Sessions.Stop (data); _serviceHosts.Clear (); } @@ -260,7 +260,7 @@ namespace WebSocketSharp.Server } foreach (var host in ServiceHosts) - host.Broadcast (data); + host.Sessions.BroadcastInternally (data); } /// @@ -280,7 +280,7 @@ namespace WebSocketSharp.Server } foreach (var host in ServiceHosts) - host.Broadcast (data); + host.Sessions.BroadcastInternally (data); } /// @@ -312,7 +312,7 @@ namespace WebSocketSharp.Server return false; } - host.Broadcast (data); + host.Sessions.BroadcastInternally (data); return true; } @@ -345,7 +345,7 @@ namespace WebSocketSharp.Server return false; } - host.Broadcast (data); + host.Sessions.BroadcastInternally (data); return true; } @@ -359,7 +359,7 @@ namespace WebSocketSharp.Server /// public Dictionary> Broadping () { - return Broadping (new byte [] {}); + return broadping (new byte [] {}); } /// @@ -378,7 +378,7 @@ namespace WebSocketSharp.Server public Dictionary> Broadping (string message) { if (message == null || message.Length == 0) - return Broadping (new byte [] {}); + return broadping (new byte [] {}); var data = Encoding.UTF8.GetBytes (message); var msg = data.CheckIfValidPingData (); @@ -388,7 +388,7 @@ namespace WebSocketSharp.Server return null; } - return Broadping (data); + return broadping (data); } /// @@ -418,7 +418,7 @@ namespace WebSocketSharp.Server return null; } - return host.Broadping (new byte [] {}); + return host.Sessions.BroadpingInternally (new byte [] {}); } /// @@ -456,11 +456,11 @@ namespace WebSocketSharp.Server return null; } - return host.Broadping (data); + return host.Sessions.BroadpingInternally (data); } /// - /// Close the session with the specified and + /// Closes the session with the specified and /// . /// /// @@ -485,11 +485,11 @@ namespace WebSocketSharp.Server return; } - host.CloseSession (id); + host.Sessions.CloseSession (id); } /// - /// Close the session with the specified , , + /// Closes the session with the specified , , /// and . /// /// @@ -520,11 +520,11 @@ namespace WebSocketSharp.Server return; } - host.CloseSession (code, reason, id); + host.Sessions.CloseSession (code, reason, id); } /// - /// Close the session with the specified , , + /// Closes the session with the specified , , /// and . /// /// @@ -555,7 +555,7 @@ namespace WebSocketSharp.Server return; } - host.CloseSession (code, reason, id); + host.Sessions.CloseSession (code, reason, id); } /// @@ -646,7 +646,7 @@ namespace WebSocketSharp.Server return false; } - return host.PingTo (id); + return host.Sessions.PingTo (id); } /// @@ -682,7 +682,7 @@ namespace WebSocketSharp.Server return false; } - return host.PingTo (message, id); + return host.Sessions.PingTo (message, id); } /// @@ -717,7 +717,7 @@ namespace WebSocketSharp.Server return false; } - return host.SendTo (data, id); + return host.Sessions.SendTo (data, id); } /// @@ -752,7 +752,7 @@ namespace WebSocketSharp.Server return false; } - return host.SendTo (data, id); + return host.Sessions.SendTo (data, id); } #endregion diff --git a/websocket-sharp/Server/WebSocketSessionManager.cs b/websocket-sharp/Server/WebSocketSessionManager.cs index 8b912a1c..a9f72261 100644 --- a/websocket-sharp/Server/WebSocketSessionManager.cs +++ b/websocket-sharp/Server/WebSocketSessionManager.cs @@ -29,6 +29,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Timers; namespace WebSocketSharp.Server @@ -95,7 +96,7 @@ namespace WebSocketSharp.Server /// public IEnumerable ActiveIDs { get { - return from result in Broadping (new byte [] {}) + return from result in BroadpingInternally (new byte [] {}) where result.Value select result.Key; } @@ -139,7 +140,7 @@ namespace WebSocketSharp.Server /// public IEnumerable InactiveIDs { get { - return from result in Broadping (new byte [] {}) + return from result in BroadpingInternally (new byte [] {}) where !result.Value select result.Key; } @@ -170,7 +171,8 @@ namespace WebSocketSharp.Server return _sessions [id]; } catch { - _logger.Error ("'id' not found.\nid: " + id); + _logger.Error ( + "The WebSocket session with the specified ID not found.\nID: " + id); return null; } } @@ -301,7 +303,7 @@ namespace WebSocketSharp.Server } } - internal void Broadcast (byte [] data) + internal void BroadcastInternally (byte [] data) { if (_stopped) broadcast (data); @@ -309,7 +311,7 @@ namespace WebSocketSharp.Server broadcastAsync (data); } - internal void Broadcast (string data) + internal void BroadcastInternally (string data) { if (_stopped) broadcast (data); @@ -317,155 +319,360 @@ namespace WebSocketSharp.Server broadcastAsync (data); } - internal Dictionary Broadping (byte [] data) + internal Dictionary BroadpingInternally (byte [] data) { var result = new Dictionary (); - foreach (var service in ServiceInstances) - result.Add (service.ID, service.Ping (data)); + foreach (var session in ServiceInstances) + result.Add (session.ID, session.Context.WebSocket.Ping (data)); return result; } - internal bool PingTo (string id) + internal bool Remove (string id) { - WebSocketService service; - if (!TryGetServiceInstance (id, out service)) + lock (_sync) { - _logger.Error ( - "The WebSocket session with the specified ID not found.\nID: " + id); - return false; + return _sessions.Remove (id); } - - return service.Ping (); } - internal bool PingTo (string message, string id) + internal void Stop () { - WebSocketService service; - if (!TryGetServiceInstance (id, out service)) + stopSweepTimer (); + lock (_sync) { - _logger.Error ( - "The WebSocket session with the specified ID not found.\nID: " + id); - return false; + if (_stopped) + return; + + _stopped = true; + foreach (var session in ServiceInstances) + session.Context.WebSocket.Close (); } + } + + internal void Stop (byte [] data) + { + stopSweepTimer (); + lock (_sync) + { + if (_stopped) + return; - return service.Ping (message); + _stopped = true; + foreach (var session in ServiceInstances) + session.Context.WebSocket.Close (data); + } } - internal bool Remove (string id) + internal bool TryGetServiceInstance (string id, out WebSocketService service) { lock (_sync) { - return _sessions.Remove (id); + return _sessions.TryGetValue (id, out service); } } - internal bool SendTo (byte [] data, string id) + #endregion + + #region Public Methods + + /// + /// Broadcasts the specified array of to all clients of the WebSocket service. + /// + /// + /// An array of to broadcast. + /// + public void Broadcast (byte [] data) { - WebSocketService service; - if (!TryGetServiceInstance (id, out service)) + var msg = data.CheckIfValidSendData (); + if (msg != null) { - _logger.Error ( - "The WebSocket session with the specified ID not found.\nID: " + id); - return false; + _logger.Error (msg); + return; } - service.Send (data); - return true; + BroadcastInternally (data); } - internal bool SendTo (string data, string id) + /// + /// Broadcasts the specified to all clients of the WebSocket service. + /// + /// + /// A to broadcast. + /// + public void Broadcast (string data) { - WebSocketService service; - if (!TryGetServiceInstance (id, out service)) + var msg = data.CheckIfValidSendData (); + if (msg != null) + { + _logger.Error (msg); + return; + } + + BroadcastInternally (data); + } + + /// + /// Sends Pings to all clients of the WebSocket service. + /// + /// + /// 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. + /// + public Dictionary Broadping () + { + return BroadpingInternally (new byte [] {}); + } + + /// + /// Sends Pings with the specified to all clients of the WebSocket service. + /// + /// + /// 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. + /// + /// + /// A that contains a message to send. + /// + public Dictionary Broadping (string message) + { + if (message == null || message.Length == 0) + return BroadpingInternally (new byte [] {}); + + var data = Encoding.UTF8.GetBytes (message); + var msg = data.CheckIfValidPingData (); + if (msg != null) + { + _logger.Error (msg); + return null; + } + + return BroadpingInternally (data); + } + + /// + /// Closes the session with the specified . + /// + /// + /// A that contains a session ID to find. + /// + public void CloseSession (string id) + { + var msg = id.CheckIfValidSessionID (); + if (msg != null) + { + _logger.Error (msg); + return; + } + + WebSocketService session; + if (!TryGetServiceInstance (id, out session)) { _logger.Error ( "The WebSocket session with the specified ID not found.\nID: " + id); - return false; + return; } - service.Send (data); - return true; + session.Context.WebSocket.Close (); } - internal void Stop () + /// + /// Closes the session with the specified , + /// and . + /// + /// + /// A that contains a status code indicating the reason for closure. + /// + /// + /// A that contains the reason for closure. + /// + /// + /// A that contains a session ID to find. + /// + public void CloseSession (ushort code, string reason, string id) { - stopSweepTimer (); - lock (_sync) + var msg = id.CheckIfValidSessionID (); + if (msg != null) { - if (_stopped) - return; + _logger.Error (msg); + return; + } - _stopped = true; - foreach (var service in ServiceInstances) - service.Stop (); + WebSocketService session; + if (!TryGetServiceInstance (id, out session)) + { + _logger.Error ( + "The WebSocket session with the specified ID not found.\nID: " + id); + return; } + + session.Context.WebSocket.Close (code, reason); } - internal void Stop (byte [] data) + /// + /// Closes the session with the specified , + /// and . + /// + /// + /// A that contains a status code indicating the reason for closure. + /// + /// + /// A that contains the reason for closure. + /// + /// + /// A that contains a session ID to find. + /// + public void CloseSession (CloseStatusCode code, string reason, string id) { - stopSweepTimer (); - lock (_sync) + var msg = id.CheckIfValidSessionID (); + if (msg != null) { - if (_stopped) - return; + _logger.Error (msg); + return; + } - _stopped = true; - foreach (var service in ServiceInstances) - service.Stop (data); + WebSocketService session; + if (!TryGetServiceInstance (id, out session)) + { + _logger.Error ( + "The WebSocket session with the specified ID not found.\nID: " + id); + return; } + + session.Context.WebSocket.Close (code, reason); } - internal void StopServiceInstance (string id) + /// + /// Sends a Ping to the client associated with the specified . + /// + /// + /// true if the WebSocket service receives a Pong from the client in a time; + /// otherwise, false. + /// + /// + /// A that contains a session ID that represents the destination for the Ping. + /// + public bool PingTo (string id) { - WebSocketService service; - if (!TryGetServiceInstance (id, out service)) + var msg = id.CheckIfValidSessionID (); + if (msg != null) + { + _logger.Error (msg); + return false; + } + + WebSocketService session; + if (!TryGetServiceInstance (id, out session)) { _logger.Error ( "The WebSocket session with the specified ID not found.\nID: " + id); - return; + return false; } - service.Stop (); + return session.Context.WebSocket.Ping (); } - internal void StopServiceInstance (ushort code, string reason, string id) + /// + /// Sends a Ping with the specified to the client associated with + /// the specified . + /// + /// + /// true if the WebSocket service receives a Pong from the client in a time; + /// otherwise, false. + /// + /// + /// A that contains a message to send. + /// + /// + /// A that contains a session ID that represents the destination for the Ping. + /// + public bool PingTo (string message, string id) { - WebSocketService service; - if (!TryGetServiceInstance (id, out service)) + var msg = id.CheckIfValidSessionID (); + if (msg != null) + { + _logger.Error (msg); + return false; + } + + WebSocketService session; + if (!TryGetServiceInstance (id, out session)) { _logger.Error ( "The WebSocket session with the specified ID not found.\nID: " + id); - return; + return false; } - service.Stop (code, reason); + return session.Context.WebSocket.Ping (message); } - internal void StopServiceInstance (CloseStatusCode code, string reason, string id) + /// + /// Sends a binary to the client associated with the specified + /// . + /// + /// + /// true if is successfully sent; otherwise, false. + /// + /// + /// An array of that contains a binary data to send. + /// + /// + /// A that contains a session ID that represents the destination for the data. + /// + public bool SendTo (byte [] data, string id) { + var msg = id.CheckIfValidSessionID (); + if (msg != null) + { + _logger.Error (msg); + return false; + } + WebSocketService service; if (!TryGetServiceInstance (id, out service)) { _logger.Error ( "The WebSocket session with the specified ID not found.\nID: " + id); - return; + return false; } - service.Stop (code, reason); + service.Send (data); + return true; } - internal bool TryGetServiceInstance (string id, out WebSocketService service) + /// + /// Sends a text to the client associated with the specified + /// . + /// + /// + /// true if is successfully sent; otherwise, false. + /// + /// + /// A that contains a text data to send. + /// + /// + /// A that contains a session ID that represents the destination for the data. + /// + public bool SendTo (string data, string id) { - lock (_sync) + var msg = id.CheckIfValidSessionID (); + if (msg != null) { - return _sessions.TryGetValue (id, out service); + _logger.Error (msg); + return false; } - } - #endregion + WebSocketService service; + if (!TryGetServiceInstance (id, out service)) + { + _logger.Error ( + "The WebSocket session with the specified ID not found.\nID: " + id); + return false; + } - #region Public Methods + service.Send (data); + return true; + } /// /// Cleans up the inactive sessions. @@ -490,7 +697,7 @@ namespace WebSocketSharp.Server { var state = service.State; if (state == WebSocketState.OPEN) - service.Stop (((ushort) CloseStatusCode.ABNORMAL).ToByteArray (ByteOrder.BIG)); + service.Context.WebSocket.Close (CloseStatusCode.ABNORMAL); else if (state == WebSocketState.CLOSING) continue; else