StreamLink.NET
How to publish data to the Liberator

The following example shows a class implementing the Caplin.StreamLink.Command.ICommandListener interface and publishing a "Bid" and an "Ask" value for the subject "/SubjectToPublishOn" to the Liberator.

The example can be split into a number of parts:

CopyC#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

using Caplin.StreamLink;
using Caplin.StreamLink.Command;
using Caplin.StreamLink.Authentication;
using Caplin.StreamLink.Logging;
using Caplin.StreamLink.Configuration;

namespace StreamLinkExamples.Publishing
{
    class PublisherExampleFull 
    {
        class MyCommandListener : ICommandListener
        {
            #region ICommandListener Members

            void ICommandListener.CommandSuccess(ICommandResultEvent ev)
            {
                // Command executed successfully
                Console.WriteLine("The publish request completed successfully");
            }

            void ICommandListener.CommandError(ICommandResultEvent ev)
            {
                // Command didn't execute successfully
                Console.WriteLine("The publish request failed with result code " + ev.Result);
            }
        }

        // Main execution method
        static void Example()
        {
            // Create basic streamlink
            IStreamLinkConfiguration myConfig = SimpleConfiguration.CreateType2HttpConnection("liberatorhost.mydomain.com", 8080);
            IStreamLink myStreamLink = new StreamLink(myConfig);
            myStreamLink.CredentialsProvider = new PasswordCredentialsProvider("admin", "admin");
            myStreamLink.StreamLinkProvider.Connect();

            // Create listener object
            ICommandListener listener = new MyCommandListener();

            // Create some field/values pairs that we wish to contribute
            Dictionary<string, string> fieldValues = new Dictionary<string, string>(); ;
            fieldValues.Add("Bid", "1.0");
            fieldValues.Add("Ask", "2.0");

            // Create publishing parameters using our values
            IPublishParameters parameters = myStreamLink.StreamLinkProvider.ParametersFactory.CreatePublishParameters(fieldValues);

            // And publish
            myStreamLink.StreamLinkProvider.Publish(listener, "/SubjectToPublishOn", parameters);

            // Sleep so we see see the contribution result
            Thread.Sleep(20000);

            myStreamLink.StreamLinkProvider.Disconnect();
        }
        #endregion
    }
}