/// <summary> /// Stops receiving the WebSocket connection requests with the specified /// <see cref="CloseStatusCode"/> and <see cref="string"/>. /// </summary> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// indicating the reason for the stop. /// </param> /// <param name="reason"> /// A <see cref="string"/> that represents the reason for the stop. /// </param> public void Stop(CloseStatusCode code, string reason) { lock (_sync) { var msg = _state.CheckIfStart() ?? code.CheckIfValidCloseParameters(reason); if (msg != null) { _logger.Error(msg); return; } _state = ServerState.ShuttingDown; } stopReceiving(5000); if (code.IsNoStatusCode()) { _services.Stop(new CloseEventArgs(), true, true); } else { var send = !code.IsReserved(); _services.Stop(new CloseEventArgs(code, reason), send, send); } _state = ServerState.Stop; }
public void Stop(CloseStatusCode code, string reason) { lock (_sync) { string text = _state.CheckIfStart() ?? code.CheckIfValidCloseParameters(reason); if (text != null) { _logger.Error(text); return; } _state = ServerState.ShuttingDown; } if (code.IsNoStatusCode()) { _services.Stop(new CloseEventArgs(), send: true, wait: true); } else { bool flag = !code.IsReserved(); _services.Stop(new CloseEventArgs(code, reason), flag, flag); } stopReceiving(5000); _state = ServerState.Stop; }
/// <summary> /// Closes the WebSocket connection asynchronously with the specified /// <see cref="CloseStatusCode"/> and <see cref="string"/>, and releases /// all associated resources. /// </summary> /// <remarks> /// <para> /// This method doesn't wait for the close to be complete. /// </para> /// <para> /// This method emits a <see cref="OnError"/> event if the size of <paramref name="reason"/> /// is greater than 123 bytes. /// </para> /// </remarks> /// <param name="code"> /// One of the <see cref="CloseStatusCode"/> enum values, represents the status code /// indicating the reason for the close. /// </param> /// <param name="reason"> /// A <see cref="string"/> that represents the reason for the close. /// </param> public void CloseAsync (CloseStatusCode code, string reason) { var msg = _readyState.CheckIfClosable () ?? code.CheckIfValidCloseParameters (reason); if (msg != null) { _logger.Error (msg); error ("An error has occurred in closing the connection.", null); return; } if (code.IsNoStatusCode ()) { closeAsync (new CloseEventArgs (), true, true); return; } var send = !code.IsReserved (); closeAsync (new CloseEventArgs (code, reason), send, send); }
internal static string CheckIfValidCloseParameters(this CloseStatusCode code, string reason) { return((code.IsNoStatusCode() && !reason.IsNullOrEmpty()) ? "NoStatusCode cannot have a reason." : ((reason.IsNullOrEmpty() || Encoding.UTF8.GetBytes(reason).Length <= 123) ? null : "A reason has greater than the allowable max size.")); }