Class WebSocket
Inheritance
Implements
Inherited Members
Namespace: Mars.Common.Socket
Assembly: Mars.Common.dll
Syntax
public class WebSocket : IDisposable
Remarks
Constructors
| Improve this Doc View SourceWebSocket(String, String[])
Declaration
public WebSocket(string url, params string[] protocols)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | url | A System.String that specifies the URL of the WebSocket server to connect. |
| System.String[] | protocols |
An array of System.String that specifies the names of the subprotocols if necessary. Each value of the array must be a token defined in RFC 2616 . |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
url is null.
|
| System.ArgumentException |
-or-
-or-
-or-
|
Fields
| Improve this Doc View SourceFragmentLength
Declaration
public static int FragmentLength
Field Value
| Type | Description |
|---|---|
| System.Int32 |
Remarks
The data will be fragmented if that length is greater than the value of this field.
If you would like to change the value, you must set it to a value between 125 and
Int32.MaxValue - 14 inclusive.
Properties
| Improve this Doc View SourceCompression
Declaration
public CompressionMethod Compression { get; set; }
Property Value
| Type | Description |
|---|---|
| CompressionMethod |
One of the CompressionMethod enum values. It represents the compression method used to compress a message. The default value is None. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The set operation cannot be used by servers. |
EmitOnPing
Declaration
public bool EmitOnPing { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
true if the WebSocket emits a OnMessage event
when receives a ping; otherwise, false. The default value is false.
|
Remarks
EnableRedirection
Declaration
public bool EnableRedirection { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
The default value is |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The set operation cannot be used by servers. |
Extensions
Declaration
public string Extensions { get; }
Property Value
| Type | Description |
|---|---|
| System.String | A System.String that represents the extensions if any. The default value is System.String.Empty. |
Remarks
IsAlive
Declaration
public bool IsAlive { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
true if the connection is alive; otherwise, false.
|
Remarks
Origin
Declaration
public string Origin { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
A System.String that represents the value of the Origin header to send. The syntax is <scheme>://<host>[:<port>]. The default value is null. |
Remarks
The HTTP Origin header is defined in Section 7 of RFC 6454 .
This instance sends the Origin header if this property has any.
The set operation does nothing if the connection has already been established or it is closing.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The set operation is not available if this instance is not a client. |
| System.ArgumentException |
The value specified for a set operation is not an absolute URI string. -or- The value specified for a set operation includes the path segments. |
Protocol
Declaration
public string Protocol { get; }
Property Value
| Type | Description |
|---|---|
| System.String | A System.String that represents the subprotocol if any. The default value is System.String.Empty. |
Remarks
ReadyState
Declaration
public WebSocketState ReadyState { get; }
Property Value
| Type | Description |
|---|---|
| WebSocketState | One of the WebSocketState enum values that indicates the current state of the connection. The default value is Connecting. |
Remarks
Url
Declaration
public Uri Url { get; }
Property Value
| Type | Description |
|---|---|
| System.Uri | A System.Uri that represents the URL used to connect, or accepted. |
Remarks
WaitTime
Declaration
public TimeSpan WaitTime { get; set; }
Property Value
| Type | Description |
|---|---|
| System.TimeSpan |
A System.TimeSpan to wait for the response. The default value is the same as 5 seconds if the instance is a client. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | The value specified for a set operation is zero or less. |
Methods
| Improve this Doc View SourceAccept()
Declaration
public void Accept()
Remarks
AcceptAsync()
Declaration
public void AcceptAsync()
Remarks
This method does not wait for the accept to be complete.
This method is not available in a client.
Close()
Declaration
public void Close()
Remarks
Close(CloseStatusCode, String)
code and
reason.
Declaration
public void Close(CloseStatusCode code, string reason)
Parameters
| Type | Name | Description |
|---|---|---|
| CloseStatusCode | code |
One of the CloseStatusCode enum values. It represents the status code indicating the reason for the close. |
| System.String | reason |
A System.String that represents the reason for the close. The size must be 123 bytes or less in UTF-8. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
-or-
-or-
-or-
|
| System.ArgumentOutOfRangeException |
The size of reason is greater than 123 bytes.
|
Close(CloseStatusCode)
code.
Declaration
public void Close(CloseStatusCode code)
Parameters
| Type | Name | Description |
|---|---|---|
| CloseStatusCode | code |
One of the CloseStatusCode enum values. It represents the status code indicating the reason for the close. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
-or-
|
Close(UInt16, String)
code and
reason.
Declaration
public void Close(ushort code, string reason)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt16 | code |
A System.UInt16 that represents the status code indicating the reason for the close. The status codes are defined in Section 7.4 of RFC 6455. |
| System.String | reason |
A System.String that represents the reason for the close. The size must be 123 bytes or less in UTF-8. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
-or-
The size of |
| System.ArgumentException |
-or-
-or-
-or-
|
Close(UInt16)
code.
Declaration
public void Close(ushort code)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt16 | code |
A System.UInt16 that represents the status code indicating the reason for the close. The status codes are defined in Section 7.4 of RFC 6455. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
code is less than 1000 or greater than 4999.
|
| System.ArgumentException |
-or-
|
CloseAsync()
Declaration
public void CloseAsync()
Remarks
This method does not wait for the close to be complete.
And this method does nothing if the current state of the connection is Closing or Closed.
CloseAsync(CloseStatusCode, String)
code and reason.
Declaration
public void CloseAsync(CloseStatusCode code, string reason)
Parameters
| Type | Name | Description |
|---|---|---|
| CloseStatusCode | code |
One of the CloseStatusCode enum values. It represents the status code indicating the reason for the close. |
| System.String | reason |
A System.String that represents the reason for the close. The size must be 123 bytes or less in UTF-8. |
Remarks
This method does not wait for the close to be complete.
And this method does nothing if the current state of the connection is Closing or Closed.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
-or-
-or-
-or-
|
| System.ArgumentOutOfRangeException |
The size of reason is greater than 123 bytes.
|
CloseAsync(CloseStatusCode)
code.
Declaration
public void CloseAsync(CloseStatusCode code)
Parameters
| Type | Name | Description |
|---|---|---|
| CloseStatusCode | code |
One of the CloseStatusCode enum values. It represents the status code indicating the reason for the close. |
Remarks
This method does not wait for the close to be complete.
And this method does nothing if the current state of the connection is Closing or Closed.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
-or-
|
CloseAsync(UInt16, String)
code and reason.
Declaration
public void CloseAsync(ushort code, string reason)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt16 | code |
A System.UInt16 that represents the status code indicating the reason for the close. The status codes are defined in Section 7.4 of RFC 6455. |
| System.String | reason |
A System.String that represents the reason for the close. The size must be 123 bytes or less in UTF-8. |
Remarks
This method does not wait for the close to be complete.
And this method does nothing if the current state of the connection is Closing or Closed.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
-or-
The size of |
| System.ArgumentException |
-or-
-or-
-or-
|
CloseAsync(UInt16)
code.
Declaration
public void CloseAsync(ushort code)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt16 | code |
A System.UInt16 that represents the status code indicating the reason for the close. The status codes are defined in Section 7.4 of RFC 6455. |
Remarks
This method does not wait for the close to be complete.
And this method does nothing if the current state of the connection is Closing or Closed.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
code is less than 1000 or greater than 4999.
|
| System.ArgumentException |
-or-
|
Connect()
Declaration
public void Connect()
Remarks
ConnectAsync()
Declaration
public void ConnectAsync()
Remarks
This method does not wait for the connect to be complete.
This method is not available in a server.
Ping()
Declaration
public bool Ping()
Returns
| Type | Description |
|---|---|
| System.Boolean |
true if the send has done with no error and a pong has been
received within a time; otherwise, false.
|
Remarks
Ping(String)
message using the WebSocket
connection.
Declaration
public bool Ping(string message)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | message |
A System.String that represents the message to send. The size must be 125 bytes or less in UTF-8. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
true if the send has done with no error and a pong has been
received within a time; otherwise, false.
|
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
message could not be UTF-8-encoded.
|
| System.ArgumentOutOfRangeException |
The size of message is greater than 125 bytes.
|
Send(Byte[])
data using the WebSocket connection.
Declaration
public void Send(byte[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | data | An array of System.Byte that represents the binary data to send. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
data is null.
|
Send(FileInfo)
Declaration
public void Send(FileInfo fileInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.FileInfo | fileInfo | A System.IO.FileInfo that specifies the file to send. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
fileInfo is null.
|
| System.ArgumentException |
The file does not exist. -or- The file could not be opened. |
Send(Stream, Int32)
stream using the WebSocket
connection.
Declaration
public void Send(Stream stream, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | stream | A System.IO.Stream instance from which to read the data to send. |
| System.Int32 | length | An System.Int32 that specifies the number of bytes to send. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
stream is null.
|
| System.ArgumentException |
-or-
-or-
No data could be read from |
Send(String)
data using the WebSocket connection.
Declaration
public void Send(string data)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | data | A System.String that represents the text data to send. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
data is null.
|
| System.ArgumentException |
data could not be UTF-8-encoded.
|
SendAsync(Byte[], Action<Boolean>)
data asynchronously using the WebSocket
connection.
Declaration
public void SendAsync(byte[] data, Action<bool> completed)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | data | An array of System.Byte that represents the binary data to send. |
| System.Action<System.Boolean> | completed |
An The delegate invokes the method called when the send is complete.
|
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
data is null.
|
SendAsync(FileInfo, Action<Boolean>)
Declaration
public void SendAsync(FileInfo fileInfo, Action<bool> completed)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.FileInfo | fileInfo | A System.IO.FileInfo that specifies the file to send. |
| System.Action<System.Boolean> | completed |
An The delegate invokes the method called when the send is complete.
|
Remarks
The file is sent as the binary data.
This method does not wait for the send to be complete.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
fileInfo is null.
|
| System.ArgumentException |
The file does not exist. -or- The file could not be opened. |
SendAsync(Stream, Int32, Action<Boolean>)
stream asynchronously using
the WebSocket connection.
Declaration
public void SendAsync(Stream stream, int length, Action<bool> completed)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | stream | A System.IO.Stream instance from which to read the data to send. |
| System.Int32 | length | An System.Int32 that specifies the number of bytes to send. |
| System.Action<System.Boolean> | completed |
An The delegate invokes the method called when the send is complete.
|
Remarks
The data is sent as the binary data.
This method does not wait for the send to be complete.
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
stream is null.
|
| System.ArgumentException |
-or-
-or-
No data could be read from |
SendAsync(String, Action<Boolean>)
data asynchronously using the WebSocket
connection.
Declaration
public void SendAsync(string data, Action<bool> completed)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | data | A System.String that represents the text data to send. |
| System.Action<System.Boolean> | completed |
An The delegate invokes the method called when the send is complete.
|
Remarks
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | The current state of the connection is not Open. |
| System.ArgumentNullException |
data is null.
|
| System.ArgumentException |
data could not be UTF-8-encoded.
|
Events
| Improve this Doc View SourceOnClose
Declaration
public event EventHandler<CloseEventArgs> OnClose
Event Type
| Type | Description |
|---|---|
| System.EventHandler<CloseEventArgs> |
Remarks
OnError
Declaration
public event EventHandler<ErrorEventArgs> OnError
Event Type
| Type | Description |
|---|---|
| System.EventHandler<ErrorEventArgs> |
Remarks
OnMessage
Declaration
public event EventHandler<MessageEventArgs> OnMessage
Event Type
| Type | Description |
|---|---|
| System.EventHandler<MessageEventArgs> |
Remarks
OnOpen
Declaration
public event EventHandler OnOpen
Event Type
| Type | Description |
|---|---|
| System.EventHandler |
Remarks
Explicit Interface Implementations
| Improve this Doc View SourceIDisposable.Dispose()
Declaration
void IDisposable.Dispose()
Remarks
This method closes the connection with close status 1001 (going away).
And this method does nothing if the current state of the connection is Closing or Closed.