Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Net.​WebSockets.​WebSocketStream
Assembly: System.Net.WebSockets
Inheritance: object → MarshalByRefObject → IO.​Stream → WebSocketStream
Implemented Interfaces
- IAsyncDisposable Implemented by: Stream
- IDisposable Implemented by: Stream
Provides a <see cref="T:System.IO.Stream" /> that delegates to a wrapped <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
Properties
public
bool
CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports reading.
public
bool
CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
public
bool
CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writing.
public
long
Length
When overridden in a derived class, gets the length in bytes of the stream.
public
long
Position
When overridden in a derived class, gets or sets the position within the current stream.
public
WebSocket
WebSocket
Gets the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> wrapped by this <see cref="T:System.Net.WebSockets.WebSocketStream" /> .
public
bool
CanTimeout
public
int
ReadTimeout
public
int
WriteTimeout
Methods
public
IAsyncResult
BeginWrite​(byte[] buffer,
int offset,
int count,
AsyncCallback? callback,
object state)
Begins an asynchronous write operation. (Consider using <xref data-throw-if-not-resolved="true" uid="System.IO.Stream.WriteAsync(System.Byte[],System.Int32,System.Int32)"></xref> instead.)
Returns An <code data-dev-comment-type="langword">IAsyncResult</code> that represents the asynchronous write, which could still be pending.
buffer
The buffer to write data from.
offset
The byte offset in <code data-dev-comment-type="paramref">buffer</code> from which to begin writing.
count
The maximum number of bytes to write.
callback
An optional asynchronous callback, to be called when the write is complete.
state
A user-provided object that distinguishes this particular asynchronous write request from other requests.
public
IAsyncResult
BeginRead​(byte[] buffer,
int offset,
int count,
AsyncCallback? callback,
object state)
Begins an asynchronous read operation. (Consider using <xref data-throw-if-not-resolved="true" uid="System.IO.Stream.ReadAsync(System.Byte[],System.Int32,System.Int32)"></xref> instead.)
Returns An <xref data-throw-if-not-resolved="true" uid="System.IAsyncResult"></xref> that represents the asynchronous read, which could still be pending.
buffer
The buffer to read the data into.
offset
The byte offset in <code data-dev-comment-type="paramref">buffer</code> at which to begin writing data read from the stream.
count
The maximum number of bytes to read.
callback
An optional asynchronous callback, to be called when the read is complete.
state
A user-provided object that distinguishes this particular asynchronous read request from other requests.
public static
WebSocketStream
Create​(WebSocket webSocket,
WebSocketMessageType writeMessageType,
bool ownsWebSocket = false)
Creates a <see cref="T:System.Net.WebSockets.WebSocketStream" /> that delegates to a wrapped <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
Returns A new instance of <see cref="T:System.Net.WebSockets.WebSocketStream" /> that forwards reads and writes on the <see cref="T:System.IO.Stream" /> to the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
webSocket
The wrapped <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
writeMessageType
The type of messages that should be written as part of <see cref="Overload:System.IO.Stream.WriteAsync" /> calls. Each write produces a message.
ownsWebSocket
<see langword="true" /> if disposing the <see cref="T:System.IO.Stream" /> should close the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> ; otherwise, <see langword="false" /> . Defaults to <see langword="false" /> .
public static
WebSocketStream
Create​(WebSocket webSocket,
WebSocketMessageType writeMessageType,
TimeSpan closeTimeout)
Creates a <see cref="T:System.Net.WebSockets.WebSocketStream" /> that delegates to a wrapped <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
Returns A new instance of <see cref="T:System.Net.WebSockets.WebSocketStream" /> that forwards reads and writes on the <see cref="T:System.IO.Stream" /> to the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
webSocket
The wrapped <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
writeMessageType
The type of messages that should be written as part of <see cref="Overload:System.IO.Stream.WriteAsync" /> calls. Each write produces a message.
closeTimeout
The amount of time that disposing the <see cref="T:System.Net.WebSockets.WebSocketStream" /> will wait for a graceful closing of the <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> 's output.
public static
WebSocketStream
CreateWritableMessageStream​(WebSocket webSocket,
WebSocketMessageType writeMessageType)
Creates a <see cref="T:System.Net.WebSockets.WebSocketStream" /> that writes a single message to the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
Returns A new instance of <see cref="T:System.Net.WebSockets.WebSocketStream" /> that forwards writes on the <see cref="T:System.IO.Stream" /> to the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
webSocket
The wrapped <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
writeMessageType
<para>The type of messages that should be written as part of <see cref="Overload:System.IO.Stream.WriteAsync" /> calls.</para> <para>Each write on the <see cref="T:System.IO.Stream" /> results in writing a partial message to the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" />.</para> <para>When the <see cref="T:System.IO.Stream" /> is disposed, it will write an empty message to the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> to signal the end of the message.</para>
public static
WebSocketStream
CreateReadableMessageStream​(WebSocket webSocket)
Creates a <see cref="T:System.Net.WebSockets.WebSocketStream" /> that reads a single message from the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
Returns A new instance of <see cref="T:System.Net.WebSockets.WebSocketStream" /> that forwards reads on the <see cref="T:System.IO.Stream" /> to the underlying <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
webSocket
The wrapped <see cref="P:System.Net.WebSockets.WebSocketStream.WebSocket" /> .
protected
void
Dispose​(bool disposing)
Releases the unmanaged resources used by the <xref data-throw-if-not-resolved="true" uid="System.IO.Stream"></xref> and optionally releases the managed resources.
disposing
<code data-dev-comment-type="langword">true</code> to release both managed and unmanaged resources; <code data-dev-comment-type="langword">false</code> to release only unmanaged resources.
public
int
EndRead​(IAsyncResult asyncResult)
Waits for the pending asynchronous read to complete. (Consider using <xref data-throw-if-not-resolved="true" uid="System.IO.Stream.ReadAsync(System.Byte[],System.Int32,System.Int32)"></xref> instead.)
Returns The number of bytes read from the stream, between zero (0) and the number of bytes requested. ReadAsync returns zero (0) only if zero bytes were requested or if no more bytes will be available because it's at the end of the stream; otherwise, read operations do not complete until at least one byte is available. If zero bytes are requested, read operations may complete immediately or may not complete until at least one byte is available (but without consuming any data).
asyncResult
The reference to the pending asynchronous request to finish.
public
void
EndWrite​(IAsyncResult asyncResult)
Ends an asynchronous write operation. (Consider using <xref data-throw-if-not-resolved="true" uid="System.IO.Stream.WriteAsync(System.Byte[],System.Int32,System.Int32)"></xref> instead.)
asyncResult
A reference to the outstanding asynchronous I/O request.
public
void
Flush​()
When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
FlushAsync​(Threading.​CancellationToken cancellationToken)
Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.
Returns A task that represents the asynchronous flush operation.
cancellationToken
The token to monitor for cancellation requests. The default value is <xref data-throw-if-not-resolved="true" uid="System.Threading.CancellationToken.None"></xref> .
public
int
Read​(byte[] buffer,
int offset,
int count)
When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
Returns The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if <code data-dev-comment-type="paramref">count</code> is 0 or the end of the stream has been reached.
buffer
An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <code data-dev-comment-type="paramref">offset</code> and ( <code data-dev-comment-type="paramref">offset</code> + <code data-dev-comment-type="paramref">count</code> - 1) replaced by the bytes read from the current source.
offset
The zero-based byte offset in <code data-dev-comment-type="paramref">buffer</code> at which to begin storing the data read from the current stream.
count
The maximum number of bytes to be read from the current stream.
public
Threading.​Tasks.​Task<​int>
ReadAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
Returns A task that represents the asynchronous read operation. The value of the <code data-dev-comment-type="paramref">TResult</code> parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if <code data-dev-comment-type="paramref">count</code> is 0 or if the end of the stream has been reached.
buffer
The buffer to write the data into.
offset
The byte offset in <code data-dev-comment-type="paramref">buffer</code> at which to begin writing data from the stream.
count
The maximum number of bytes to read.
cancellationToken
The token to monitor for cancellation requests. The default value is <xref data-throw-if-not-resolved="true" uid="System.Threading.CancellationToken.None"></xref> .
public
Threading.​Tasks.​ValueTask<​int>
ReadAsync​(Memory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
Returns A task that represents the asynchronous read operation. The value of its <xref data-throw-if-not-resolved="true" uid="System.Threading.Tasks.ValueTask`1.Result"></xref> property contains the total number of bytes read into the buffer. The result value can be less than the length of the buffer if that many bytes are not currently available, or it can be 0 (zero) if the length of the buffer is 0 or if the end of the stream has been reached.
buffer
The region of memory to write the data into.
cancellationToken
The token to monitor for cancellation requests. The default value is <xref data-throw-if-not-resolved="true" uid="System.Threading.CancellationToken.None"></xref> .
public
long
Seek​(long offset,
IO.​SeekOrigin origin)
When overridden in a derived class, sets the position within the current stream.
Returns The new position within the current stream.
offset
A byte offset relative to the <code data-dev-comment-type="paramref">origin</code> parameter.
origin
A value of type <xref data-throw-if-not-resolved="true" uid="System.IO.SeekOrigin"></xref> indicating the reference point used to obtain the new position.
public
void
SetLength​(long value)
When overridden in a derived class, sets the length of the current stream.
value
The desired length of the current stream in bytes.
public
void
Write​(byte[] buffer,
int offset,
int count)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
buffer
An array of bytes. This method copies <code data-dev-comment-type="paramref">count</code> bytes from <code data-dev-comment-type="paramref">buffer</code> to the current stream.
offset
The zero-based byte offset in <code data-dev-comment-type="paramref">buffer</code> at which to begin copying bytes to the current stream.
count
The number of bytes to be written to the current stream.
WriteAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
Returns A task that represents the asynchronous write operation.
buffer
The buffer to write data from.
offset
The zero-based byte offset in <code data-dev-comment-type="paramref">buffer</code> from which to begin copying bytes to the stream.
count
The maximum number of bytes to write.
cancellationToken
The token to monitor for cancellation requests. The default value is <xref data-throw-if-not-resolved="true" uid="System.Threading.CancellationToken.None"></xref> .
WriteAsync​(ReadOnlyMemory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
Returns A task that represents the asynchronous write operation.
buffer
The region of memory to write data from.
cancellationToken
The token to monitor for cancellation requests. The default value is <xref data-throw-if-not-resolved="true" uid="System.Threading.CancellationToken.None"></xref> .
public
void
CopyTo​(IO.​Stream destination)
Inherited from IO.​Stream
public
void
CopyTo​(IO.​Stream destination,
int bufferSize)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination,
int bufferSize)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination,
int bufferSize,
Threading.​CancellationToken cancellationToken)
Inherited from IO.​Stream
CopyToAsync​(IO.​Stream destination,
Threading.​CancellationToken cancellationToken)
Inherited from IO.​Stream
protected
Threading.​WaitHandle
CreateWaitHandle​()
Inherited from IO.​Stream
public
int
Read​(Span<​byte> buffer)
Inherited from IO.​Stream
public
Threading.​Tasks.​Task<​int>
ReadAsync​(byte[] buffer,
int offset,
int count)
Inherited from IO.​Stream
public
int
ReadAtLeast​(Span<​byte> buffer,
int minimumBytes,
bool throwOnEndOfStream = true)
Inherited from IO.​Stream
public
Threading.​Tasks.​ValueTask<​int>
ReadAtLeastAsync​(Memory<​byte> buffer,
int minimumBytes,
bool throwOnEndOfStream = true,
Threading.​CancellationToken cancellationToken = null)
Inherited from IO.​Stream
public
void
ReadExactly​(byte[] buffer,
int offset,
int count)
Inherited from IO.​Stream
public
void
ReadExactly​(Span<​byte> buffer)
Inherited from IO.​Stream
ReadExactlyAsync​(byte[] buffer,
int offset,
int count,
Threading.​CancellationToken cancellationToken = null)
Inherited from IO.​Stream
ReadExactlyAsync​(Memory<​byte> buffer,
Threading.​CancellationToken cancellationToken = null)
Inherited from IO.​Stream
public
void
Write​(ReadOnlySpan<​byte> buffer)
Inherited from IO.​Stream
WriteAsync​(byte[] buffer,
int offset,
int count)
Inherited from IO.​Stream
protected
MarshalByRefObject
MemberwiseClone​(bool cloneIdentity)
Inherited from MarshalByRefObject
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object