My Project
Loading...
Searching...
No Matches
Public Member Functions | Properties | Events | List of all members
WebSocketSharp.WebSocket Class Reference

Implements the WebSocket interface. More...

Inheritance diagram for WebSocketSharp.WebSocket:

Public Member Functions

 WebSocket (string url, params string[] protocols)
 Initializes a new instance of the WebSocket class with the specified URL and optionally subprotocols.
 
void Close ()
 Closes the connection.
 
void Close (ushort code)
 Closes the connection with the specified code.
 
void Close (CloseStatusCode code)
 Closes the connection with the specified code.
 
void Close (ushort code, string reason)
 Closes the connection with the specified code and reason.
 
void Close (CloseStatusCode code, string reason)
 Closes the connection with the specified code and reason.
 
void CloseAsync ()
 Closes the connection asynchronously.
 
void CloseAsync (ushort code)
 Closes the connection asynchronously with the specified code.
 
void CloseAsync (CloseStatusCode code)
 Closes the connection asynchronously with the specified code.
 
void CloseAsync (ushort code, string reason)
 Closes the connection asynchronously with the specified code and reason.
 
void CloseAsync (CloseStatusCode code, string reason)
 Closes the connection asynchronously with the specified code and reason.
 
void Connect ()
 Establishes a connection.
 
void ConnectAsync ()
 Establishes a connection asynchronously.
 
bool Ping ()
 Sends a ping to the remote endpoint.
 
bool Ping (string message)
 Sends a ping with the specified message to the remote endpoint.
 
void Send (byte[] data)
 Sends the specified data to the remote endpoint.
 
void Send (FileInfo fileInfo)
 Sends the specified file to the remote endpoint.
 
void Send (string data)
 Sends the specified data to the remote endpoint.
 
void Send (Stream stream, int length)
 Sends the data from the specified stream instance to the remote endpoint.
 
void SendAsync (byte[] data, Action< bool > completed)
 Sends the specified data to the remote endpoint asynchronously.
 
void SendAsync (FileInfo fileInfo, Action< bool > completed)
 Sends the specified file to the remote endpoint asynchronously.
 
void SendAsync (string data, Action< bool > completed)
 Sends the specified data to the remote endpoint asynchronously.
 
void SendAsync (Stream stream, int length, Action< bool > completed)
 Sends the data from the specified stream instance to the remote endpoint asynchronously.
 
void SetCookie (Cookie cookie)
 Sets an HTTP cookie to send with the handshake request.
 
void SetCredentials (string username, string password, bool preAuth)
 Sets the credentials for the HTTP authentication (Basic/Digest).
 
void SetProxy (string url, string username, string password)
 Sets the URL of the HTTP proxy server through which to connect and the credentials for the HTTP proxy authentication (Basic/Digest).
 

Properties

CompressionMethod Compression [get, set]
 Gets or sets the compression method used to compress a message.
 
IEnumerable< CookieCookies [get]
 Gets the HTTP cookies included in the handshake request/response.
 
NetworkCredential Credentials [get]
 Gets the credentials for the HTTP authentication (Basic/Digest).
 
bool EmitOnPing [get, set]
 Gets or sets a value indicating whether the message event is emitted when the interface receives a ping.
 
bool EnableRedirection [get, set]
 Gets or sets a value indicating whether the URL redirection for the handshake request is allowed.
 
string Extensions [get]
 Gets the extensions selected by the server.
 
bool IsAlive [get]
 Gets a value indicating whether the communication is possible.
 
bool IsSecure [get]
 Gets a value indicating whether the connection is secure.
 
Logger Log [get, set]
 Gets the logging function.
 
string Origin [get, set]
 Gets or sets the value of the HTTP Origin header to send with the handshake request.
 
string Protocol [get, set]
 Gets the name of subprotocol selected by the server.
 
WebSocketState ReadyState [get]
 Gets the current state of the interface.
 
ClientSslConfiguration SslConfiguration [get]
 Gets the configuration for secure connection.
 
Uri Url [get]
 Gets the URL to which to connect.
 
TimeSpan WaitTime [get, set]
 Gets or sets the time to wait for the response to the ping or close.
 

Events

EventHandler< CloseEventArgsOnClose
 Occurs when the connection has been closed.
 
EventHandler< ErrorEventArgsOnError
 Occurs when the interface gets an error.
 
EventHandler< MessageEventArgsOnMessage
 Occurs when the interface receives a message.
 
EventHandler OnOpen
 Occurs when the connection has been established.
 

Detailed Description

