Skip to content

Instantly share code, notes, and snippets.

@XSockets
Created October 28, 2013 18:22
Show Gist options
  • Save XSockets/7201916 to your computer and use it in GitHub Desktop.
Save XSockets/7201916 to your computer and use it in GitHub Desktop.
Based on XSockets 3.0 (not yet released) A sample of how easy it is to create a PerformanceCounter in XSockets. We do this by creating interceptors (or a custom pipeline if we are only monitoring messages) NOTE: You have to run the process as admin to be able to create interceptors
/*
Note that we as always in XSockets only have to implement the interface and the framework will find the plugin.
If you wan tto you can even load assemblies with plugins at runtime (or just reference them if youre lazy)
*/
/// <summary>
/// An example of a custom message interceptor that writes to performancecounters for messages.
///
/// Note: Be sure do use thread safe techniques in interceptors
/// </summary>
public class MyMessageInterceptor : IMessageInterceptor
{
/// <summary>
/// A performance counter for messages
/// </summary>
private static readonly XSocketsMessageCounter MessageCounter;
static MyMessageInterceptor()
{
MessageCounter = new XSocketsMessageCounter();
}
/// <summary>
/// Text-message sent TO the server
/// </summary>
/// <param name="socket"></param>
/// <param name="textArgs"></param>
public void OnMessage(IXSocketController socket, ITextArgs textArgs)
{
MessageCounter.IncrementIn();
}
/// <summary>
/// Binary-message sent TO the server
/// </summary>
/// <param name="socket"></param>
/// <param name="binaryArgs"></param>
public void OnMessage(IXSocketController socket, IBinaryArgs binaryArgs)
{
MessageCounter.IncrementIn();
}
/// <summary>
/// Text-message sent FROM the server
/// </summary>
/// <param name="socket"></param>
/// <param name="textArgs"></param>
public void OnSend(IXSocketController socket, ITextArgs textArgs)
{
MessageCounter.IncrementOut();
}
/// <summary>
/// Binary-message sent FROM the server
/// </summary>
/// <param name="socket"></param>
/// <param name="binaryArgs"></param>
public void OnSend(IXSocketController socket, IBinaryArgs binaryArgs)
{
MessageCounter.IncrementOut();
}
}
//Just a sample of starting the server(s)
//Note the new autoconfiguration...
class Program
{
static void Main(string[] args)
{
//get the servercontainer.
var wss = Composable.GetExport<IXSocketServerContainer>();
//enable interceptors
wss.StartServers(withInterceptors: true);
//display serverinfo
foreach (var server in wss.Servers)
{
Console.WriteLine("Started Server: {0}:{1}",server.ConfigurationSetting.Location ,server.ConfigurationSetting.Port);
Console.WriteLine("Scheme: {0}",server.ConfigurationSetting.Scheme);
Console.WriteLine("SSL/TLS: {0}", server.ConfigurationSetting.IsSecure);
Console.WriteLine("Allowed Connections (0 = infinite): {0}",server.ConfigurationSetting.NumberOfAllowedConections);
Console.WriteLine();
}
Console.WriteLine("Hit enter to quit...");
Console.ReadLine();
}
}
/* Output:
Started Server: 192.168.1.7:4502
Scheme: ws
SSL/TLS: False
Allowed Connections (0 = infinite): 0
Started Server: 127.0.0.1:4502
Scheme: ws
SSL/TLS: False
Allowed Connections (0 = infinite): 0
Hit enter to quit...
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment