websocket-sharp : WebSocketSharp Namespace

# WebSocket Class

Implements the WebSocket interface.

## Syntax

public class WebSocket : IDisposable

## Remarks

The WebSocket class provides a set of methods and properties for two-way communication using the WebSocket protocol (RFC 6455).

## Requirements

Namespace: WebSocketSharp
Assembly: websocket-sharp (in websocket-sharp.dll)

## Members

See Also: Inherited members from object.

## Public Constructors

| WebSocket (string, params string[]) | Initializes a new instance of the WebSocketSharp.WebSocket class with the specified WebSocket URL and subprotocols.
| WebSocket (string, EventHandler, EventHandler<MessageEventArgs>, EventHandler<ErrorEventArgs>, EventHandler<CloseEventArgs>, params string[]) | Initializes a new instance of the WebSocketSharp.WebSocket class with the specified WebSocket URL, OnOpen, OnMessage, OnError, OnClose event handlers and subprotocols.

## Public Properties

| Compression | CompressionMethod . Gets or sets the compression method used to compress the payload data of the WebSocket Data frame.
[read-only] | Cookies | IEnumerable<WebSocketSharp.Net.Cookie> . Gets the cookies used in the WebSocket opening handshake.
[read-only] | Extensions | string . Gets the WebSocket extensions selected by the server.
[read-only] | IsAlive | bool . Gets a value indicating whether the WebSocket connection is alive.
[read-only] | IsSecure | bool . Gets a value indicating whether the WebSocket connection is secure.
| Origin | string . Gets or sets the value of the Origin header used in the WebSocket opening handshake.
[read-only] | Protocol | string . Gets the WebSocket subprotocol selected by the server.
[read-only] | ReadyState | WsState . Gets the state of the WebSocket connection.
[read-only] | Url | Uri . Gets the WebSocket URL to connect.

## Public Methods

Close ()

> Closes the WebSocket connection and releases all associated resources.

Close (ushort)

> Closes the WebSocket connection with the specified code and releases all associated resources.

Close (CloseStatusCode)

> Closes the WebSocket connection with the specified code and releases all associated resources.

Close (ushort, string)

> Closes the WebSocket connection with the specified code and reason, and releases all associated resources.

Close (CloseStatusCode, string)

> Closes the WebSocket connection with the specified code and reason, and releases all associated resources.

Connect ()

> Establishes a WebSocket connection.

Dispose ()

> Closes the WebSocket connection and releases all associated resources.

Ping () : bool

> Pings using the WebSocket connection.

Ping (string) : bool

> Pings with the specified message using the WebSocket connection.

Send (byte[])

> Sends a binary data using the WebSocket connection.

Send (System.IO.FileInfo)

> Sends a binary data using the WebSocket connection.

Send (string)

> Sends a text data using the WebSocket connection.

SendAsync (byte[], Action)

> Sends a binary data asynchronously using the WebSocket connection.

SendAsync (System.IO.FileInfo, Action)

> Sends a binary data asynchronously using the WebSocket connection.

SendAsync (string, Action)

> Sends a text data asynchronously using the WebSocket connection.

SetCookie (WebSocketSharp.Net.Cookie)

> Sets a WebSocketSharp.Net.Cookie used in the WebSocket opening handshake.

## Public Events

| OnClose | Occurs when the WebSocketSharp.WebSocket receives a Close frame or the Close method is called.
| OnError | Occurs when the WebSocketSharp.WebSocket gets an error.
| OnMessage | Occurs when the WebSocketSharp.WebSocket receives a data frame.
| OnOpen | Occurs when the WebSocket connection has been established.

## Extension Methods

static

IsNull<T> (this T) : bool

> Determines whether the specified object is `null`.

static

IsNullDo<T> (this T, Action) : bool

> Determines whether the specified object is `null`. And invokes the specified Action delegate if the specified object is `null`.

## Member Details

### WebSocket Constructor

> Initializes a new instance of the WebSocketSharp.WebSocket class with the specified WebSocket URL and subprotocols.

> ## Syntax

> public WebSocket (string url, params string[] protocols)

> #### Parameters

> > url
> >   A string that contains a WebSocket URL to connect.
> > protocols
> >   An array of string that contains the WebSocket subprotocols if any.

> #### Exceptions