Implements the WebSocket interface.

This class provides a set of methods and properties for two-way communication using the WebSocket protocol.

The WebSocket protocol is defined in RFC 6455

.

Constructor & Destructor Documentation

◆ WebSocket()

WebSocketSharp.WebSocket.WebSocket ( string  url,
params string[]  protocols 
)
inline

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

Parameters
urlA string that specifies the URL to which to connect. The scheme of the URL must be ws or wss. The new instance uses a secure connection if the scheme is wss.
protocolsAn array of string that specifies the names of the subprotocols if necessary. Each value of the array must be a token defined in RFC 2616.
Exceptions
ArgumentNullExceptionurl is null.
ArgumentExceptionurl is an empty string. -or- url is an invalid WebSocket URL string. -or- protocols contains a value that is not a token. -or- protocols contains a value twice.

Member Function Documentation

◆ Close() [1/5]

void WebSocketSharp.WebSocket.Close ( )
inline

Closes the connection.

This method does nothing if the current state of the interface is Closing or Closed.

◆ Close() [2/5]

void WebSocketSharp.WebSocket.Close ( CloseStatusCode  code)
inline

Closes the connection with the specified code.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeOne of the CloseStatusCode enum values. It specifies the status code indicating the reason for the close.
Exceptions
ArgumentExceptioncode is CloseStatusCode.ServerError. It cannot be used by a client. -or- code is CloseStatusCode.MandatoryExtension. It cannot be used by a server.

◆ Close() [3/5]

void WebSocketSharp.WebSocket.Close ( CloseStatusCode  code,
string  reason 
)
inline

Closes the connection with the specified code and reason.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeOne of the CloseStatusCode enum values. It specifies the status code indicating the reason for the close.
reasonA string that specifies the reason for the close. Its size must be 123 bytes or less in UTF-8.
Exceptions
ArgumentExceptioncode is CloseStatusCode.ServerError. It cannot be used by a client. -or- code is CloseStatusCode.MandatoryExtension. It cannot be used by a server. -or- code is CloseStatusCode.NoStatus and reason is specified. -or- reason could not be UTF-8-encoded.
ArgumentOutOfRangeExceptionThe size of reason is greater than 123 bytes.

◆ Close() [4/5]

void WebSocketSharp.WebSocket.Close ( ushort  code)
inline

Closes the connection with the specified code.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeA ushort that specifies the status code indicating the reason for the close. The status codes are defined in Section 7.4of RFC 6455.
Exceptions
ArgumentOutOfRangeExceptioncode is less than 1000 or greater than 4999.
ArgumentExceptioncode is 1011 (server error). It cannot be used by a client. -or- code is 1010 (mandatory extension). It cannot be used by a server.

◆ Close() [5/5]

void WebSocketSharp.WebSocket.Close ( ushort  code,
string  reason 
)
inline

Closes the connection with the specified code and reason.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeA ushort that specifies the status code indicating the reason for the close. The status codes are defined in Section 7.4of RFC 6455.
reasonA string that specifies the reason for the close. Its size must be 123 bytes or less in UTF-8.
Exceptions
ArgumentOutOfRangeExceptioncode is less than 1000 or greater than 4999. -or- The size of reason is greater than 123 bytes.
ArgumentExceptioncode is 1011 (server error). It cannot be used by a client. -or- code is 1010 (mandatory extension). It cannot be used by a server. -or- code is 1005 (no status) and reason is specified. -or- reason could not be UTF-8-encoded.

◆ CloseAsync() [1/5]

void WebSocketSharp.WebSocket.CloseAsync ( )
inline

Closes the connection asynchronously.

This method does not wait for the close to be complete.

This method does nothing if the current state of the interface is Closing or Closed.

◆ CloseAsync() [2/5]

void WebSocketSharp.WebSocket.CloseAsync ( CloseStatusCode  code)
inline

Closes the connection asynchronously with the specified code.

This method does not wait for the close to be complete.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeOne of the CloseStatusCode enum values. It specifies the status code indicating the reason for the close.
Exceptions
ArgumentExceptioncode is CloseStatusCode.ServerError. It cannot be used by a client. -or- code is CloseStatusCode.MandatoryExtension. It cannot be used by a server.

◆ CloseAsync() [3/5]

void WebSocketSharp.WebSocket.CloseAsync ( CloseStatusCode  code,
string  reason 
)
inline

Closes the connection asynchronously with the specified code and reason.

