Configuration - XSockets/XSockets.NET-4.0 GitHub Wiki

##Configuration When self-hosted the XSockets server will start with two endpoints loopback & machine IP (both on port 4502). For example my current machine would start the server at ws://127.0.0.1:4502 and ws:192.168.1.6:4502

Custom configuration

The namespace for configuration is XSockets.Core.Configuration

There are two ways of creating custom configuration.

Pass in configurations/settings to the StartServers method

Create one or more configuration classes that XSockets will implement at startup Passing configuration as a parametertop

Just create the configurations needed and pass them to StartServers

//using XSockets.Core.Common.Configuration;
//using XSockets.Core.Common.Socket;
//using XSockets.Core.Configuration;
//using XSockets.Plugin.Framework;

//List of IConfigurationSettings
var myCustomConfigs = new List<IConfigurationSetting>();
//Add one configuration
myCustomConfigs.Add(new ConfigurationSetting("ws://192.74.38.15:4502"));  
using (var server = Composable.GetExport<IXSocketServerContainer>())
{
    server.Start(configurationSettings:myCustomConfigs);
    Console.WriteLine("Started, hit enter to quit");
    Console.ReadLine();
    server.Stop();
}

Note: you can of course pass in several configuration.

Autoconfiguration with custom port

Just use the AutoConfiguration class to create a custom configuration that lets you choose a port to use. For example port 8181 as shown below.

using (var container = Composable.GetExport<IXSocketServerContainer>())
{
	//Get a custom config on port 8181
	var autoConfigPort8181 = new AutoConfiguration(defaultPort:8181).GetConfigurationSettings(true);
    
	//Start the server with the autoconfig for port 8181
	container.Start(configurationSettings:autoConfigPort8181);
    Console.ReadLine();
}

Configuration as a plugin

Just inherit the XSockets.Core.Configuration.ConfigurationSetting class and implement your configuration. XSockets will find and and use these custom configurations.

//using XSockets.Core.Configuration;

public class MyTestConfig : ConfigurationSetting
{
    public MyTestConfig() : base("ws://195.74.38.15:4502")
    {
    }
}

Now there is no need to pass in anything to the StartServers method, it will find the configuration above. When you use this technique the server will not create the default configuration. If you want to have for example 127.0.0.1:4502 as a configuration you have to add that as a plugin as well.

container.Start();

What can I configure

The Start method has a number of options, and then the ConfigurationSetting class itself has a number of options.

Method signature of Start

void Start(bool useLoopback = true, bool withInterceptors = true, IList<IConfigurationSetting> configurationSettings = null);

*Note: Interceptors are by default enabled!

DNS Configuration

One of the most common questions about configuration is how to enable DNS configuration.

Public Endpoint

Let's say that you want to connect (client <-> server) to ws://chucknorris.com:4502 the configuration for that could look like

//using XSockets.Core.Configuration;

public class ChuckNorrisConfig : ConfigurationSetting
{
    public ChuckNorrisConfig() : base(new Uri("ws://chucknorris.com:4502")) { }
}

**Note: This setup will create an endpoint based on the DNS provided. Note that port 4502 have to be open.

Public & Private Endpoint

Let's say that you want to connect (client <-> firewall <-> server) to ws://chucknorris.com:4502, but the public endpoint is represented by a firewall. Your firewall will then forward the connection to our servers private IP address (for example 192.168.1.7).

//using XSockets.Core.Configuration;

public class ChuckNorrisConfig : ConfigurationSetting
{
    public ChuckNorrisConfig() : base(new Uri("ws://chucknorris.com:4502"), new Uri("ws://192.168.1.7:4510")) { }
}

**Note: This setup requires that you forward traffic in your firewall to 192.168.1.7:4510

SSL/TLS

To get WSS you have to set the endpoint to be ´wss´ instead of ws, and you will also specify you certificate. This can either be done by setting CertificateLocation and CertificateSubjectDistinguishedName (as in the sample) or load the certificate from disk.

Certificate from store

//using System.Security.Cryptography.X509Certificates;
//using XSockets.Core.Configuration;

public class ChuckNorrisConfig : ConfigurationSetting
{
    public ChuckNorrisConfig() : base(new Uri("wss://chucknorris.com:4502"))
    {
        this.CertificateLocation = StoreLocation.LocalMachine;
        this.CertificateSubjectDistinguishedName = "cn=chucknorris.com";
    }
}

X509Certificate2

//using System.Security.Cryptography.X509Certificates;
//using XSockets.Core.Configuration;

public class ChuckNorrisConfig : ConfigurationSetting
{
    public ChuckNorrisConfig() : base(new Uri("wss://chucknorris.com:4502"))
    {
        this.Certificate = new X509Certificate2("file.name", "password");
    }
}