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:
- Implementation of the Caplin.StreamLink.Command.ICommandListener interface so that the result of the publication can be received.
- Creating a StreamLink object and connecting to the Liberator with the username "admin" and the password "admin".
- Populating a dictionary for field-value pairs.
- Creating a Caplin.StreamLink.Command.IPublishParameters object containing these field-value pairs.
- Calling the Caplin.StreamLink.IStreamLinkProvider.Publish method to publish these values to the Liberator.
- Sleeping for a short period to ensure that the response has been received.
- Disconnecting from the Liberator.
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 } }