This method does not wait for the close to be complete.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeOne of the CloseStatusCode enum values. It specifies the status code indicating the reason for the close.
reasonA string that specifies the reason for the close. Its size must be 123 bytes or less in UTF-8.
Exceptions
ArgumentExceptioncode is CloseStatusCode.ServerError. It cannot be used by a client. -or- code is CloseStatusCode.MandatoryExtension. It cannot be used by a server. -or- code is CloseStatusCode.NoStatus and reason is specified. -or- reason could not be UTF-8-encoded.
ArgumentOutOfRangeExceptionThe size of reason is greater than 123 bytes.

◆ CloseAsync() [4/5]

void WebSocketSharp.WebSocket.CloseAsync ( ushort  code)
inline

Closes the connection asynchronously with the specified code.

This method does not wait for the close to be complete.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeA ushort that specifies the status code indicating the reason for the close. The status codes are defined in Section 7.4of RFC 6455.
Exceptions
ArgumentOutOfRangeExceptioncode is less than 1000 or greater than 4999.
ArgumentExceptioncode is 1011 (server error). It cannot be used by a client. -or- code is 1010 (mandatory extension). It cannot be used by a server.

◆ CloseAsync() [5/5]

void WebSocketSharp.WebSocket.CloseAsync ( ushort  code,
string  reason 
)
inline

Closes the connection asynchronously with the specified code and reason.

This method does not wait for the close to be complete.

This method does nothing if the current state of the interface is Closing or Closed.

Parameters
codeA ushort that specifies the status code indicating the reason for the close. The status codes are defined in Section 7.4of RFC 6455.
reasonA string that specifies the reason for the close. Its size must be 123 bytes or less in UTF-8.
Exceptions
ArgumentOutOfRangeExceptioncode is less than 1000 or greater than 4999. -or- The size of reason is greater than 123 bytes.
ArgumentExceptioncode is 1011 (server error). It cannot be used by a client. -or- code is 1010 (mandatory extension). It cannot be used by a server. -or- code is 1005 (no status) and reason is specified. -or- reason could not be UTF-8-encoded.

◆ Connect()

void WebSocketSharp.WebSocket.Connect ( )
inline

Establishes a connection.

This method does nothing if the current state of the interface is Connecting or Open.

Exceptions
InvalidOperationExceptionThe interface is not for the client. -or- A series of reconnecting has failed.

◆ ConnectAsync()

void WebSocketSharp.WebSocket.ConnectAsync ( )
inline

Establishes a connection asynchronously.

This method does not wait for the connect process to be complete.

This method does nothing if the current state of the interface is Connecting or Open.

Exceptions
InvalidOperationExceptionThe interface is not for the client. -or- A series of reconnecting has failed.

◆ Ping() [1/2]

bool WebSocketSharp.WebSocket.Ping ( )
inline

Sends a ping to the remote endpoint.

Returns
true if the send has successfully done and a pong has been received within a time; otherwise, false.

◆ Ping() [2/2]

bool WebSocketSharp.WebSocket.Ping ( string  message)
inline

Sends a ping with the specified message to the remote endpoint.

Returns
true if the send has successfully done and a pong has been received within a time; otherwise, false.
Parameters
messageA string that specifies the message to send. Its size must be 125 bytes or less in UTF-8.
Exceptions
ArgumentExceptionmessage could not be UTF-8-encoded.
ArgumentOutOfRangeExceptionThe size of message is greater than 125 bytes.

◆ Send() [1/4]

void WebSocketSharp.WebSocket.Send ( byte[]  data)
inline

Sends the specified data to the remote endpoint.

Parameters
dataAn array of byte that specifies the binary data to send.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptiondata is null.

◆ Send() [2/4]

void WebSocketSharp.WebSocket.Send ( FileInfo  fileInfo)
inline

Sends the specified file to the remote endpoint.

Parameters
fileInfoA FileInfo that specifies the file to send. The file is sent as the binary data.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptionfileInfo is null.
ArgumentExceptionThe file does not exist. -or- The file could not be opened.

◆ Send() [3/4]

void WebSocketSharp.WebSocket.Send ( Stream  stream,
int  length 
)
inline

Sends the data from the specified stream instance to the remote endpoint.

Parameters
streamA Stream instance from which to read the data to send. The data is sent as the binary data.
lengthAn int that specifies the number of bytes to send.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptionstream is null.
ArgumentExceptionstream cannot be read. -or- length is less than 1. -or- No data could be read from stream .

◆ Send() [4/4]

void WebSocketSharp.WebSocket.Send ( string  data)
inline

Sends the specified data to the remote endpoint.

Parameters
dataA string that specifies the text data to send.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptiondata is null.
ArgumentExceptiondata could not be UTF-8-encoded.

◆ SendAsync() [1/4]

void WebSocketSharp.WebSocket.SendAsync ( byte[]  data,
Action< bool >  completed 
)
inline

Sends the specified data to the remote endpoint asynchronously.

This method does not wait for the send to be complete.

Parameters
dataAn array of byte that specifies the binary data to send.
completedAn T:System.Action<bool> delegate. The delegate invokes the method called when the send is complete. The bool parameter passed to the method is true if the send has successfully done; otherwise, false. null if not necessary.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptiondata is null.

◆ SendAsync() [2/4]

void WebSocketSharp.WebSocket.SendAsync ( FileInfo  fileInfo,
Action< bool >  completed 
)
inline

Sends the specified file to the remote endpoint asynchronously.

This method does not wait for the send to be complete.

Parameters
fileInfoA FileInfo that specifies the file to send. The file is sent as the binary data.
completedAn T:System.Action<bool> delegate. The delegate invokes the method called when the send is complete. The bool parameter passed to the method is true if the send has successfully done; otherwise, false. null if not necessary.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptionfileInfo is null.
ArgumentExceptionThe file does not exist. -or- The file could not be opened.

◆ SendAsync() [3/4]

void WebSocketSharp.WebSocket.SendAsync ( Stream  stream,
int  length,
Action< bool >  completed 
)
inline

Sends the data from the specified stream instance to the remote endpoint asynchronously.

This method does not wait for the send to be complete.

Parameters
streamA Stream instance from which to read the data to send. The data is sent as the binary data.
lengthAn int that specifies the number of bytes to send.
completedAn T:System.Action<bool> delegate. The delegate invokes the method called when the send is complete. The bool parameter passed to the method is true if the send has successfully done; otherwise, false. null if not necessary.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptionstream is null.
ArgumentExceptionstream cannot be read. -or- length is less than 1. -or- No data could be read from stream .

◆ SendAsync() [4/4]

void WebSocketSharp.WebSocket.SendAsync ( string  data,
Action< bool >  completed 
)
inline

Sends the specified data to the remote endpoint asynchronously.

This method does not wait for the send to be complete.

Parameters
dataA string that specifies the text data to send.
completedAn T:System.Action<bool> delegate. The delegate invokes the method called when the send is complete. The bool parameter passed to the method is true if the send has successfully done; otherwise, false. null if not necessary.
Exceptions
InvalidOperationExceptionThe current state of the interface is not Open.
ArgumentNullExceptiondata is null.
ArgumentExceptiondata could not be UTF-8-encoded.

◆ SetCookie()

void WebSocketSharp.WebSocket.SetCookie ( Cookie  cookie)
inline

Sets an HTTP cookie to send with the handshake request.

This method works if the current state of the interface is New or Closed.

Parameters
cookieA Cookie that specifies the cookie to send.
Exceptions
InvalidOperationExceptionThe interface is not for the client.
ArgumentNullExceptioncookie is null.

◆ SetCredentials()

void WebSocketSharp.WebSocket.SetCredentials ( string  username,
string  password,
bool  preAuth 
)
inline

Sets the credentials for the HTTP authentication (Basic/Digest).

This method works if the current state of the interface is New or Closed.

Parameters
usernameA string that specifies the username associated with the credentials. null or an empty string if initializes the credentials.
passwordA string that specifies the password for the username associated with the credentials. null or an empty string if not necessary.
preAuthA bool: true if sends the credentials for the Basic authentication in advance with the first handshake request; otherwise, false.
Exceptions
InvalidOperationExceptionThe interface is not for the client.
ArgumentExceptionusername contains an invalid character. -or- password contains an invalid character.

◆ SetProxy()

void WebSocketSharp.WebSocket.SetProxy ( string  url,
string  username,
string  password 
)
inline

Sets the URL of the HTTP proxy server through which to connect and the credentials for the HTTP proxy authentication (Basic/Digest).

This method works if the current state of the interface is New or Closed.

Parameters
urlA string that specifies the URL of the proxy server through which to connect. The syntax is http://&lt;host&gt;[:<port>]. null or an empty string if initializes the URL and the credentials.
usernameA string that specifies the username associated with the credentials. null or an empty string if the credentials are not necessary.
passwordA string that specifies the password for the username associated with the credentials. null or an empty string if not necessary.
Exceptions
InvalidOperationExceptionThe interface is not for the client.
ArgumentExceptionurl is not an absolute URI string. -or- The scheme of url is not http. -or- url includes the path segments. -or- username contains an invalid character. -or- password contains an invalid character.