> > Type | Reason
> > --- | ---
> > ArgumentNullException | url is `null`.
> > ArgumentException | url is not valid WebSocket URL.

> ## Remarks

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### WebSocket Constructor

> Initializes a new instance of the WebSocketSharp.WebSocket class with the specified WebSocket URL, OnOpen, OnMessage, OnError, OnClose event handlers and subprotocols.

> ## Syntax

> public WebSocket (string url, EventHandler onOpen, EventHandler<MessageEventArgs> onMessage, EventHandler<ErrorEventArgs> onError, EventHandler<CloseEventArgs> onClose, params string[] protocols)

> #### Parameters

> > url
> >   A string that contains a WebSocket URL to connect.
> > onOpen
> >   An WebSocket.OnOpen event handler.
> > onMessage
> >   An WebSocket.OnMessage event handler.
> > onError
> >   An WebSocket.OnError event handler.
> > onClose
> >   An WebSocket.OnClose event handler.
> > protocols
> >   An array of string that contains the WebSocket subprotocols if any.

> #### Exceptions

> > Type | Reason
> > --- | ---
> > ArgumentNullException | url is `null`.
> > ArgumentException | url is not valid WebSocket URL.

> ## Remarks

> This constructor initializes a new instance of the WebSocketSharp.WebSocket class and establishes a WebSocket connection.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Close Method

> Closes the WebSocket connection and releases all associated resources.

> ## Syntax

> public void Close ()

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Close Method

> Closes the WebSocket connection with the specified code and releases all associated resources.

> ## Syntax

> public void Close (ushort code)

> #### Parameters

> > code
> >   A ushort that indicates the status code for closure.

> ## Remarks

> This Close method emits a WebSocket.OnError event if code is not in the allowable range of the WebSocket close status code.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Close Method

> Closes the WebSocket connection with the specified code and releases all associated resources.

> ## Syntax

> public void Close (CloseStatusCode code)

> #### Parameters

> > code
> >   One of the WebSocketSharp.CloseStatusCode values that indicates the status code for closure.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Close Method

> Closes the WebSocket connection with the specified code and reason, and releases all associated resources.

> ## Syntax

> public void Close (ushort code, string reason)

> #### Parameters

> > code
> >   A ushort that indicates the status code for closure.
> > reason
> >   A string that contains the reason for closure.

> ## Remarks

> This Close method emits a WebSocket.OnError event if code is not in the allowable range of the WebSocket close status code.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Close Method

> Closes the WebSocket connection with the specified code and reason, and releases all associated resources.

> ## Syntax

> public void Close (CloseStatusCode code, string reason)

> #### Parameters

> > code
> >   One of the WebSocketSharp.CloseStatusCode values that indicates the status code for closure.
> > reason
> >   A string that contains the reason for closure.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Compression Property

> Gets or sets the compression method used to compress the payload data of the WebSocket Data frame.

> ## Syntax

> public CompressionMethod Compression { get; set; }

> #### Value

> > One of the WebSocketSharp.CompressionMethod values that indicates the compression method to use. The default is CompressionMethod.NONE.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Connect Method

> Establishes a WebSocket connection.

> ## Syntax

> public void Connect ()

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Cookies Property

> Gets the cookies used in the WebSocket opening handshake.

> ## Syntax

> public IEnumerable<WebSocketSharp.Net.Cookie> Cookies { get; }

> #### Value

> > An IEnumerable<Cookie> interface that provides an enumerator which supports the iteration over the collection of cookies.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Dispose Method

> Closes the WebSocket connection and releases all associated resources.

> ## Syntax

> public void Dispose ()

> ## Remarks

> This method closes the WebSocket connection with the CloseStatusCode.AWAY.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Extensions Property

> Gets the WebSocket extensions selected by the server.

> ## Syntax

> public string Extensions { get; }

> #### Value

> > A string that contains the extensions if any. The default is string.Empty.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### IsAlive Property

> Gets a value indicating whether the WebSocket connection is alive.

> ## Syntax

> public bool IsAlive { get; }

> #### Value

> > `true` if the connection is alive; otherwise, `false`.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### IsSecure Property

> Gets a value indicating whether the WebSocket connection is secure.

> ## Syntax

> public bool IsSecure { get; }

> #### Value

> > `true` if the connection is secure; otherwise, `false`.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### OnClose Event

