Modules & Plugins - XSockets/XSockets.NET-4.0 GitHub Wiki
This section covers how to create custom modules/plugins for different parts of XSockets. This only covers the Interfaces
within XSockets, but you can ofcourse add your custom interfaces as plugins by telling the plugin framework about the interfaces you want to use. Read more about custom plugin in The Plugin Framework
section
###How to implement your custom Pipeline
The server will only have one XSockets.Core.Common.Socket.IXSocketPipeline
, but you can override the default one by just deriving it.
Each message passing into the server or out of the server will pass the pipeline
//using XSockets.Core.Common.Socket;
//using XSockets.Core.XSocket;
//using XSockets.Core.Common.Socket.Event.Interface;
public class MyPipeline : XSocketPipeline
{
public override void OnIncomingMessage(IXSocketController controller, IMessage e)
{
Debug.WriteLine(string.Format("IN: Controller:{}, Topic:{1}, Data:{2}", e.Controller, e.Topic, e.Data));
base.OnIncomingMessage(controller, e);
}
public override IMessage OnOutgoingMessage(IXSocketController controller, IMessage e)
{
Debug.WriteLine(string.Format("OUT: Controller:{}, Topic:{1}, Data:{2}", e.Controller, e.Topic, e.Data));
return base.OnOutgoingMessage(controller, e);
}
}
When the socket is connected and the handshake is completed the AuthenticationPipeline
will be called. By default the pipeline will look for a FormsAuthenticationTicket, but you can override this pipline by just implementing a interface XSockets.Core.Common.Socket.IXSocketAuthenticationPipeline
There can only be one pipeline so even if you implement several pipelines only one wil be used.
//using XSockets.Core.Common.Protocol;
//using XSockets.Core.Common.Socket;
//using XSockets.Plugin.Framework.Attributes;
[Export(typeof(IXSocketAuthenticationPipeline))]
public class MyAuthenticationPipeline : IXSocketAuthenticationPipeline
{
public IPrincipal GetPrincipal(IXSocketProtocol protocol)
{
if (protocol.ConnectionContext.User == null)
{
//creating a fake super user ;)
var roles = new string[]{"superman","hulk"};
var userIdentity = new GenericIdentity("David");
protocol.ConnectionContext.User = new GenericPrincipal(userIdentity, roles);
}
return protocol.ConnectionContext.User;
}
}
There can only be one Pipeline
, and one AuthenticationPipeline
, but interceptors can be 0 to N. Every interceptor will be called at a specific time. ConnectionInterceptors for example will be called when someone connects, disconnects or when a handshake is completed (ok or not).
Interceptors are common when debugging or logging, but XSockets does not choose a logger for you. Implement the interface and do whatever you want inside of the interceptor(s).
Sample of a connection interceptor that logs handshake and connect/disconnect with debug
level
//using XSockets.Core.Common.Interceptor;
//using XSockets.Core.Common.Protocol;
//using XSockets.Core.Common.Utility.Logging;
//using XSockets.Plugin.Framework;
public class MyConnectionInterceptor : IConnectionInterceptor
{
public void Connected(IXSocketProtocol protocol)
{
Composable.GetExport<IXLogger>().Verbose("Connected {@protocol}" , protocol.ConnectionContext);
}
public void Disconnected(IXSocketProtocol protocol)
{
Composable.GetExport<IXLogger>().Verbose("Disconnected {@protocol}", protocol.ConnectionContext);
}
public void HandshakeCompleted(IXSocketProtocol protocol)
{
Composable.GetExport<IXLogger>().Verbose("Handshake ok {@protocol}", protocol.ConnectionContext);
}
public void HandshakeInvalid(string rawHandshake)
{
Composable.GetExport<IXLogger>().Verbose("Handshake failed {raw}", rawHandshake);
}
}
#####How to write MessageInterceptors
Sample of a message interceptor that logs in/out messages with debug
level
//using XSockets.Core.Common.Interceptor;
//using XSockets.Core.Common.Protocol;
//using XSockets.Core.Common.Socket.Event.Interface;
//using XSockets.Core.Common.Utility.Logging;
//using XSockets.Plugin.Framework;
public class MyMessageInterceptor : IMessageInterceptor
{
public void OnIncomingMessage(IXSocketProtocol protocol, IMessage message)
{
Composable.GetExport<IXLogger>().Debug("{incoming message {@message}}", message);
}
public void OnOutgoingMessage(IXSocketProtocol protocol, IMessage message)
{
Composable.GetExport<IXLogger>().Debug("{outgoing message {@message}}", message);
}
}
#####How to write ErrorInterceptors
Sample of a error interceptor that logs exceptions with error
level
//using System;
//using XSockets.Core.Common.Interceptor;
//using XSockets.Core.Common.Utility.Logging;
//using XSockets.Plugin.Framework;
public class MyErrorInterceptor : IErrorInterceptor
{
public void OnError(Exception exception)
{
Composable.GetExport<IXLogger>().Error(exception, "Exception");
}
}
###How to write a custom JSON Serializer
You can replace the default serializer with your own favorite serializer. As everything else in XSockets.NET it is just a module/plugin. Implement the IXSocketJsonSerializer
interface and export the new plugin to tell XSockets to use your serializer instead of the defualt one.
In the sample below we use the ServiceStack.Text serializer
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Text;
using XSockets.Core.Common.Utility.Serialization;
namespace MyNameSpace.Serialization
{
[Export(typeof(IXSocketJsonSerializer))]
public class MyJsonSerializer : IXSocketJsonSerializer
{
public MyJsonSerializer()
{
JsConfig.ExcludeTypeInfo = true;
JsConfig.IncludeTypeInfo = false;
}
public string SerializeToString<T>(T obj)
{
return JsonSerializer.SerializeToString(obj);
}
public string SerializeToString(object obj, Type type)
{
return JsonSerializer.SerializeToString(obj, type);
}
public T DeserializeFromString<T>(string json)
{
return JsonSerializer.DeserializeFromString<T>(json);
}
public object DeserializeFromString(string json, Type type)
{
return JsonSerializer.DeserializeFromString(json, type);
}
public IDictionary<string,string> DeserializeFromString(string json, params string[] keys)
{
var obj = JsonSerializer.DeserializeFromString<List<JsonObject>>(@json);
return keys.ToDictionary(key => key, key => obj[0].Child(key));
}
}
}
###How to write custom Protocols Since everything (almost) is a modules/plugin in XSockets you can add custom protocols. This enables you to write your own protocols so that you can connect other "things" than the clients we have created. The good thing about this is that even though your clients use different protocols they will still be able to communicate with each other cross-protocol!
See our code sample and video at the [XSockets Academy](http://xsockets.net/academy/xva-05-02-customprotocol) page