Property Documentation

◆ Compression

CompressionMethod WebSocketSharp.WebSocket.Compression
getset

Gets or sets the compression method used to compress a message.

The set operation works if the current state of the interface is New or Closed.

One of the CompressionMethod enum values.

It indicates the compression method used to compress a message.

The default value is CompressionMethod.None.

Exceptions
InvalidOperationExceptionThe set operation is not available if the interface is not for the client.

◆ Cookies

IEnumerable<Cookie> WebSocketSharp.WebSocket.Cookies
get

Gets the HTTP cookies included in the handshake request/response.

An T:System.Collections.Generic.IEnumerable<WebSocketSharp.Net.Cookie> instance.

It provides an enumerator which supports the iteration over the collection of the cookies.

◆ Credentials

NetworkCredential WebSocketSharp.WebSocket.Credentials
get

Gets the credentials for the HTTP authentication (Basic/Digest).

A NetworkCredential that represents the credentials used to authenticate the client.

The default value is null.

◆ EmitOnPing

bool WebSocketSharp.WebSocket.EmitOnPing
getset

Gets or sets a value indicating whether the message event is emitted when the interface receives a ping.

true if the interface emits the message event when receives a ping; otherwise, false.

The default value is false.

◆ EnableRedirection

bool WebSocketSharp.WebSocket.EnableRedirection
getset

Gets or sets a value indicating whether the URL redirection for the handshake request is allowed.

The set operation works if the current state of the interface is New or Closed.

true if the interface allows the URL redirection for the handshake request; otherwise, false.

The default value is false.

Exceptions
InvalidOperationExceptionThe set operation is not available if the interface is not for the client.

◆ Extensions

string WebSocketSharp.WebSocket.Extensions
get

Gets the extensions selected by the server.

A string that represents a list of the extensions negotiated between the client and server.

An empty string if not specified or selected.

◆ IsAlive

bool WebSocketSharp.WebSocket.IsAlive
get

Gets a value indicating whether the communication is possible.

true if the communication is possible; otherwise, false.

◆ IsSecure

bool WebSocketSharp.WebSocket.IsSecure
get

Gets a value indicating whether the connection is secure.

true if the connection is secure; otherwise, false.

◆ Log

Logger WebSocketSharp.WebSocket.Log
getset

Gets the logging function.

The default logging level is LogLevel.Error.

A Logger that provides the logging function.

◆ Origin

string WebSocketSharp.WebSocket.Origin
getset

Gets or sets the value of the HTTP Origin header to send with the handshake request.

The HTTP Origin header is defined in Section 7 of RFC 6454

.

The interface sends the Origin header if this property has any.

The set operation works if the current state of the interface is New or Closed.

A string that represents the value of the Origin header to send.

The syntax is <scheme>://<host>[:<port>].

The default value is null.

Exceptions
InvalidOperationExceptionThe set operation is not available if the interface is not for the client.
ArgumentExceptionThe 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

string WebSocketSharp.WebSocket.Protocol
getset

Gets the name of subprotocol selected by the server.

A string that will be one of the names of subprotocols specified by client.

An empty string if not specified or selected.

◆ ReadyState

WebSocketState WebSocketSharp.WebSocket.ReadyState
get

Gets the current state of the interface.

One of the WebSocketState enum values.

It indicates the current state of the interface.

The default value is WebSocketState.New.

◆ SslConfiguration

ClientSslConfiguration WebSocketSharp.WebSocket.SslConfiguration
get

Gets the configuration for secure connection.

The configuration is used when the interface attempts to connect, so it must be configured before any connect method is called.

A ClientSslConfiguration that represents the configuration used to establish a secure connection.

Exceptions
InvalidOperationExceptionThe interface is not for the client. -or- The interface does not use a secure connection.

◆ Url

Uri WebSocketSharp.WebSocket.Url
get

Gets the URL to which to connect.

A Uri that represents the URL to which to connect.

Also it represents the URL requested by the client if the interface is for the server.

◆ WaitTime

TimeSpan WebSocketSharp.WebSocket.WaitTime
getset

Gets or sets the time to wait for the response to the ping or close.

The set operation works if the current state of the interface is New or Closed.

A TimeSpan that represents the time to wait for the response.

The default value is the same as 5 seconds if the interface is for the client.

Exceptions
ArgumentOutOfRangeExceptionThe value specified for a set operation is zero or less.

The documentation for this class was generated from the following file: