diff --git a/websocket-sharp/Server/WebSocketBehavior.cs b/websocket-sharp/Server/WebSocketBehavior.cs index 079d4da7..5ae6c8f6 100644 --- a/websocket-sharp/Server/WebSocketBehavior.cs +++ b/websocket-sharp/Server/WebSocketBehavior.cs @@ -366,7 +366,7 @@ namespace WebSocketSharp.Server _websocket.OnError += onError; _websocket.OnClose += onClose; - _websocket.Accept (); + _websocket.InternalAccept (); } #endregion diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 551d4749..1538e2db 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -655,6 +655,13 @@ namespace WebSocketSharp : null; } + private string checkIfCanAccept () + { + return _client + ? "This operation isn't available as a client." + : _readyState.CheckIfCanAccept (); + } + private string checkIfCanConnect () { return !_client && _readyState == WebSocketState.Closed @@ -1562,20 +1569,6 @@ namespace WebSocketSharp #region Internal Methods - // As server - internal void Accept () - { - try { - if (acceptHandshake ()) { - _readyState = WebSocketState.Open; - open (); - } - } - catch (Exception ex) { - processException (ex, "An exception has occurred while accepting."); - } - } - internal static string CheckCloseParameters (ushort code, string reason, bool client) { return !code.IsCloseStatusCode () @@ -1669,6 +1662,20 @@ namespace WebSocketSharp return Convert.ToBase64String (src); } + // As server + internal void InternalAccept () + { + try { + if (acceptHandshake ()) { + _readyState = WebSocketState.Open; + open (); + } + } + catch (Exception ex) { + processException (ex, "An exception has occurred while accepting."); + } + } + internal bool Ping (byte[] frameAsBytes, TimeSpan timeout) { try { @@ -1743,6 +1750,56 @@ namespace WebSocketSharp #region Public Methods + /// + /// Accepts the WebSocket connection request. + /// + /// + /// This method isn't available as a client. + /// + public void Accept () + { + var msg = checkIfCanAccept (); + if (msg != null) { + _logger.Error (msg); + error ("An error has occurred in accepting.", null); + + return; + } + + if (accept ()) + open (); + } + + /// + /// Accepts the WebSocket connection request asynchronously. + /// + /// + /// + /// This method doesn't wait for the accept to be complete. + /// + /// + /// This method isn't available as a client. + /// + /// + public void AcceptAsync () + { + var msg = checkIfCanAccept (); + if (msg != null) { + _logger.Error (msg); + error ("An error has occurred in accepting.", null); + + return; + } + + Func acceptor = accept; + acceptor.BeginInvoke ( + ar => { + if (acceptor.EndInvoke (ar)) + open (); + }, + null); + } + /// /// Closes the WebSocket connection, and releases all associated resources. ///