.NET Client Classic API
The ExternalClient API provides the ability to connect to a Diffusion™ server as an external client from within any .NET application.
There is a single class called ExternalClient which can be instantiated with the required connection details and used to make an actual connection.
The topic or topics to subscribe to can be specified when connecting or at any time after connection.
When a connection object is instantiated, subscribe to the InboundMessageReceived delegate, which receives all messages for all topics.
The API permits the following types of connection to be specified by using the ServerDetails specified when configuring the connection object:
TCP | For a standard connection over DPT. This connects to the External Client Connector. |
TCPSSL | For a secure TCP/IP connection over DPTS. This connects to the External Client Connector. |
HTTP | For a connection using HTTP protocol |
HTTPSSL | For a secure connection using HTTP protocol |
WEBSOCKET | For a connection using WebSocket protocol |
WEBSOCKETSSL | For a secure connection using WebSocket protocol |
For a detailed description of the API, see the issued documentation (in docs directory).
The following example shows a simple client class which sends a message containing 'Hello' to the server until it receives a message from the server (Publisher) asking it to stop.
public class ClientApplication : IServerConnectionListener, ITopicListener { #region Fields private readonly PushTechnology.DiffusionExternalClient.ExternalClient theClient; #endregion // Fields #region Constructor public ClientApplication() { var connectionDetails = ConnectionFactory.CreateConnectionDetails( "ws://diffusion.example.com:80", "http://diffusion.example.com:80" ); connectionDetails.Topics = new TopicSet("MyTopic"); theClient = new PushTechnology.DiffusionExternalClient.ExternalClient(connectionDetails); // Add a topic listener – we are listening to all messages for this example, but individual topics can // also be used as selectors theClient.AddGlobalTopicListener( this ); // Now connect – this is an asynchronous process, so we have to wait until ServerConnected is invoked theClient.Connect(); } #endregion // Constructor #region Implementation of IServerConnectionListener /// <summary> /// Notification of connection. /// /// This is called when a connection to a server is established. /// </summary> /// <param name="connector">The server connector.</param> public void ServerConnected( IDiffusionClientConnector connector ) { Console.WriteLine( "Connected to server: " + connector ); // Send a message as we are now connected ITopicMessage message = theClient.CreateDeltaMessage( "MyTopic" ); // Populate the message message.Put( "Hello" ); // Send the message to the Diffusion server theClient.SendMessage( message ); } /// <summary> /// Notification that the status for a topic that was subscribed to has changed. /// </summary> /// <param name="connector">The connector.</param> /// <param name="topicName">The name of the topic on which the status has changed.</param> /// <param name="statusType">The topic status change type.</param> public void ServerTopicStatusChanged( IDiffusionClientConnector connector, string topicName, TopicStatusChangeType statusType ) { Console.WriteLine( string.Format( "Topic status for '{0}' changed to '{1}'.", topicName, statusType )); } /// <summary> /// Notification of rejected credentials from the server. /// </summary> /// <param name="connector"></param> /// <param name="credentials"></param> public void ServerRejectedCredentials( IDiffusionClientConnector connector, V4Credentials credentials ) { Console.WriteLine( "Server rejected credentials."); } /// <summary> /// Notification of disconnection. /// /// The reason for the disconnection can be established by checking the state of the connection /// using IDiffusionClientConnector.State. /// </summary> /// <param name="connector">The server connector.</param> /// <param name="args">The arguments which can be interrogated for the state and details of a server closure.</param> public void ServerDisconnected( IDiffusionClientConnector connector, ServerClosedEventArgs args ) { Console.WriteLine( "Disconnected from server."); } #endregion #region Implementation of ITopicListener /// <summary> /// Handles a message received from an IMessageSource. /// /// This handles an incoming message from a specified source. /// </summary> /// <param name="source">The message source.</param> /// <param name="message">The message.</param> public bool HandleTopicMessage( IMessageSource source, ITopicMessage message ) { if (message.AsString().Equals("STOP")) { theClient.Disconnect(); } return false; } #endregion }