> Occurs when the WebSocketSharp.WebSocket receives a Close frame or the Close method is called.

> ## Syntax

> public event EventHandler<CloseEventArgs> OnClose

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### OnError Event

> Occurs when the WebSocketSharp.WebSocket gets an error.

> ## Syntax

> public event EventHandler<ErrorEventArgs> OnError

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### OnMessage Event

> Occurs when the WebSocketSharp.WebSocket receives a data frame.

> ## Syntax

> public event EventHandler<MessageEventArgs> OnMessage

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### OnOpen Event

> Occurs when the WebSocket connection has been established.

> ## Syntax

> public event EventHandler OnOpen

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Origin Property

> Gets or sets the value of the Origin header used in the WebSocket opening handshake.

> ## Syntax

> public string Origin { get; set; }

> #### Value

> > A string that contains the value of the HTTP Origin header to send. The default is string.Empty.

> > The value of the Origin header has the following syntax: `<scheme>://<host>[:<port>]`

> ## Remarks

> A WebSocketSharp.WebSocket instance does not send the Origin header in the WebSocket opening handshake if the value of this property is string.Empty.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Ping Method

> Pings using the WebSocket connection.

> ## Syntax

> public bool Ping ()

> #### Returns

> > `true` if the WebSocketSharp.WebSocket receives a Pong in a time; otherwise, `false`.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Ping Method

> Pings with the specified message using the WebSocket connection.

> ## Syntax

> public bool Ping (string message)

> #### Parameters

> > message
> >   A string that contains a message.

> #### Returns

> > `true` if the WebSocketSharp.WebSocket receives a Pong in a time; otherwise, `false`.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Protocol Property

> Gets the WebSocket subprotocol selected by the server.

> ## Syntax

> public string Protocol { get; }

> #### Value

> > A string that contains the subprotocol if any. The default is string.Empty.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### ReadyState Property

> Gets the state of the WebSocket connection.

> ## Syntax

> public WsState ReadyState { get; }

> #### Value

> > One of the WebSocketSharp.WsState values. The default is WsState.CONNECTING.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Send Method

> Sends a binary data using the WebSocket connection.

> ## Syntax

> public void Send (byte[] data)

> #### Parameters

> > data
> >   An array of byte that contains a binary data to send.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Send Method

> Sends a binary data using the WebSocket connection.

> ## Syntax

> public void Send (System.IO.FileInfo file)

> #### Parameters

> > file
> >   A System.IO.FileInfo that contains a binary data to send.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Send Method

> Sends a text data using the WebSocket connection.

> ## Syntax

> public void Send (string data)

> #### Parameters

> > data
> >   A string that contains a text data to send.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### SendAsync Method

> Sends a binary data asynchronously using the WebSocket connection.

> ## Syntax

> public void SendAsync (byte[] data, Action completed)

> #### Parameters

> > data
> >   An array of byte that contains a binary data to send.
> > completed
> >   An Action delegate that references the method(s) called when the asynchronous operation completes.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### SendAsync Method

> Sends a binary data asynchronously using the WebSocket connection.

> ## Syntax

> public void SendAsync (System.IO.FileInfo file, Action completed)

> #### Parameters

> > file
> >   A System.IO.FileInfo that contains a binary data to send.
> > completed
> >   An Action delegate that references the method(s) called when the asynchronous operation completes.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### SendAsync Method

> Sends a text data asynchronously using the WebSocket connection.

> ## Syntax

> public void SendAsync (string data, Action completed)

> #### Parameters

> > data
> >   A string that contains a text data to send.
> > completed
> >   An Action delegate that references the method(s) called when the asynchronous operation completes.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### SetCookie Method

> Sets a WebSocketSharp.Net.Cookie used in the WebSocket opening handshake.

> ## Syntax

> public void SetCookie (WebSocketSharp.Net.Cookie cookie)

> #### Parameters

> > cookie
> >   A WebSocketSharp.Net.Cookie that contains an HTTP Cookie to set.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)

### Url Property

> Gets the WebSocket URL to connect.

> ## Syntax

> public Uri Url { get; }

> #### Value

> > A Uri that contains the WebSocket URL to connect.

> ## Remarks

> Documentation for this section has not yet been entered.

> ## Requirements

> Namespace: WebSocketSharp
> Assembly: websocket-sharp (in websocket-sharp.dll)