Start publishing with .NET
Create a .NET client that publishes data through topics on the Diffusion™ server.
To complete this example, you need a Diffusion server and a development system with the .NET Framework installed on it.
You also require either a named user that has a role with the "ADMINISTRATOR" role. For more information about roles and permissions, see Role-based authorization.
and permissions. For example, theThe client publishes a value to the foo/counter topic every second. You can subscribe to the foo/counter topic by creating a client to subscribe to the topic. For more information, see Start subscribing with .NET.
Full example
The completed publishing client class
contains the following
code:
using System; using System.Threading; using PushTechnology.ClientInterface.Client.Callbacks; using PushTechnology.ClientInterface.Client.Factories; using PushTechnology.ClientInterface.Client.Features.Control.Topics; using PushTechnology.ClientInterface.Client.Topics; namespace PushTechnology.ClientInterface.GettingStarted { /// <summary> /// A client that publishes an incrementing count to the topic "foo/counter". /// </summary> public sealed class PublishingClient { public static void Main( string[] args ) { // Connect using a principal with 'modify_topic' and 'update_topic' permissions var session = Diffusion.Sessions.Principal( "principal" ).Password( "password" ).Open( "url" ); // Get the TopicControl and TopicUpdateControl features var topicControl = session.GetTopicControlFeature(); var updateControl = session.GetTopicUpdateControlFeature(); // Create a single-value topic 'foo/counter' var topic = "foo/counter"; var addCallback = new AddCallback(); topicControl.AddTopic( topic, TopicType.SINGLE_VALUE, addCallback ); // Wait for the OnTopicAdded callback, or a failure if ( !addCallback.Wait( TimeSpan.FromSeconds( 5 ) ) ) { Console.WriteLine( "Callback not received within timeout." ); return; } else if ( addCallback.Error != null ) { Console.WriteLine( "Error : {0}", addCallback.Error.ToString() ); return; } var updateCallback = new UpdateCallback( topic ); // Update the topic for 16 minutes for ( var i = 0; i < 1000; ++i ) { updateControl.Updater.Update( topic, i.ToString(), updateCallback ); Thread.Sleep( 1000 ); } // Close session session.Close(); } } /// <summary> /// Basic implementation of the ITopicControlAddCallback. /// </summary> internal sealed class AddCallback : ITopicControlAddCallback { private readonly AutoResetEvent resetEvent = new AutoResetEvent( false ); /// <summary> /// Any error from this AddCallback will be stored here. /// </summary> public Exception Error { get; private set; } public AddCallback() { Error = null; } /// <summary> /// This is called to notify that a call context was closed prematurely, typically due to a timeout or the /// session being closed. No further calls will be made for the context. /// </summary> public void OnDiscard() { Error = new Exception( "This context was closed prematurely." ); resetEvent.Set(); } /// <summary> /// This is called to notify that the topic has been added. /// </summary> /// <param name="topicPath">The full path of the topic that was added.</param> public void OnTopicAdded( string topicPath ) { resetEvent.Set(); } /// <summary> /// This is called to notify that an attempt to add a topic has failed. /// </summary> /// <param name="topicPath">The topic path as supplied to the add request.</param> /// <param name="reason">The reason for failure.</param> public void OnTopicAddFailed( string topicPath, TopicAddFailReason reason ) { Error = new Exception( string.Format( "Failed to add topic {0} : '{1}", topicPath, reason ) ); resetEvent.Set(); } public bool Wait( TimeSpan timeout ) { return resetEvent.WaitOne( timeout ); } } /// <summary> /// A simple ITopicUpdaterUpdateCallback implementation that prints confimation of the actions completed. /// </summary> internal sealed class UpdateCallback : ITopicUpdaterUpdateCallback { private readonly string topic; /// <summary> /// Constructor. /// </summary> /// <param name="topic">The topic path</param> public UpdateCallback( string topic ) { this.topic = topic; } /// <summary> /// Notification of a contextual error related to this callback. /// </summary> /// <remarks> /// Situations in which <code>OnError</code> is called include the session being closed, a /// communication timeout, or a problem with the provided parameters. No further calls will be made to this /// callback. /// </remarks> /// <param name="errorReason">A value representing the error</param> public void OnError( ErrorReason errorReason ) { Console.WriteLine( "Topic {0} could not be updated : {1}", topic, errorReason ); } /// <summary> /// Indicates a successful update. /// </summary> public void OnSuccess() { Console.WriteLine( "Topic {0} updated successfully.", topic ); } } }