Rebex WebSocket
WebSocket library for modern and legacy platforms
Download 30-day free trial Buy from $349More .NET libraries
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Easy-to-use WebSocket client
On this page:
Simple API
Rebex WebSocket features a simple API that is suitable for most use cases:
// create an instance of WebSocket client var client = new WebSocketClient(); // connect to a server client.Connect("ws://echo.example.org"); // send a text message client.Send("Hello!"); // receive a text message string response = client.Receive<string>(); // send a binary message byte[] data = { 1, 2, 3, 4, 5, 6, 7 }; client.Send(data); // receive a binary message byte[] reply = client.Receive<byte[]>(); // close the WebSocket client.Close();
' create an instance of WebSocket client Dim client As New WebSocketClient() ' connect to a server client.Connect("ws://echo.example.org") ' send a text message client.Send("Hello!") ' receive a text message Dim response As String = client.Receive(Of String)() ' send a binary message Dim data As Byte() = {1, 2, 3, 4, 5, 6, 7} client.Send(Data) ' receive a binary message Dim reply As Byte() = client.Receive(Of Byte())() ' close the WebSocket client.Close()
Tip: In addition to
string
, the Send
/Receive
methods support a byte array type (byte[]
).
The Receive
method also supports the object
type, returning either a string or a byte array, depending
on the message type received from the server.
Tip:
WebSocketClient
also supports a Task-based asynchronous variant of this API.
Classic .NET-like API
Rebex WebSocket also features an asynchronous API that mimics .NET's ClientWebSocket
API:
// connect to a server await client.ConnectAsync("ws://echo.example.org", CancellationToken.None); // send a message byte[] message = Encoding.UTF8.GetBytes("Hello!"); await client.SendAsync(new ArraySegment<byte>(message), WebSocketMessageType.Text, true, CancellationToken.None);
' connect to a server Await client.ConnectAsync("ws://echo.example.org", CancellationToken.None) ' send a message Dim message As Byte() = Encoding.UTF8.GetBytes("Hello!") Await client.SendAsync(New ArraySegment(Of Byte)(message), WebSocketMessageType.Text, True, CancellationToken.None)
Tip:
WebSocketClient
also supports a synchronous variant of this API.
Options and settings
Use Options
property to configure the WebSocketClient
instance before connecting to the server:
// specify a subprotocol client.Options.AddSubProtocol("plus@sftp.ws"); // specify credentials client.Options.Credentials = new NetworkCredential("username", "password"); // set request header client.Options.SetRequestHeader("X-Custom-Token", "1234567"); // set keep-alive interval to once-per-minute client.Options.KeepAliveInterval = new TimeSpan(0, 1, 0); // connect to a server client.Connect(uri);
' specify a subprotocol client.Options.AddSubProtocol("plus@sftp.ws") ' specify credentials client.Options.Credentials = New NetworkCredential("username", "password") ' set request header client.Options.SetRequestHeader("X-Custom-Token", "1234567") ' set keep-alive interval to once-per-minute client.Options.KeepAliveInterval = New TimeSpan(0, 1, 0) ' connect to a server client.Connect(uri)
Check out TLS/SSL core for an overview of TLS/SSL options.
Back to feature list...