Custom handlers

Instead of subscribing to XmppXElementStreamObserver for handling packets we also can write handlers and inject them to the ChannelPipeline.

The following example is creating a handler that handles incoming software version requests XEP-0092: Software Version

using XmppDotNet;
using XmppDotNet.Xml;
using XmppDotNet.Xmpp;
using XmppDotNet.Xmpp.Client;
using System;
using System.Reactive.Linq;

/// <summary>
/// This handler automatically replies to incoming XMPP software version requests (XEP-0092: Software Version)
/// </summary>    
public class VersionHandler : XmppHandler
{
    public VersionHandler(XmppClient xmppClient)
        : base(xmppClient)
    {
        xmppClient
            .XmppXElementReceived
            .Where(
                el =>
                    el.OfType<Iq>()
                    && el.Cast<Iq>().Type == IqType.Get
                    && el.Cast<Iq>().Query.OfType<XmppDotNet.Xmpp.Version.Version>()
            )
            .Subscribe(async el =>
            {
                var iq = el.Cast<Iq>();

                var resIq = new VersionIq();
                resIq.Id = iq.Id;
                resIq.To = iq.From;
                resIq.Type = IqType.Result;

                resIq.Version.Name = "XmppDotNet-Client";
                resIq.Version.Os = "Windows";
                resIq.Version.Ver = "1.2.0";

                await xmppClient.SendAsync(resIq).ConfigureAwait(false);
            });
    }
}

Now our handler then can be ingested to the XmppClient. We add the handler in the constructor when we create our XmppClinet instance.

 var xmppClient = new XmppClient(
    conf =>
    {
        conf.UseSocketTransport();
        conf.AutoReconnect = true;

    },
    (handlers, client) => handlers.Add(new VersionHandler(client)))
{
    Jid = "user@server.com",
    Password = "***secret***"
};