WebSocketClient Class
Namespace: Rebex.Net
Assembly: Rebex.WebSocket.dll (version 8.0.9673)
WebSocket client API.
See https://www.rebex.net/websocket/features/ for code snippets of common use cases.
Syntax
public class WebSocketClient : NetworkSession, ILogWriterProvider, IDisposable
Inherited Members
Examples
See https://www.rebex.net/websocket/samples.aspx for ready-to-use samples.
// initialize new WebSocket client instance
var client = new Rebex.Net.WebSocketClient();
// attach logger for diagnostic purposes
client.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\web-socket.log");
// connect the client
// - use "ws://" for unsecured connections
// - use "wss://" for TLS secured connections
client.Connect($"wss://{hostname}");
// send a message
client.Send("Test");
// receive the response
var response = client.Receive<string>();
Console.WriteLine(response);
Constructors
| Name | Description |
|---|---|
| WebSocketClient() | Creates a new instance of WebSocketClient. See https://www.rebex.net/websocket/features/ for code snippets of common use cases. |
Properties
| Name | Description |
|---|---|
| CloseStatus | Gets the close status (only available once the WebSocket has been closed). |
| CloseStatusDescription | Gets the close status description (only available once the WebSocket has been closed). |
| DefaultLogWriter | Gets or sets a default log writer used by all new instances of NetworkSession object. Inherited from NetworkSession. |
| HttpResponseHeaders | Gets or sets the HTTP response headers or null if not available. |
| HttpStatusCode | Gets the HTTP response status code or zero if not available. |
| InstanceId | Gets instance ID (intended for logging purposes). Inherited from NetworkSession. |
| IsAuthenticated | Gets a value indicating whether the session is authenticated. Inherited from NetworkSession. |
| IsConnected | Gets a value indicating whether the session is connected. Inherited from NetworkSession. |
| LogWriter | Gets or sets the LogWriter used by this object. Inherited from NetworkSession. |
| Options | WebSocket options. |
| Proxy | Gets or sets the network proxy to use to access a remote server. |
| ServerName | Gets the server name, if available. Inherited from NetworkSession. |
| ServerPort | Gets the server port, if available. Inherited from NetworkSession. |
| Settings | Gets an WebSocketClientSettings object. |
| State | Gets the last known state of the WebSocket object. |
| SubProtocol | Gets the sub-protocol selected by the server (only available once the WebSocket has been connected). |
| UserName | Gets the authenticated user name, if available. Inherited from NetworkSession. |
Methods
| Name | Description |
|---|---|
| Abort() | Aborts the WebSocket and all active operations. |
| Close() | Closes the sending part of the WebSocket (unless already closed) and waits for the receiving part to be closed. |
| Close(WebSocketCloseStatus, String) | Closes the sending part of the WebSocket (unless already closed) and waits for the receiving part to be closed. |
| CloseAsync(WebSocketCloseStatus, String, CancellationToken) | Closes the sending part of the WebSocket (unless already closed) and waits for the receiving part to be closed. |
| CloseOutput(WebSocketCloseStatus, String) | Closes the sending part of the WebSocket, making it possible to still receive messages from the server. |
| CloseOutputAsync(WebSocketCloseStatus, String, CancellationToken) | Closes the sending part of the WebSocket, making it possible to still receive messages from the server. |
| Connect(String) | Establish a WebSocket connection to the specified URL. |
| Connect(Uri) | Establish a WebSocket connection to the specified URL. |
| ConnectAsync(String, CancellationToken) | Establish a WebSocket connection to the specified URL. |
| ConnectAsync(Uri, CancellationToken) | Establish a WebSocket connection to the specified URL. |
| Dispose() | Disposes the WebSocketClient object and the underlying connection. |
| GetHashCode() | Gets hash code for this object. Inherited from NetworkSession. |
| Poll(Int32) | Wait until a message fragment is received or the specified timeout has elapsed. |
| Receive(ArraySegment<Byte>) | Receives a message fragment from the server. |
| Receive<T>() | Receives a message of the specified type from the server. Only String, byte array and Object types are currently supported. |
| ReceiveAsync(ArraySegment<Byte>, CancellationToken) | Receives a message fragment from the server. |
| ReceiveAsync<T>(CancellationToken) | Receives a message of the specified type from the server. Only String, byte array and Object types are currently supported. |
| Send(ArraySegment<Byte>, WebSocketMessageType, WebSocketMessageFlags) | Sends the specified message fragment to the server. |
| Send(ArraySegment<Byte>, WebSocketMessageType, Boolean) | Sends the specified message fragment to the server. |
| Send(Byte[]) | Sends the specified binary message to the server. |
| Send(String) | Sends the specified text message to the server. |
| SendAsync(ArraySegment<Byte>, WebSocketMessageType, WebSocketMessageFlags, CancellationToken) | Sends the specified message fragment to the server. |
| SendAsync(ArraySegment<Byte>, WebSocketMessageType, Boolean, CancellationToken) | Sends the specified message fragment to the server. |
| SendAsync(Byte[]) | Sends the specified binary message to the server. |
| SendAsync(String) | Sends the specified text message to the server. |
| SetSocketFactory(ISocketFactory) | Sets the socket factory to be used to create communication sockets. |
Events
| Name | Description |
|---|---|
| ValidatingCertificate | Occurs when a server certificate needs to be validated. |