public static WebSocket CreateFromStream(
Stream stream,
bool isServer,
string?subProtocol,
TimeSpan keepAliveInterval)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!stream.CanRead || !stream.CanWrite)
{
throw new ArgumentException(!stream.CanRead ? SR.NotReadableStream : SR.NotWriteableStream, nameof(stream));
}
if (subProtocol != null)
{
WebSocketValidate.ValidateSubprotocol(subProtocol);
}
if (keepAliveInterval != Timeout.InfiniteTimeSpan && keepAliveInterval < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(keepAliveInterval), keepAliveInterval,
SR.Format(SR.net_WebSockets_ArgumentOutOfRange_TooSmall,
0));
}
return(ManagedWebSocket.CreateFromConnectedStream(stream, isServer, subProtocol, keepAliveInterval));
}