Rebex WebSockets (beta)

Multiplatform WebSockets library for .NET 2.0-4.6, .NET CF 2.0-3.5, Xamarin.iOS, Xamarin.Android and Xamarin.Mac

WebSockets are becoming increasingly popular, but support in .NET is far from perfect. Versions of .NET prior to 4.5 don't support WebSockets at all, and even .NET 4.5 only supports WebSockets on Windows 8 or higher.

Rebex WebSockets combines Rebex networking library with Xamarin's open source WebSocket implementation. The result is a multi-platform library that works on both current and legacy platforms. WebSockets over a secure TLS/SSL channel are supported as well - including TLS 1.2, 1.1, 1.0, SNI and SHA-2.

Download Rebex WebSockets client beta (trial) »

Supported platforms

Usage

Rebex WebSockets API resembles the one provided by .NET 4.5 with minor differences (such as WebSocketClient instead of ClientWebSocket) and some syntactic sugar (string-based Send and Receive methods):

C# sample code

...
using Rebex.Net;
...

// create an instace of WebSocket client
var client = new WebSocketClient();
client.ValidatingCertificate += CustomCertificateValidator;

// connect to a server
client.Connect("ws://echo.websocket.org");

// send a message
client.Send("Hello!");

// receive response
string response = client.Receive();

// display it
Console.WriteLine(response);

Limitations on .NET Compact Framework

Legacy platforms targeted by .NET Compact Framework (such as Windows CE 5.0 or 6.x) lack certificate validator with SHA-2 signature support. We are working on a custom replacement, but until it's finished, you will have to provide a custom certificate validator:

...
using Rebex.Net;
...
// create an instace of WebSocket client
var client = new WebSocketClient();

// register custom certificate validator
client.ValidatingCertificate += CustomCertificateValidator;

// connect to a server
client.Connect("wss://echo.websocket.org");
...

Certificate verifier might looks like this:

// list of trusted certificate thumbprints
// (replace with a persistend storage if needed)
List<string> TrustedThumbprints = new List<string>();

void CustomCertificateValidator(object sender, SslCertificateValidationEventArgs e)
{
	// try using the default certificate validator (uses Windows certificate infrastructure)
	ValidationResult res = e.CertificateChain.Validate(e.ServerName, 0);
	if (res.Valid)
	{
		e.Accept();
		return;
	}
	// get the server certificate
	Certificate cert = e.CertificateChain.LeafCertificate;
	// check whether the certificate is already trusted
	if (TrustedThumbprints.Contains(cert.Thumbprint))
	{
		e.Accept();
		return;
	}
	// the certificate is not trusted yet - show it to the user
	StringBuilder sb = new StringBuilder();
	sb.Append("Do you trust the following certificate?\r\n");
	sb.Append(" Common name:\t" + cert.GetCommonName() + "\r\n");
	sb.Append(" Thumbprint:\t" + cert.Thumbprint + "\r\n");
	sb.Append(" Expires on:\t" + cert.GetExpirationDate().ToString() + "\r\n");
	// ask user whether to trust this certificate
	DialogResult result = MessageBox.Show(
		sb.ToString(),
		"Do you trust this certificate?",
		MessageBoxButtons.YesNo,
		MessageBoxIcon.Question,
		MessageBoxDefaultButton.Button2
		);
	// accept or reject the certificate based on user response
	if (result == DialogResult.Yes)
	{
		e.Accept();
		// OPTIONALLY store the current certificate in a list of explicitly trusted certificates
		WriteLine("Adding certificate thumbprint to trusted certificate store.");
		TrustedThumbprints.Add(cert.Thumbprint);
	}
	else
	{
		e.Reject();
	}
}

Changelog

2016-09-15 - First version of Rebex WebSockets released on Rebex Labs.

Send us feedback

Do you have any comments, questions or suggestions? Either post at our support forum or contact us directly at support@rebex.net.